现在最常用的数据分析的编程语言为R和Python。每种语言都有自己的特点,Python因为Scikit-Learn库赢得了优势。Scikit-Learn有完整的文档,并实现很多机器学习算法,而每种算法使用的接口几乎相同,可以非常快的测试其它学习算法。

Pandas一般和Scikit-Learn配合使用,它是基于Numpy构建的含有更高级数据结构和工具的数据统计工具,可以把它当成excel。

加载数据

首先把数据加载到内存。下载UCI数据集:

 
1
2
3
4
5
6
7
8
9
10
11
import numpy as np
import urllib
# 数据集的url
url = "http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data"
# 下载文件
raw_data = urllib.urlopen(url)
# 把数据加载到numpy matrix
dataset = np.loadtxt(raw_data, delimiter=",")
# 分离数据集
X = dataset[:,0:7]  # 属性集
y = dataset[:,8]    # 标签

数据标准化

在开始应用学习算法之前,应首先对数据执行标准化,这是为了确保特征值的范围在0-1。对数据进行预处理:

 
1
2
3
4
5
from sklearn import preprocessing
# normalize
normalized_X = preprocessing.normalize(X)
# standardize
standardized_X = preprocessing.scale(X)

分类

ExtraTreesClassifier(基于树):

 
1
2
3
4
5
6
from sklearn import metrics
from sklearn.ensemble import ExtraTreesClassifier
model = ExtraTreesClassifier()
model.fit(X, y)
# 显示属性的相对重要性
print(model.feature_importances_)

LogisticRegression:

 
1
2
3
4
5
6
7
8
9
from sklearn.feature_selection import RFE
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
 
rfe = RFE(model, 3)
rfe = rfe.fit(X, y)
 
print(rfe.support_)
print(rfe.ranking_)

机器学习算法

Logistic regression

通常用来解决分类问题(binary),但是也支持多个分类。这个算法会给出属于某一分类的概率:

 
1
2
3
4
5
6
7
8
9
10
11
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X, y)
print(model)
# 做预测
expected = y
predicted = model.predict(X)
# 输出
print(metrics.classification_report(expected, predicted))
print(metrics.confusion_matrix(expected, predicted))

朴素贝叶斯-Naive Bayes

这也是广为人知的机器学习算法,用来学习数据分布的密度,在多分类问题中可以提供高质量的预测结果。

 
1
2
3
4
5
6
7
8
9
10
11
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB
model = GaussianNB()
model.fit(X, y)
print(model)
# 预测
expected = y
predicted = model.predict(X)
# 结果
print(metrics.classification_report(expected, predicted))
print(metrics.confusion_matrix(expected, predicted))

KNN算法(K-Nearest Neighbours)

它通常用在更复杂分类算法的一部分,它在回归问题中可以提供很好的结果。

决策树-Decision Trees

能很好的处理回归和分类问题。

 
1
2
3
4
5
6
7
8
9
10
11
12
from sklearn import metrics
from sklearn.tree import DecisionTreeClassifier
# fit a CART model to the data
model = DecisionTreeClassifier()
model.fit(X, y)
print(model)
# 预测
expected = y
predicted = model.predict(X)
# 结果
print(metrics.classification_report(expected, predicted))
print(metrics.confusion_matrix(expected, predicted))

支持向量机-Support Vector Machines

 
1
2
3
4
5
6
7
8
9
10
11
12
from sklearn import metrics
from sklearn.svm import SVC
# fit a SVM model to the data
model = SVC()
model.fit(X, y)
print(model)
# 预测
expected = y
predicted = model.predict(X)
# 结果
print(metrics.classification_report(expected, predicted))
print(metrics.confusion_matrix(expected, predicted))

Scikit-Learn还提供了一堆更复杂的算法,包括clustering,Bagging 和 Boosting。

