Memorandum @ Python OR Seminar: scikit-learn

I think python is one of the best libraries. Library for machine learning. The learning included is as follows (example).

--Supervised learning --Nearest neighbor method, generalized linear model, linear discriminant analysis, SVM, decision tree, random forest, naive Bayes, etc. --Unsupervised learning --Mixed Gaussian model, principal component analysis, factor analysis, independent component analysis, clustering, hidden Markov model, etc. --Other --Cross validation, grid search, accuracy, etc.

Supervised learning

Support Vector Machine

regression analysis

#First, create learning data
>>> import numpy
>>> np.seed(0) #Random number seed fixed
>>> x = numpy.sort(5 * numpy.random.rand(40, 1), axis=0)
>>> y = numpy.sin(x).ravel()
>>> y[::5] += 3 * (0.5 - numpy.random.rand(8))
>>> plot(x, y, 'o')

Use SVR for regression analysis (abbreviation for Support Vector Regression). About argument options

C (default = 1.0)

  • Penalty parameters
  • Larger does not allow margin (hard margin), smaller allows margin

kernel (default = rbf)

  • Kernel function type
  • Linear: linear, Polynomial: poly, RBF (Gauss): rbf, Sigmoid: sigmoid, Precomputed: precomputed

gamma (default = 0.0)

  • RBF, polynomial kernel coefficients

degree (default = 2)

  • Degrees of RBF, polynomials, sigmoid kernel functions
>>> from sklearn.svm import SVR
#Creating a learner
>>> svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
>>> svr_lin = SVR(kernel='linear', C=1e3)
>>> svr_poly = SVR(kernel='poly', C=1e3, degree=2)
#Learn with fit, predict with predict
>>> y_rbf = svr_rbf.fit(x, y).predict(x)
>>> y_lin = svr_lin.fit(x, y).predict(x)
>>> y_poly = svr_poly.fit(x, y).predict(x)

Classification

I think the so-called SVM is here. Scikit-learn uses SVC (short for Support Vector Classifier).

#Learning data creation
>>> numpy.random.seed(0)
>>> X = numpy.random.randn(300, 2)
>>> Y = numpy.logical_xor(X[:,0]>0, X[:,1]>0)
from sklearn.svm import SVC
#Creating a classifier
>>> clf = SVC(kernel='rbf', C=1e3, gamma=0.1)
#Learning
>>> clf.fit(X, Y)
#Calculate the distance to the decision function
>>> xx, yy = np.meshgrid(linspace(-3, 3, 500), linspace(-3, 3, 500))
>>> Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
>>> Z = Z.reshape(xx.shape)
#Graphing
>>> imshow(Z, interpolation='nearest', extent=[xx.min(),
...                                            xx.max(),
...                                            yy.min(),
...                                            yy.max()],
...                                            aspect='auto',
...                                            origin='lower',
...                                            cmap=cm.PuOr_r)
>>> ctr = contour(xx, yy, Z, levels=[0], linetypes='--')
>>> scatter(X[:, 0], X[:, 1], c=Y, cmap=cm.Paired)
>>> axis([xx.min(), xx.max(), yy.min(), yy.max()])
>>> show()

ʻImshow () : Graph the array Argument options: interpolation`

  • Complementation during graph processing
    • 'nearest' -

extent

  • Specify range -[Minimum in the horizontal direction, Maximum value in the horizontal direction, Minimum value in the vertical direction, Maximum value in the vertical direction]

aspect

  • Aspect ratio adjustment

origin

  • Set a reference point --'lower' --Align Z [0,0] to the lower left corner

cmap

  • Specify color map

Unsupervised learning

k-means method

The k-means method is shown as an example.

>>> import sklearn.datasets, sklearn.cluster
>>> #Reading IRIS data
>>> d = sklearn.datasets.load_iris()
>>> km = sklearn.cluster.KMeans(3)
>>> km.fit(d.data)
>>> for i, e in enumerate(d.data):
...    scatter(e[0], e[2], c='rgb'[km.labels_[i]])

Other

The rest is Official Documents.

Recommended Posts

Memorandum @ Python OR Seminar: scikit-learn
Memorandum @ Python OR Seminar
Memorandum @ Python OR Seminar: matplotlib
Memorandum @ Python OR Seminar: Pulp
Memorandum @ Python OR Seminar: Pandas
Python memorandum
Python Memorandum 2
Python memorandum
python memorandum
python memorandum
Python memorandum
python memorandum
Python memorandum
Python pathlib memorandum
Python memorandum (algorithm)
Python memorandum [links]
Python> list> extend () or + =
Python memorandum numbering variables
Python from or import
python autotest or sniffer
Python memorandum (personal bookmark)
[Python] Iterative processing_Personal memorandum
python memorandum super basic
Effective Python Learning Memorandum Day 15 [15/100]
Cisco Memorandum _ Python config input
Python 3.4 or later standard pip
Effective Python Learning Memorandum Day 6 [6/100]
Neural network with Python (scikit-learn)
Effective Python Learning Memorandum Day 12 [12/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
ABC memorandum [ABC163 C --managementr] (Python)
About python beginner's memorandum function
[Python] SQLAlchemy error avoidance memorandum
python: Basics of using scikit-learn ①
A memorandum about correlation [Python]
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Python bitwise operator and OR
Effective Python Learning Memorandum Day 13 [13/100]
A memorandum about Python mock
Effective Python Learning Memorandum Day 3 [3/100]
[Python] Linear regression with scikit-learn
Effective Python Learning Memorandum Day 5 [5/100]
[python] Random number generation memorandum
Effective Python Learning Memorandum Day 4 [4/100]
Ruby's `` like Python. 2.6 or later
Python or and and operator trap
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
python parallel / asynchronous execution memorandum
Python pywin32 (win32com) Excel operation memorandum
[Python] A memorandum of beautiful soup4
Which is better, PyPy or Python?
python + django + scikit-learn + mecab (2) on heroku
ABC memorandum [ABC161 C --Replacing Integer] (Python)
Git & Github & python & VScode Personal memorandum
I can't install scikit-learn in Python
ABC memorandum [ABC158 C --Tax Increase] (Python)
[Python] return A [or / and] B
Memorandum of beginners Python "isdigit" movement