From scikit-learn, which is often used when doing machine learning with Python, a dazzling library that can be used by calling the recently talked about TensorFlow has appeared. Moreover, it is made by google. That is skflow.
Until now, scikit-learn did not have a neural network implementation, but this makes it possible to handle not only neural networks but also deep learning. Also, for TensorFlow side, it is possible to link with the data preprocessing function (Preprocessing etc.) that is abundant on scikit-learn side. It's a big advantage.
In that sense, I think the advent of skflow, which connects the two, will make it very easy to build machine learning processes.
Since 0.8.0 of TensorFlow, skflow has been incorporated into the main body, so a separate installation is no longer necessary. As an option, it is convenient to install scikit-learn
, numpy
, pandas
, but you can use it without it.
This is a quote from GitHub, but the linear classifier uses the following.
from tensorflow.contrib import skflow
from sklearn import datasets, metrics
iris = datasets.load_iris()
classifier = skflow.TensorFlowLinearClassifier(n_classes=3)
classifier.fit(iris.data, iris.target)
score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))
print("Accuracy: %f" % score)
skflow.TensorFlowLinearClassifier
is the IF with the TensorFlow side. After this, you can proceed with the processing with the familiar API in scikit-learn such as fit
.
The neural network is as follows. Using TensorFlowDNNClassifier
, we are building a neural network with hidden layers of 10-20-10.
from tensorflow.contrib import skflow
from sklearn import datasets, metrics
iris = datasets.load_iris()
classifier = skflow.TensorFlowDNNClassifier(hidden_units=[10, 20, 10], n_classes=3)
classifier.fit(iris.data, iris.target)
score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))
print("Accuracy: %f" % score)
If you want to build the model in more detail, do as follows. It feels like building directly with skflow.ops
.
from tensorflow.contrib import skflow
from sklearn import datasets, metrics
iris = datasets.load_iris()
def my_model(X, y):
"""This is DNN with 10, 20, 10 hidden layers, and dropout of 0.5 probability."""
layers = skflow.ops.dnn(X, [10, 20, 10], dropout=0.5)
return skflow.models.logistic_regression(layers, y)
classifier = skflow.TensorFlowEstimator(model_fn=my_model, n_classes=3)
classifier.fit(iris.data, iris.target)
score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))
print("Accuracy: %f" % score)
It was a simple introduction, but on the contrary, it is so simple that it is rarely introduced. If you are interested in TensorFlow but don't want to learn a new way of writing, please take a look.