Neural network with Python (scikit-learn)

2020/2 added

More detailed contents are posted on the following blog. https://kakedashi-engineer.appspot.com/2020/02/03/ml-sklearn/

Neural network library

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.

Supplement: model_selection module

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

Neural network with Python (scikit-learn)
Neural network with OpenCV 3 and Python 3
3. Normal distribution with neural network!
Neural network starting with Chainer
[Python] Linear regression with scikit-learn
Neural network implementation in python
4. Circle parameters with neural network!
Network programming with Python Scapy
Measuring network one-way delay with python
PRML Chapter 5 Neural Network Python Implementation
Simple classification model with neural network
[TensorFlow] [Keras] Neural network construction with Keras
Python sample to learn XOR with genetic algorithm with neural network
FizzBuzz with Python3
Compose with a neural network! Run Magenta
Scraping with Python
Statistics with python
Predict time series data with neural network
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
[Python] Use string data with scikit-learn SVM
Play with 2016-Python
AES256 with python
Tested with Python
3. Natural language processing with Python 2-1. Co-occurrence network
python starts with ()
DBSCAN with scikit-learn
Persist the neural network built with PyBrain
Clustering with scikit-learn (1)
with syntax (Python)
Clustering with scikit-learn (2)
PCA with Scikit-learn
Bingo with python
Zundokokiyoshi with python
Python & Machine Learning Study Memo ③: Neural Network
kmeans ++ with scikit-learn
2. Mean and standard deviation with neural network!
Excel with Python
Microcomputer with Python
Parametric Neural Network
Cast with python
[Python / Machine Learning] Why Deep Learning # 1 Perceptron Neural Network
3. Natural language processing with Python 2-2. Co-occurrence network [mecab-ipadic-NEologd]
Load the network modeled with Rhinoceros in Python ③
Experiment with various optimization algorithms with a neural network
Verification of Batch Normalization with multi-layer neural network
Load the network modeled with Rhinoceros in Python ②
Load the network modeled with Rhinoceros in Python ①
Template network config generation with Python and Jinja2
Serial communication with Python
Zip, unzip with python
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Cross Validation with scikit-learn
Try scraping with Python.
Sequential search with Python