代码改变世界

Scikit-Learn工具库介绍

2016-03-27 21:20  GreyYang  阅读(1632)  评论(0)    收藏  举报

A Gentle Introduction to Scikit-Learn: A Python Machine Learning Library  原文链接

 

如果你正在寻找一个将机器学习的种种高深知识从象牙塔中带入生产环境的工具,那么Scikit-Learn将是你的不二选择。这篇文章将介绍一下Scikit-Learn工具库的使用方法,并且提供一些有帮助的资源给大家。

0x01 Scikit-Learn从何而来?

Scikit-Learn来自于Google summer of code project,是由David Cournapeau在2007年开始开发的。随后Matthieu Brucher加入,并将该项目来作为其理论研究的一部分。2010年时INRIA参与并发布了第一个release版本(v0.1 beta)。

到目前Scikit-Learn已经发展为一个由INRIA, Google, TinycluesPython Software Foundation赞助的项目,长期活跃的贡献者有30多人。

0x02 Scikit-Learn是什么?

Scikit-Learn提供了一系列的监督与非监督学习算法的Python接口,它使用了简单又友好的BSD license,鼓励学术使用或商用。

Scikit-Learn类库是基于SciPy (Scientific Python)构建的,所以在使用时必须安装SciPy,其中包含:

  • NumPy: Base n-dimensional array package
  • SciPy: Fundamental library for scientific computing
  • Matplotlib: Comprehensive 2D/3D plotting
  • IPython: Enhanced interactive console
  • Sympy: Symbolic mathematics
  • Pandas: Data structures and analysis

这些依赖的类库集一般被称为SciKits。这些类库提供的深度学习算法,就被称为Scikit-Learn。

SciKit-Learn拥有很好的兼容性和鲁棒性以供其能在生产环境中发挥良好,其同样有用很好的易用性、代码质量、协同性、详细的文档以及性能表现。

虽然Scikit-Learn提供的是Python接口,但是为了性能考虑,很多内部实现都是用了C语言类库,这使得Scikit-Learn在向量、数组、矩阵等方面速度很快。当然,在自己使用Scikit-Learn的时候,也可以在一些性能瓶颈处使用C语言。

0x03 有什么特性?

Scikit-Learn主要关注的是数据建模,对于装载、操作、汇总数据方便并不关注,对于这些特性请参考NumPy和Pandas,它们做的更好。

下面是一些Scikit-Learn提供的一些常用模块:

  • Clustering: for grouping unlabeled data such as KMeans.
  • Cross Validation: for estimating the performance of supervised models on unseen data.
  • Datasets: for test datasets and for generating datasets with specific properties for investigating model behaviour.
  • Dimensionality Reduction: for reducing the number of attributes in data for summarization, visualization and feature selection such as Principal component analysis.
  • Ensemble methods: for combining the predictions of multiple supervised models.
  • Feature extraction: for defining attributes in image and text data.
  • Feature selection: for identifying meaningful attributes from which to create supervised models.
  • Parameter Tuning: for getting the most out of supervised models.
  • Manifold Learning: For summarizing and depicting complex multi-dimensional data.
  • Supervised Models: a vast array not limited to generalized linear models, discriminate analysis, naive bayes, lazy methods, neural networks, support vector machines and decision trees.

0x04 Example:分类与回归树算法

来个例子说明下Scikit-Learn是如何工作的吧。

在这个例子中我们使用分类与回归树(CART)的决策树算法来对一组花的数据集来建模。这个示例数据集是以类库的方式组织的,并且以及加载。我们的分类器将感知数据并进行训练,最终这组数据集的分类精度及混淆矩阵将被输出。

 1 from sklearn import datasets
 2 from sklearn import metrics
 3 from sklearn.tree import DecisionTreeClassifier
 4 
 5 # load the iris datasets
 6 dataset = datasets.load_iris()
 7 # fit a CART model to the data
 8 model = DecisionTreeClassifier()
 9 model.fit(dataset.data, dataset.target)
10 print(model)
11 # make predictions
12 expected = dataset.target
13 predicted = model.predict(dataset.data)
14 # summarize the fit of the model
15 print(metrics.classification_report(expected, predicted))
16 print(metrics.confusion_matrix(expected, predicted))

 

(请参考https://gist.github.com/jbrownlee/10204387#file-gistfile1-py

很简单吧?

0x05 谁在使用Scikit-Learn?

scikit-learn testimonials page 列举了 Inria, Mendeley, wise.io , Evernote, Telecom ParisTech 和 AWeber 都是该类库的使用者. 当然这很可能只是一小部分有名的公司被列举出来了,实际的使用者一定更多。

Scikit-Learn有全面覆盖的测试用例和有序的版本计划,非常适合用于开发实际产品或项目。

0x06 一些资源

如果你想获取更多的内容, 看下Scikit-Learn homepage网站上包含的一些资源. 你可以从github repository下载源码。

文档

我认为从你感兴趣的一些算法开始学习可能更好,阅读快速入门文档吧,还有别忘记,API说明本身也是很好的入门材料。

论文

如果你对这个类库背后的知识很感兴趣,那么请Check-out下面的文章来阅读吧。

书籍

如果你在寻找一本书籍来入门, 我推荐“Building Machine Learning Systems with Python”. 这本书的例子生动有趣.

Scikit-Learn Resoure Kit (免费)

Scikit-Learn Resource Kit

下载路径

准备走捷径?

Checkout 我的 Jump-Start Scikit-Learn Playbook 吧。

在里面你会发现35可以直接拷贝至你项目中的独立代码片段哦。