Scikit-Learn机器学习入门
现在最常用的数据分析的编程语言为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机器学习入门的更多相关文章
- Scikit Learn: 在python中机器学习
转自:http://my.oschina.net/u/175377/blog/84420#OSC_h2_23 Scikit Learn: 在python中机器学习 Warning 警告:有些没能理解的 ...
- (原创)(三)机器学习笔记之Scikit Learn的线性回归模型初探
一.Scikit Learn中使用estimator三部曲 1. 构造estimator 2. 训练模型:fit 3. 利用模型进行预测:predict 二.模型评价 模型训练好后,度量模型拟合效果的 ...
- (原创)(四)机器学习笔记之Scikit Learn的Logistic回归初探
目录 5.3 使用LogisticRegressionCV进行正则化的 Logistic Regression 参数调优 一.Scikit Learn中有关logistics回归函数的介绍 1. 交叉 ...
- pyhton机器学习入门基础(机器学习与决策树)
//2019.07.26#scikit-learn数据挖掘工具包1.Scikit learn是基于python的数据挖掘和机器学习的工具包,方便实现数据的数据分析与高级操作,是数据分析里面非常重要的工 ...
- [转]MNIST机器学习入门
MNIST机器学习入门 转自:http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html?plg_ ...
- scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类 (python代码)
scikit learn 模块 调参 pipeline+girdsearch 数据举例:文档分类数据集 fetch_20newsgroups #-*- coding: UTF-8 -*- import ...
- Azure机器学习入门(三)创建Azure机器学习实验
在此动手实践中,我们将在Azure机器学习Studio中一步步地开发预测分析模型,首先我们从UCI机器学习库的链接下载普查收入数据集的样本并开始动手实践: http://archive.ics.uci ...
- 机器学习入门 - Google机器学习速成课程 - 笔记汇总
机器学习入门 - Google机器学习速成课程 https://www.cnblogs.com/anliven/p/6107783.html MLCC简介 前提条件和准备工作 完成课程的下一步 机器学 ...
- web安全之机器学习入门——3.1 KNN/k近邻
目录 sklearn.neighbors.NearestNeighbors 参数/方法 基础用法 用于监督学习 检测异常操作(一) 检测异常操作(二) 检测rootkit 检测webshell skl ...
- Scikit Learn
Scikit Learn Scikit-Learn简称sklearn,基于 Python 语言的,简单高效的数据挖掘和数据分析工具,建立在 NumPy,SciPy 和 matplotlib 上.
随机推荐
- 引用js文件
在子模板里引用js文件的时候,需要把相应的.js文件放到static目录下,如引用static/jQuery/index.js文件: {% extends "base.html" ...
- 铁乐学python_day09_作业
练习题 1.整理函数相关知识点,写博客 2.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素, 并将其作为新列表返回给调用者. def odd_index(l): lis = [] for ...
- 题解 P1018 【乘积最大】
题目链接:P1018 乘积最大 题面 今年是国际数学联盟确定的"2000――世界数学年",又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面 ...
- 【Alpha】Daily Scrum Meeting——blog1
团队成员 吴吉键 201421122007(组长) 魏修祺 201421122008 孙劲林 201421122022 1. 会议当天照片 忘记拍了,能补上不? 2. 每个人的工作 3. 燃尽图
- JQuery Mobile+JS实现智能浮动定位导航条
实现原理 主要用到几个知识点: 什么是scrollTop? CSS position定位 判断是否为IE6浏览器 元素相对于窗口的距离 原理:1,浏览器向下滚动时,当document的scrollTo ...
- ubuntu16.04更新内核--使用4.6以上的内核会让用A卡的Dell电脑更快--及卸载多余内核
tips:我自己就是Dell的A卡电脑,用16.04桌面感觉不如fedora流畅,后来手动升级到4.6.2内核,发现可以和fedora与windows一般桌面操作流畅度. 我试过了4.7的开发版内核, ...
- @objc vs @objc dynamic官方解释
Some Objective-C APIs—like target-action—accept method or property names as parameters, then use tho ...
- 1051. [HAOI2006]受欢迎的牛【强连通分量】
Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...
- Python自动化之__unicode__
def __unicode__(self): return u'%s %s' % (self.first_name, self.last_name) 如果定义了__unicode__()方法但是没有定 ...
- java 内存优化
计数器pc 2.2 虚拟机栈和程序计数器一样,虚拟机栈也是线程私有的,它的生命周期与线程相同.虚拟机栈描述的是java方法执行的内存模型. 每个方法(不包含native方法)执行的同时都会创建一个栈帧 ...