主要以翻译为主:
内容:介绍相关词汇,并给出例子
Machine learning: the problem setting
通常用n个数据样本,试图预测未知样本的特性。每个样本包含多个数据,则含有多个属性或特征、
可以把机器学习分为几个大类:
有监督学习:数据包含我们想预测的属性(链接跳转到scikit-learn监督学习页),可继续分为:分类(一个例子是手写数字识别示例)和回归(一个例子是预测鲑鱼的长度与它的年龄和体重的关系。)。
无监督学习:训练数据不包含目标值。包含聚类,密度估计,为了可视化将高维数据降为二到三维。
训练数据集和测试数据集
Loading an example dataset
scikit学习有一些标准的数据集,例如虹膜和数字分类和波士顿房价数据回归数据集。
接下来,从shell启动一个Python解释器,然后加载虹膜和数字数据集。惯例是,美元符号$表示shell提示符,但> > > 表示Python解释器的提示:
$ python
>>> from sklearn import datasets
>>> iris = datasets.load_iris()
>>> digits = datasets.load_digits()
数据集是一个类似字典的对象,它保存所有数据和一些关于数据的元数据。该数据存储在.data 成员,这是一个n_samples,n_features矩阵。在有监督问题的情况下,一个或多个响应变量存储在.target 成员中。关于不同数据集的更多细节可以在专用的章节中找到。
例如,在digits数据集的情况下,digits.data获得可用于数字样本进行分类的特征:
>>> 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])
Shape of the data arrays
data总是2维的,(n_samples,n_features),即便原始数据可能不是这个结构。在digits例子中,每个原始样本是大小为(8*8)的image图片
>>> 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.]])
所以转化数据后可以使用scikit-learn。
Loading from external datasets
numpy 数组或者scipy 稀疏矩阵都可以,还可以用Pandas数据框,
把标准列数据转为scikit-learn可使用的格式的建议:
pandas.io提供工具来读取数据从常见的格式包括CSV、Excel、JSON和SQL。数据框也可以由元组或字典列表构建。
loading external datasets.
Learning and predicting
在数字数据集的情况下,任务是预测给定的图像,它代表哪个数字。
我们给出了10种可能的类(九到0的数字)的样本,我们在其中fit拟合一个estimator,可以预测未知样本所属的类。
estimator是一个Python对象,运行fit(X,y)和predict(T)方法。
sklearn.svm.SVC类就是estimator的一个例子,它实现支持向量机分类方法。estimator构建需要模型参数,但目前仍把它当做黑盒子。
>>> from sklearn import svm
>>> clf = svm.SVC(gamma=0.001, C=100.)
Choosing the parameters of the model
在这个例子中,我们手动设定的gamma值。可以通过使用网格搜索和交叉验证等工具,自动找到参数的良好值。
把estimator实例命名为clf,它是个分类器。现在拟合模型,即模型学习。把训练数据集放入fit方法。训练数据集选择除最后一个image的数据,即【:,-1】,生成一个不包含最后一个image的新的矩阵。digits.data
>>> 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)
现在可以预测新的数据,可用digits最后一个image进行预测,因为没有用它进行训练分类器。
>>> clf.predict(digits.data[-1:])
array([8])
相应的图像是这样的:可以看到图像分辨率很低,
完整可以运行学习的例子在这里:Recognizing hand-written digits.
Model persistence
模型持久化保持模型,即pickle
>>> from sklearn import svm
>>> from sklearn import datasets
>>> clf = svm.SVC()
>>> iris = datasets.load_iris()
>>> X, y = iris.data, iris.target
>>> clf.fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', 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
在大数据时可用joblib's代替pickle(joblib.dump和joblib.load),但是只能pickle到disk,and not to a string
>>> from sklearn.externals import joblib
>>> joblib.dump(clf, 'filename.pkl')
接下来可以在其他Python程序中加载持久化的模型
>>> clf = joblib.load('filename.pkl')
注意joblib.dump和joblib.load也可以接受file-like对象,更多查阅
here
.
注意pickle有一些安全和维护性问题,更多查阅模型持久化Model persistence
Conventions
一些规则
Type casting
除非特别说明,输入将被转为float64
>>> import numpy as np
>>> from sklearn import random_projection
>>> rng = 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')
在这个例子中,X是float32,通过fit_transform(X)转为float64
回归的target被转为float64,而分类的targets保持不变:
>>> from sklearn import datasets
>>> from sklearn.svm import SVC
>>> iris = datasets.load_iris()
>>> clf = SVC()
>>> clf.fit(iris.data, iris.target)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
>>> list(clf.predict(iris.data[:3]))
[0, 0, 0]
>>> clf.fit(iris.data, iris.target_names[iris.target])
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
>>> list(clf.predict(iris.data[:3]))
['setosa', 'setosa', 'setosa']
这里, the first
predict()
returns an integer array, since
iris.target
(an integer array) was used in
fit
. The second
predict()
returns a string array, since
iris.target_names
was for fitting.
Refitting and updating parameters
estimator的超参数在模型构建之后可以通过sklearn.pipeline.Pipeline.set_params
方法更改。fit()多次调用后的结果会覆盖掉之前fit()学习到的结果。
>>> import numpy as np
>>> from sklearn.svm import SVC
>>> rng = np.random.RandomState(0)
>>> X = rng.rand(100, 10)
>>> y = rng.binomial(1, 0.5, 100)
>>> X_test = rng.rand(5, 10)
>>> 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', kernel='linear',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
>>> clf.predict(X_test)
array([1, 0, 1, 1, 0])
>>> clf.set_params(kernel='rbf').fit(X, y)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
>>> clf.predict(X_test)
array([0, 0, 0, 1, 0])
在这里,默认的核函数rbf第一次被改为linear,在用SVC()构建estimator之后。并且改回为rbf,重新拟合estimator,形成第二个预测器。
Multiclass vs. multilabel fitting
使用multiclass classifiers时,依赖target data 的格式
>>> from sklearn.svm import SVC
>>> from sklearn.multiclass import OneVsRestClassifier
>>> from sklearn.preprocessing import LabelBinarizer
>>> X = [[1, 2], [2, 4], [4, 5], [3, 2], [3, 1]]
>>> y = [0, 0, 1, 1, 2]
>>> classif = OneVsRestClassifier(estimator=SVC(random_state=0))
>>> classif.fit(X, y).predict(X)
array([0, 0, 1, 1, 2])
上面的例子中,分类器在多分类标签的1维数组上拟合,predict()方法提供相应的多分类预测。也可以在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]])
这里,分类器fit()在代表y的2维二进制标签上,采用LabelBinarizer。predict()返回二维数组,代表相应的多标签预测。
请注意,第四和第五的返回的全是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, 1, 0],
[0, 0, 1, 0, 1]])
这个例子中,分类器拟合的实例,每个匹配多个标签。 MultiLabelBinarizer,用来二值化2维的多标签,然后进行拟合。因此,predict()为每个实例返回2维的多预测标签。