More detailed contents are posted on the following blog. https://kakedashi-engineer.appspot.com/2020/02/03/ml-sklearn/
There are quite a few times when I want to play with a neural network (multilayer perceptron). Recently, a lot of libraries for tensorflow, chainer, theano and deep learning have been released, but it can be a little troublesome to build an environment. And for some reason, the neural network was not implemented in scikit-learn, which is familiar in machine learning. (By the way, there is also a library called PyBrain) scikit-learn 0.18.0 However, the neural network was finally implemented in Ver. 0.18.0 released in September 2016. Now you can use neural networks with the familiar super-simple API. You did it. In the sample code below, iris classification is performed.
sklearn_nn.py
# -*- coding: utf-8 -*-
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
clf = MLPClassifier(solver="sgd",random_state=0,max_iter=10000)
clf.fit(X_train, y_train)
print (clf.score(X_test, y_test))
neural_network.MLPClassifier() Besides neural_network.BernoulliRBM() neural_network.MLPRegressor() and so on.
API About the arguments that neural_network.MLPClassifier () takes. For more information official http://scikit-learn.org/stable/modules/generated/sklearn.neural_network.MLPClassifier.html#sklearn.neural_network.MLPClassifier I want you to see, but I will summarize it briefly. (The right side is the default value)
python
hidden_layer_sizes=(100, )#Number of nodes in the hidden layer(Can be multi-layered)
activation='relu'#Activation function(identify, logistic, tanh, relu)
solver='adam'#Optimization method(lbfgs(Quasi-Newton method), sgd, adam)
alpha=0.0001
batch_size='auto'#Batch size(sgd,Apply with adam)
learning_rate='constant'
learning_rate_init=0.001
power_t=0.5
max_iter=200#Maximum number of epochs
shuffle=True#Shuffle the sample with each iteration
random_state=None
tol=0.0001
verbose=False
warm_start=False
momentum=0.9
nesterovs_momentum=True
early_stopping=False
validation_fraction=0.1
beta_1=0.9
beta_2=0.999
epsilon=1e-08
You can see that it can be set quite finely. I think this is enough for most people who want to use neural networks. However, GPGPU etc. cannot be used, so those people should use tensorflow etc. obediently.
train_test_split was previously in the cross_validation module, Seems to have moved from 0.18.0 to the model_selection module.
Recommended Posts