这是一篇翻译的博客,原文链接在这里。这是我看的为数不多的介绍scikit-learn简介而全面的文章,特别适合入门。我这里把这篇文章翻译一下,英语好的同学可以直接看原文。

大部分喜欢用Python来学习数据科学的人,应该听过scikit-learn,这个开源的Python库帮我们实现了一系列有关机器学习,数据处理,交叉验证和可视化的算法。其提供的接口非常好用。

这就是为什么DataCamp(原网站)要为那些已经开始学习Python库却没有一个简明且方便的总结的人提供这个总结。(原文是cheat sheet,翻译过来就是小抄,我这里翻译成总结,感觉意思上更积极点)。或者你压根都不知道scikit-learn如何使用,那这份总结将会帮助你快速的了解其相关的基本知识,让你快速上手。

你会发现,当你处理机器学习问题时,scikit-learn简直就是神器。

这份scikit-learn总结将会介绍一些基本步骤让你快速实现机器学习算法,主要包括:读取数据,数据预处理,如何创建模型来拟合数据,如何验证你的模型以及如何调参让模型变得更好。

总的来说,这份总结将会通过示例代码让你开始你的数据科学项目,你能立刻创建模型,验证模型,调试模型。(原文提供了pdf版的下载,内容和原文差不多)

A 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)

(补充,这里看不懂不要紧,其实就是个小例子,后面会详细解答)

Loading The Data

你的数据需要是numeric类型,然后存储成numpy数组或者scipy稀疏矩阵。我们也接受其他能转换成numeric数组的类型,比如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

Preprocessing The Data

Standardization

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

Normalization

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

Binarization

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

Encoding Categorical Features

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

Imputing Missing Values

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

Generating Polynomial Features

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

Training And Test Data

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

Create Your Model

Supervised Learning Estimators

Linear Regression

>>> from sklearn.linear_model import LinearRegression)
>>> lr = LinearRegression(normalize=True))

Support Vector Machines (SVM)

>>> 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))

Unsupervised Learning Estimators

Principal Component Analysis (PCA)

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

K Means

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

Model Fitting

Supervised learning

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

Unsupervised Learning

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

Prediction

Supervised Estimators

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

Unsupervised Estimators

>>> y_pred = k_means.predict(X_test))

Evaluate Your Model's Performance

Classification Metrics

Accuracy Score

>>> 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)))

Regression Metrics

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))

Clustering Metrics

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))

Cross-Validation

>>> print(cross_val_score(knn, X_train, y_train, cv=4))
>>> print(cross_val_score(lr, X, y, cv=2))

Tune Your Model

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_)

Going Further

学习完上面的例子后,你可以通过our scikit-learn tutorial for beginners来学习更多的例子。另外你可以学习matplotlib来可视化数据。

不要错过后续教程 Bokeh cheat sheet, the Pandas cheat sheet or the Python cheat sheet for data science.

Python Machine Learning: Scikit-Learn Tutorial的更多相关文章

  1. Python机器学习 (Python Machine Learning 中文版 PDF)

    Python机器学习介绍(Python Machine Learning 中文版) 机器学习,如今最令人振奋的计算机领域之一.看看那些大公司,Google.Facebook.Apple.Amazon早 ...

  2. [Python & Machine Learning] 学习笔记之scikit-learn机器学习库

    1. scikit-learn介绍 scikit-learn是Python的一个开源机器学习模块,它建立在NumPy,SciPy和matplotlib模块之上.值得一提的是,scikit-learn最 ...

  3. Python -- machine learning, neural network -- PyBrain 机器学习 神经网络

    I am using pybrain on my Linuxmint 13 x86_64 PC. As what it is described: PyBrain is a modular Machi ...

  4. Python机器学习介绍(Python Machine Learning 中文版)

    Python机器学习 机器学习,如今最令人振奋的计算机领域之一.看看那些大公司,Google.Facebook.Apple.Amazon早已展开了一场关于机器学习的军备竞赛.从手机上的语音助手.垃圾邮 ...

  5. 《Python Machine Learning》索引

    目录部分: 第一章:赋予计算机从数据中学习的能力 第二章:训练简单的机器学习算法——分类 第三章:使用sklearn训练机器学习分类器 第四章:建立好的训练集——数据预处理 第五章:通过降维压缩数据 ...

  6. How do I learn machine learning?

    https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644   How Can I Learn X? ...

  7. Getting started with machine learning in Python

    Getting started with machine learning in Python Machine learning is a field that uses algorithms to ...

  8. 【机器学习Machine Learning】资料大全

    昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...

  9. 机器学习算法之旅A Tour of Machine Learning Algorithms

    In this post we take a tour of the most popular machine learning algorithms. It is useful to tour th ...

