Machine learning에서 가장 많이 쓰이는 library이다.
Homepage : https://scikit-learn.org/stable/user_guide.html
Manual : https://scikit-learn.org/stable/_downloads/scikit-learn-docs.pdf
사용할 기능
Classification
- SVM : https://scikit-learn.org/stable/modules/svm.html
- Ensemble (Random forest) : https://scikit-learn.org/stable/modules/ensemble.html
SVM:
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn import svm # Support Vector Machine
X = [[0, 0], [2, 2]]
y = [0.5, 2.5]
regr = svm.SVR()
regr.fit(X, y) # SVR()
regr.predict([[1, 1]]) # array([1.5])
https://scikit-learn.org/stable/modules/svm.html
from sklearn.svm import SVC # Support Vector Classification
X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
y = np.array([1, 1, 2, 2])
clf = make_pipeline(StandardScaler(), SVC(gamma='auto'))
clf.fit(X, y) # Pipeline(steps=[('standardscaler', StandardScaler()),('svc', SVC(gamma='auto'))])
print(clf.predict([[-0.8, -1]])) # [1]
https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC
from sklearn.svm import SVR # Support Vector Regression
n_samples, n_features = 10, 5
rng = np.random.RandomState(0)
y = rng.randn(n_samples)
X = rng.randn(n_samples, n_features)
regr = make_pipeline(StandardScaler(), SVR(C=1.0, epsilon=0.2))
regr.fit(X, y) # Pipeline(steps=[('standardscaler', StandardScaler()),('svr', SVR(epsilon=0.2))])
https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html#sklearn.svm.SVR
Random forest:
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(random_state=0)
X = [[ 1, 2, 3], # 2 samples, 3 features
[11, 12, 13]]
y = [0, 1] # classes of each sample
clf.fit(X, y)
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=1000, n_features=4,
n_informative=2, n_redundant=0,
random_state=0, shuffle=False)
clf = RandomForestClassifier(max_depth=2, random_state=0)
clf.fit(X, y)
print(clf.predict([[0, 0, 0, 0]])) # [1]
'Library' 카테고리의 다른 글
numpy - ravel_multi_index (0) | 2021.12.21 |
---|---|
sklearn - template (0) | 2021.07.01 |
sklearn - Scaler (0) | 2021.06.23 |
Scikit-allel (0) | 2020.11.06 |
sklearn - Standardization (0) | 2020.11.05 |
댓글