https://zhuanlan.zhihu.com/p/82695065
本站搜集的资料仅用于学生学习辅助,版权属于原作者、如不同意转载请联系管理员删除,谢谢。
官网:scikit-learn:Machine Learning in Python
开源(Open source),可移植(reusable),基于NumPy、SciPy、matplotlib,用于数据挖掘(data mining)和数据分析(data analysis)的库。
目录:
(1)Classification 分类
Identifying to which category an object belongs to. 应用:虚假评价检测(Spam detection),图像识别(Image recognition) 算法:SVM,最近邻法(nearest neighbors),随机森林(random forest)
(2)Regression 回归
Predicting a continuous-valued attribute associated with an object. 应用:药物反应(Drug response),股票价格(Stock prices) 算法:支持向量回归(SVR),岭回归(ridge regression),lasso回归(Lasso)
(3)Clustering 聚类
Automatic grouping of similar objects into sets. 应用:顾客区分(Customer segmentation),分组实验结果(Grouping experiment outcomes) 算法:k均值(k-means),谱聚类(spectral clustering),均值偏移(mean-shift)
(4)Dimensionality reduction 降维
Reducing the number of random variables to consider. 应用:可视化(Visualization),增加效率(Increased efficiency) 算法:PCA,特征选择(feature selection),非负矩阵分解(non-negative matrix factorization)
(5)Model selection 模型选择
Comparing, validating and choosing parameters and models. 目标:通过改变参数提高准确度 模块:grid search 网格搜索,cross validation 交叉验证,metrics
(6)Preprocessing 预处理
Feature extraction and normalization. 特征提取及归一化 应用:Transforming input data such as text for use with machine learning algorithms. 模块:preprocessing 预处理,feature extortion 特征提取
conda install scikit-learn
or
pip install -U scikit-learn
注:本人使用conda安装tf成功,但是安装sklearn失败。遂使用pip安装sklearn,成功。
附1. conda更新或删除软件
conda update scikit-learn conda remove scikit-learn
附2. 更新 pip
You are using pip version 9.0.1, however version 19.2.3 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
参考:Installing scikit-learn
(1)导入简单数据
from sklearn import datasetsiris = datasets.load_iris()digits = datasets.load_digits()print(digits.data)
[[ 0. 0. 5. ... 0. 0. 0.] [ 0. 0. 0. ... 10. 0. 0.] [ 0. 0. 0. ... 16. 9. 0.] ... [ 0. 0. 1. ... 6. 0. 0.] [ 0. 0. 2. ... 12. 0. 0.] [ 0. 0. 10. ... 12. 1. 0.]]
digits.target
array([0, 1, 2, ..., 8, 9, 8])
digits.images[0]
array([[ 0., 0., 5., 13., 9., 1., 0., 0.], [ 0., 0., 13., 15., 10., 15., 5., 0.], [ 0., 3., 15., 2., 0., 11., 8., 0.], [ 0., 4., 12., 0., 0., 8., 8., 0.], [ 0., 5., 8., 0., 0., 9., 8., 0.], [ 0., 4., 11., 0., 1., 12., 7., 0.], [ 0., 2., 14., 5., 10., 12., 0., 0.], [ 0., 0., 6., 13., 10., 0., 0., 0.]])
(2)学习和预测
from sklearn import svmclf = svm.SVC(gamma=0.001, C=100.)clf.fit(digits.data[:-1], digits.target[:-1])
SVC(C=100.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma=0.001, kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)
clf.predict(digits.data[-1:])
array([8])
(3)模型持久性
from sklearn import svmfrom sklearn import datasetsclf = svm.SVC(gamma='scale')iris = datasets.load_iris()X, y = iris.data, iris.targetclf.fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)
import pickle s = pickle.dumps(clf) clf2 = pickle.loads(s) clf2.predict(X[0:1])
array([0])
y[0]
0
from joblib import dump, load dump(clf, 'filename.joblib')
['filename.joblib']
clf = load('filename.joblib')
(4)Conventions
a. 类型转换 Type casting
import numpy as npfrom sklearn import random_projectionrng = np.random.RandomState(0)X = rng.rand(10, 2000)X = np.array(X, dtype='float32')X.dtype
dtype('float32')
transformer = random_projection.GaussianRandomProjection() X_new = transformer.fit_transform(X) X_new.dtype
dtype('float64')
以及
from sklearn import datasetsfrom sklearn.svm import SVCiris = datasets.load_iris()clf = SVC(gamma='scale')clf.fit(iris.data, iris.target)
list(clf.predict(iris.data[:3]))
[0, 0, 0]
clf.fit(iris.data, iris.target_names[iris.target])
['setosa', 'setosa', 'setosa']
b. 重拟合及更新参数
import numpy as np from sklearn.datasets import load_iris from sklearn.svm import SVC X, y = load_iris(return_X_y=True) clf = SVC() clf.set_params(kernel='linear').fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='auto_deprecated', kernel='linear', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)
clf.predict(X[:5])
array([0, 0, 0, 0, 0])
clf.set_params(kernel='rbf', gamma='scale').fit(X, y)
c. 多类别 vs 多标签 拟合分类
from sklearn.svm import SVCfrom sklearn.multiclass import OneVsRestClassifierfrom sklearn.preprocessing import LabelBinarizerX = [[1, 2], [2, 4], [4, 5], [3, 2], [3, 1]]y = [0, 0, 1, 1, 2]classif = OneVsRestClassifier(estimator=SVC(gamma='scale', random_state=0))classif.fit(X, y).predict(X)
array([0, 0, 1, 1, 2])
y = LabelBinarizer().fit_transform(y) classif.fit(X, y).predict(X)
array([[1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 0, 0]])
from sklearn.preprocessing import MultiLabelBinarizer y = [[0, 1], [0, 2], [1, 3], [0, 2, 3], [2, 4]] y = MultiLabelBinarizer().fit_transform(y) classif.fit(X, y).predict(X)
array([[1, 1, 0, 0, 0], [1, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 1, 0, 0], [1, 0, 1, 0, 0]])
参考:An introduction to machine learning with scikit-learn
版权所有:Research Centre of Nonlinear Science 邮政编码:430073 E-mail:liujie@wtu.edu.cn 备案序号:鄂ICP备15000386号 鄂公网安备 42011102000704号