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.
Support Vector Machine
#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)
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
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]])
The rest is Official Documents.
Recommended Posts