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 上.
随机推荐
- 用CHTCollectionViewWaterfallLayout写瀑布流
用CHTCollectionViewWaterfallLayout写瀑布流 实现的瀑布流效果图: 源码: WaterfallCell.h 与 WaterfallCell.m // // Waterfa ...
- [翻译] BKZoomView
BKZoomView https://github.com/freshking/BKZoomView A UIView that will zoom into its parent view. It ...
- MFC连接postgre数据库
配置环境: include:libpq-fe.h文件 lib:libpq.lib文件 dll:libeay32.dll,libiconv-2.dll,libintl-8.dll,libpq.dll,s ...
- Linux 系统的用户和组详解_【all】
1.Linux 用户和用户组详解 2.Linux 文件特殊权限详解 3.Linux 文件的读写执行权限的说明 4.Linux 架构之简述企业网站 5.Linux 环境变量设置详解 6.企业生产环境用户 ...
- 关于Tomcat端口出现的问题
=Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. Th ...
- October 03rd 2017 Week 40th Tuesday
Don't make promises you can't keep. But those are the best kind. 不要许下做不到的承诺,但是我们做不到的承诺往往是最好的. The be ...
- 0x01 现阶段目标
现阶段目标: 1.完成前端知识基础的学习. 具体如下: 在目前学习的基础上(html,css,JavaScript+BOM基础已经大致了解).针对DOM进行学习,个人在http://how2j.cn? ...
- PHP常用符号和函数
(转)最近在写PHP程序的时候发现了一些特殊的PHP符号,例如连续小于符号,三个小于符号,eot,eod,echo示例,print示例等,突然间 发现用这么久的PHP了,竟然连PHP的基本符号都没有认 ...
- ethers.js-2-wallets and signers
Application Programming Interface (API) Wallets and Signers A Wallet manages a private/public key pa ...
- 集合之fail-fast机制
在JDK的Collection中我们时常会看到类似于这样的话: 例如,ArrayList: 注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证.快速失 ...