Scikit-Learn 备忘录

Scikit-Learn 备忘录整理自Scikit_Learn_Cheat_Sheet_Python,归属于笔者的程序猿的数据科学与机器学习实战手册,前置阅读 Python语法速览与机器学习开发环境搭建

Scikit-Learn

Scikit-learn是开源的Python机器学习库,提供了数据预处理、交叉验证、算法与可视化算法等一系列接口。

Basic Example:基本用例

>>> from sklearn import neighbors, datasets, preprocessing
>>> from sklearn.cross_validation import train_test_split
>>> from sklearn.metrics import accuracy_score
>>> iris = datasets.load_iris()
>>> X, y = iris.data[:, :2], iris.target
>>> X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33)
>>> scaler = preprocessing.StandardScaler().fit(X_train)
>>> X_train = scaler.transform(X_train)
>>> X_test = scaler.transform(X_test)
>>> knn = neighbors.KNeighborsClassifier(n_neighbors=5)
>>> knn.fit(X_train, y_train)
>>> y_pred = knn.predict(X_test)
>>> accuracy_score(y_test, y_pred)

数据加载与切分

我们一般使用NumPy中的数组或者Pandas中的DataFrame等数据结构来存放数据:

>>> import numpy as np
>>> X = np.random.random((10,5))
>>> y = np.array(['M','M','F','F','M','F','M','M','F','F','F'])
>>> X[X < 0.7] = 0

NumPy还提供了方便的接口帮我们划分训练数据与测试数据:

>>> from sklearn.cross_validation import train_test_split
>>> X_train, X_test, y_train, y_test = train_test_split(X,
 y, random_state=0)

Model:模型

模型创建

监督学习

  • Linear Regression

>>> from sklearn.linear_model import LinearRegression
>>> lr = LinearRegression(normalize=True)
  • Support Vector Machines

>>> from sklearn.svm import SVC
>>> svc = SVC(kernel='linear')
  • Naive Bayes

>>> from sklearn.naive_bayes import GaussianNB
>>> gnb = GaussianNB()
  • KNN

>>> from sklearn import neighbors
>>> knn = neighbors.KNeighborsClassifier(n_neighbors=5)

无监督学习

  • Principal Component Analysis

>>> from sklearn.decomposition import PCA
>>> pca = PCA(n_components=0.95)
  • KMeans

>>> from sklearn.cluster import KMeans
>>> k_means = KMeans(n_clusters=3, random_state=0)

模型拟合

有监督学习

>>> lr.fit(X, y)
>>> knn.fit(X_train, y_train)
>>> svc.fit(X_train, y_train)

无监督学习

>>> k_means.fit(X_train)
>>> pca_model = pca.fit_transform(X_train)

模型预测

有监督预测

>>> y_pred = svc.predict(np.random.random((2,5)))
>>> y_pred = lr.predict(X_test)
>>> y_pred = knn.predict_proba(X_test)

无监督预测

>>> y_pred = k_means.predict(X_test)

模型评估

分类度量

  • Accuracy Scope

>>> knn.score(X_test, y_test)
>>> from sklearn.metrics import accuracy_score
>>> accuracy_score(y_test, y_pred)
  • Classification Report

>>> from sklearn.metrics import classification_report 
>>> print(classification_report(y_test, y_pred))
  • Confusion Matrix

>>> from sklearn.metrics import confusion_matrix 
>>> print(confusion_matrix(y_test, y_pred))

回归度量

  • Mean Absolute Error

>>> from sklearn.metrics import mean_absolute_error
>>> y_true = [3, -0.5, 2]
>>> mean_absolute_error(y_true, y_pred)
  • Mean Squared Error

>>> from sklearn.metrics import mean_squared_error
>>> mean_squared_error(y_test, y_pred)
  • R2 Score

>>> from sklearn.metrics import r2_score
>>> r2_score(y_true, y_pred)

聚类度量

  • Adjusted Rand Index

>>> from sklearn.metrics import adjusted_rand_score
>>> adjusted_rand_score(y_true, y_pred)
  • Homogeneity

>>> from sklearn.metrics import homogeneity_score
>>> homogeneity_score(y_true, y_pred)
  • V-measure

>>> from sklearn.metrics import v_measure_score
>>> metrics.v_measure_score(y_true, y_pred)

交叉验证

>>> from sklearn.cross_validation import cross_val_score
>>> print(cross_val_score(knn, X_train, y_train, cv=4))
>>> print(cross_val_score(lr, X, y, cv=2))

数据预处理

标准化

>>> from sklearn.preprocessing import StandardScaler
>>> scaler = StandardScaler().fit(X_train)
>>> standardized_X = scaler.transform(X_train)
>>> standardized_X_test = scaler.transform(X_test)

归一化

>>> from sklearn.preprocessing import Normalizer
>>> scaler = Normalizer().fit(X_train)
>>> normalized_X = scaler.transform(X_train)
>>> normalized_X_test = scaler.transform(X_test)

二值化

>>> from sklearn.preprocessing import Binarizer
>>> binarizer = Binarizer(threshold=0.0).fit(X)
>>> binary_X = binarizer.transform(X)

类条件编码

>>> from sklearn.preprocessing import LabelEncoder
>>> enc = LabelEncoder()
>>> y = enc.fit_transform(y)

缺失值推导

>>> from sklearn.preprocessing import Imputer
>>> imp = Imputer(missing_values=0, strategy='mean', axis=0)
>>> imp.fit_transform(X_train)

多项式属性生成

>>> from sklearn.preprocessing import PolynomialFeatures
>>> poly = PolynomialFeatures(5)
>>> poly.fit_transform(X)

模型调优

Grid Search

>>> from sklearn.grid_search import GridSearchCV
>>> params = {"n_neighbors": np.arange(1,3), "metric": ["euclidean", "cityblock"]}
>>> grid = GridSearchCV(estimator=knn,
 param_grid=params)
>>> grid.fit(X_train, y_train)
>>> print(grid.best_score_)
>>> print(grid.best_estimator_.n_neighbors)

Randomized Parameter Optimization

>>> from sklearn.grid_search import RandomizedSearchCV
>>> params = {"n_neighbors": range(1,5), "weights": ["uniform", "distance"]}
>>> rsearch = RandomizedSearchCV(estimator=knn,
 param_distributions=params,
 cv=4,
 n_iter=8,
 random_state=5)
>>> rsearch.fit(X_train, y_train)
>>> print(rsearch.best_score_)

相关推荐