Scikit-Learn机器学习入门的更多相关文章

  1. Scikit Learn: 在python中机器学习

    转自:http://my.oschina.net/u/175377/blog/84420#OSC_h2_23 Scikit Learn: 在python中机器学习 Warning 警告:有些没能理解的 ...

  2. (原创)(三)机器学习笔记之Scikit Learn的线性回归模型初探

    一.Scikit Learn中使用estimator三部曲 1. 构造estimator 2. 训练模型:fit 3. 利用模型进行预测:predict 二.模型评价 模型训练好后,度量模型拟合效果的 ...

  3. (原创)(四)机器学习笔记之Scikit Learn的Logistic回归初探

    目录 5.3 使用LogisticRegressionCV进行正则化的 Logistic Regression 参数调优 一.Scikit Learn中有关logistics回归函数的介绍 1. 交叉 ...

  4. pyhton机器学习入门基础(机器学习与决策树)

    //2019.07.26#scikit-learn数据挖掘工具包1.Scikit learn是基于python的数据挖掘和机器学习的工具包,方便实现数据的数据分析与高级操作,是数据分析里面非常重要的工 ...

  5. [转]MNIST机器学习入门

    MNIST机器学习入门 转自:http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html?plg_ ...

  6. scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类 (python代码)

    scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类数据集 fetch_20newsgroups #-*- coding: UTF-8 -*- import ...

  7. Azure机器学习入门(三)创建Azure机器学习实验

    在此动手实践中,我们将在Azure机器学习Studio中一步步地开发预测分析模型,首先我们从UCI机器学习库的链接下载普查收入数据集的样本并开始动手实践: http://archive.ics.uci ...

  8. 机器学习入门 - Google机器学习速成课程 - 笔记汇总

    机器学习入门 - Google机器学习速成课程 https://www.cnblogs.com/anliven/p/6107783.html MLCC简介 前提条件和准备工作 完成课程的下一步 机器学 ...

  9. web安全之机器学习入门——3.1 KNN/k近邻

    目录 sklearn.neighbors.NearestNeighbors 参数/方法 基础用法 用于监督学习 检测异常操作(一) 检测异常操作(二) 检测rootkit 检测webshell skl ...

  10. Scikit Learn

    Scikit Learn Scikit-Learn简称sklearn,基于 Python 语言的,简单高效的数据挖掘和数据分析工具,建立在 NumPy,SciPy 和 matplotlib 上.

随机推荐

  1. 关于springMVC的一些常用注解

    ①:@RequestMapping("/helloworld").@RequestMapping(value="/emp", method=RequestMet ...

  2. Script" References MACLEAN‘s post Speed ​​up the index creation.

    alter session set workarea_size_policy=MANUAL; alter session set db_file_multiblock_read_count=512; ...

  3. Oracle特殊恢复原理与实战(DSI系列)

    1.深入浅出Oracle(DSI系列Ⅰ) 2.Oracle特殊恢复原理与实战(DSI系列Ⅱ) 3.Oracle SQL Tuning(DSI系列Ⅲ)即将开设 4.Oracle DB Performan ...

  4. AFNetworking2.0简易GET,POST请求封装以及使用

    AFNetworking2.0简易GET,POST请求封装以及使用 AFNetworking不用我赘述其强大性,本人仅仅做了非常简易的封装,解决了有时候请求出错的问题,提供源码给大家. 封装源码库下载 ...

  5. Oracle数据库运维:要对监听日志文件(listener.log)进行定期清理,如果不定期清理,会遇到下面一些麻烦

    原文链接: http://www.lookdaima.com/WebForms/WebPages/Blanks/Pm/Docs/DocItemDetail.aspx?EmPreviewTypeV=2& ...

  6. mysql创建表的注意事项

    1 库名,表名,字段名必须使用小写字母,"_"分割. 2 库名,表名,字段名必须不超过12个字符. 3 库名,表名,字段名见名识意,建议使用名词而不是动词. 4 建议使用InnoD ...

  7. 设计模式之——单例模式(Singleton)的常见应用场景(转):

    单例模式(Singleton)也叫单态模式,是设计模式中最为简单的一种模式,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象,也因此 ...

  8. 【原创】修改最大用户进程限制 "ulimit -u"

    centos 6.x 内核版本2.6.32以上,修改/etc/security/limits.d/90-nproc.conf:因为系统是先读/etc/security/limits.conf的值,在用 ...

  9. HTML头标签使用-又一次定向,refresh

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  10. Codeforces 1106 E. Lunar New Year and Red Envelopes 优先队列+dp

    题意大致是Bob新年拿红包,每个红包可以在s-t时间内取,但是取了之后得在d+1时间开始才能继续取红包. 同时他女儿能在m个时间点阻止他取红包,求女儿阻止后Bob取得的w总和最小值. Bob取红包的策 ...