随机推荐

  1. 铁乐学python_day22_面向对象编程4

    以下内容大部分摘自博客http://www.cnblogs.com/Eva-J/ 封装 [封装]隐藏对象的属性和实现细节,仅对外提供公共访问方式. [好处] 将变化隔离: 便于使用: 提高复用性: 提 ...

  2. Subversion、TortoiseSVN、Ankhsvn+VS使用

    Subversion为版本控制软件的服务器端.VisualSVN Server 可以在Windows平台安装和管理全功能的Subversion server. TortoiseSVN为Subversi ...

  3. PHP设计模式系列 - 单例

    单例模式 通过提供自身共享实例的访问,单例设计模式用于限制特定对象只能被创建一次. 使用场景 例如数据库实例,一般都会走单例模式. 单例模式可以减少类的实例化 代码:来源InitPHP框架,先检测类有 ...

  4. 如何确定PHP CLI 的php.ini文件的位置

    当我们安装扩展时,可能需要手动配置php.ini文件,把扩展加进去,所以要确认PHP CLI的php.ini文件的位置.可以运行php --ini查找PHP CLI的ini文件位置,结果类似如下(各个 ...

  5. redis-Sentinel配置

    Sentinel介绍 Redis的主从模式下,主节点一旦发生故障不能提供服务,需要人 工干预,将从节点晋升为主节点,同时还需要修改客户端配置. 对于很多应用场景这种方式无法接受. Redis从 2.8 ...

  6. BZOJ1188:[HNOI2007]分裂游戏(博弈论)

    Description 聪聪和睿睿最近迷上了一款叫做分裂的游戏.该游戏的规则试:共有n个瓶子,标号为0,1,2.....n-1,第i个瓶子中装有p[i]颗巧克力豆,两个人轮流取豆子,每一轮每人选择3个 ...

  7. CF893F:Subtree Minimum Query(线段树合并)

    Description 给你一颗有根树,点有权值,m次询问,每次问你某个点的子树中距离其不超过k的点的权值的最小值.(边权均为1,点权有可能重复,k值每次询问有可能不同,强制在线) Input 第一行 ...

  8. Python全栈开发:list 、tuple以及dict的总结

    总结: 列表:增:append(),inset(),extend() 删:pop(),remove(),clear(),del 改:a.通过指定元素和切片重新赋值.b.可以使用repelace替换列表 ...

  9. [AHOI2005]矿藏编码

    嘟嘟嘟 这道题题面我是看了小半天才懂(太菜了),然后就发现好水啊. 只要维护一个栈,存的是t,代表当前的正方形是2t * 2t的,然后从头开始扫序列,如果遇到2,就把栈顶元素取出来,然后放进去四个t ...

  10. 记一次爬虫经历(友话APP的Web端)

    背景:学校为迎接新生举办了一个活动,在友话APP的校园圈子内发布动态即可参与活动,最终抽取数名同学赠送福利. 分析:动态的数量会随着迎新的开始逐渐增加,人工统计显然不现实,因此可以使用爬虫脚本在友话A ...