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 上.
随机推荐
- windows多线程同步
概述 任何单个应用程序都不能完全使该处理器达到满负荷.当一个线程遇到较长等待时间事件时,同步多线程还允许另一线程中的指令使用所有执行单元.例如,当一个线程发生高速缓存不命中,另一个线程可以继续执行.同 ...
- Spring 源码阅读之BeanFactory
1. BeanFactory 的结构体系如下: 2. XmlBeanFactory ,装载Spring配置信息 package org.springframework.beans.factory.xm ...
- 4-6 R语言函数 排序
#sort:对向量进行排序;返回排好序的内容 #order:返回排好序的内容的下标/多个排序标准 > x <- data.frame(v1=1:5,v2=c(10,7,9,6,8),v3= ...
- @objc vs @objc dynamic官方解释
Some Objective-C APIs—like target-action—accept method or property names as parameters, then use tho ...
- JNI学习笔记
JNI是什么->一套c和java的互掉规则 为什么使用JNI 1.非常多敏感效率的代码已经用C实现了 2. JNI双向.java调用c,c调用java Java集成本地代码问题 1.代码 ...
- java.sql.SQLException: Incorrect string value: '\xE5\xB0‘
mysql插入中文字符报java.sql.SQLException: Incorrect string value: '\xE5\xB0‘ #原因:由于默认情况下,mysql的字符集是latin1(I ...
- 1503. [NOI2004]郁闷的出纳员【平衡树-splay】
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经 ...
- zookeeper_monitor监控
.安装 git clone https://github.com/kwarunek/zookeeper_monitor.git cd zookeeper_monitor/ python setup.p ...
- Apache去掉index.php
把 #LoadModule rewrite_module modules/mod_rewrite.so 前面的#去掉, 再把权限AllowOverride None都改为AllowOverride A ...
- php基础学习-sdy
1.php语言结构和函数 exit()和die() exit()相当于把下面的代码都注释了 die()终止脚本 两个差不多 函数有很多种 (1)语言结构 (2)自定义函数 (3)内置函数 functi ...