现在最常用的数据分析的编程语言为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. 解决由于显卡驱动BUG导致桌面右键卡顿的问题:bat文件源码

    @ ECHO OFF%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe&q ...

  2. 电脑技巧——DOS和windows的区别?

    本质:都是微软公司的操作系统,某种从程度上说windows是dos的后续操作系统版本.只是windows相比dos有质的飞跃.dos只支持命令操作,windows则有了良好的图形操作界面,window ...

  3. EntityFramework Code First便捷工具——数据迁移

    使用EntityFramework Code First开发,数据迁移是一个不得不提的技术. 在我们的开发过程中,难免需要对模型进行改进,模型改进后,会导致实体集与数据库不一致,当然我们可以通过删除数 ...

  4. Sql Server 流程控制语句

    T-SQL中用来编写流程控制模块的语句有:BEGIN...AND语句.IF...ELSE语句.CASE语句.WHILE语句.GOTO语句.BREAK语句.WAITFOR语句和RETURN语句. 批处理 ...

  5. Java Math类学习

    1.  java.lang.Math Math类其成员皆为静态成员(static),无需创建对象,直接用类名Math作为前缀使用它们即可. 2.  Math类有两个静态常量:E(自然对数)和PI(圆周 ...

  6. AOP的核心:代理与织入

    分为两步: 1.动态生成代理类: 2.织入: 2.6 织入(Weaving) 织入是将增强添加到目标的具体连接点上的过程 . AOP 织入方式: 方式 实现 应用编译期织入 特殊的 Java 编译器. ...

  7. C# 数字证书 RSA加密解密 加签验签

    KeyValuePair<string, string> keyPair = Encrypter.CreateRSAKey(); string privateKey = keyPair.V ...

  8. Spring源码分析(二十)准备环境

    摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. prepareRefresh函数主要是做些准备工作,例如对系统属性及环 ...

  9. 从CMDB查询云平台组件或者IP简单脚本

    #!/bin/bash#author xiaoweige#todo: ip -- > ingredient or ingredient -- > ip #todo: get the ip ...

  10. 谈谈我的js学习过程(二)——“Hello World!”

    在<谈谈我的js学习过程(一)>中,我简单聊了一下我认为的javascript的学习方法,接下来我们可以尝试来写一个最简单的js代码. "Hello World!"对于 ...