在这篇文章中,我们将使用Python中最流行的机器学习工具scikit- learn,在Python中实现几种机器学习算法。使用简单的数据集来训练分类器区分不同类型的水果。

这篇文章的目的是识别出最适合当前问题的机器学习算法。因此,我们要比较不同的算法,选择性能最好的算法。让我们开始吧!

数据

水果数据集由爱丁堡大学的Iain Murray博士创建。他买了几十个不同种类的橘子、柠檬和苹果,并把它们的尺寸记录在一张桌子上。密歇根大学的教授们对水果数据进行了些微的格式化,可以从这里下载。

下载地址:https://github.com/susanli2016/Machine-Learning-with-Python/blob/master/fruit_data_with_colors.txt

让我们先看一看数据的前几行。

1%matplotlib inline

2import pandas as pd

3import matplotlib.pyplot as plt

4fruits= pd.read_table('fruit_data_with_colors.txt')

5fruits.head()

图1

数据集的每一行表示一个水果块,它由表中的几个特征表示。

在数据集中有59个水果和7个特征:

1print(fruits.shape)

(59, 7)

在数据集中有四种水果:

1print(fruits['fruit_name'].unique())

[“苹果”柑橘”“橙子”“柠檬”]

除了柑橘,数据是相当平衡的。我们只好接着进行下一步。

1print(fruits.groupby('fruit_name').size())

图2

1import seaborn as sns

2sns.countplot(fruits['fruit_name'],label="Count")

3plt.show()

图3

可视化

每个数字变量的箱线图将使我们更清楚地了解输入变量的分布:

1fruits.drop('fruit_label', axis=1).plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False, figsize=(9,9),

2title='Box Plot for each input variable')

3plt.savefig('fruits_box')

4plt.show()

图4

看起来颜色分值近似于高斯分布。

1import pylab as pl

2fruits.drop('fruit_label' ,axis=1).hist(bins=30, figsize=(9,9))

3pl.suptitle("Histogram for each numeric input variable")

4plt.savefig('fruits_hist')

5plt.show()

图5

一些成对的属性是相关的(质量和宽度)。这表明了高度的相关性和可预测的关系。

1from pandas.tools.plottingimport scatter_matrix

2from matplotlibimport cm

3feature_names= ['mass','width','height','color_score']

4X= fruits[feature_names]

5y= fruits['fruit_label']

6cmap= cm.get_cmap('gnuplot')

7scatter= pd.scatter_matrix(X, c= y, marker= 'o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap= cmap)

8plt.suptitle('Scatter-matrix for each input variable')

9plt.savefig('fruits_scatter_matrix')

图6

统计摘要

图7

我们可以看到数值没有相同的缩放比例。我们需要将缩放比例扩展应用到我们为训练集计算的测试集上。

创建训练和测试集,并应用缩放比例

1from sklearn.model_selectionimport train_test_split

2X_train, X_test, y_train, y_test= train_test_split(X, y, random_state=0)

3from sklearn.preprocessingimport MinMaxScaler

4scaler= MinMaxScaler()

5X_train= scaler.fit_transform(X_train)

6X_test= scaler.transform(X_test)

构建模型

逻辑回归

1from sklearn.linear_modelimport LogisticRegression

2logreg= LogisticRegression()

3logreg.fit(X_train, y_train)

4print('Accuracy of Logistic regression classifier on training set: {:.2f}'

5.format(logreg.score(X_train, y_train)))

6print('Accuracy of Logistic regression classifier on test set: {:.2f}'

7.format(logreg.score(X_test, y_test)))

训练集中逻辑回归分类器的精确度:0.70

测试集中逻辑回归分类器的精确度:0.40

决策树

1from sklearn.treeimport DecisionTreeClassifier

2clf= DecisionTreeClassifier().fit(X_train, y_train)

3print('Accuracy of Decision Tree classifier on training set: {:.2f}'

4.format(clf.score(X_train, y_train)))

5print('Accuracy of Decision Tree classifier on test set: {:.2f}'

6.format(clf.score(X_test, y_test)))

训练集中决策树分类器的精确度:1.00

测试集中决策树分类器的精确度:0.73

K-Nearest Neighbors(K-NN )

1from sklearn.neighborsimport KNeighborsClassifier

2knn= KNeighborsClassifier()

3knn.fit(X_train, y_train)

4print('Accuracy of K-NN classifier on training set: {:.2f}'

5.format(knn.score(X_train, y_train)))

6print('Accuracy of K-NN classifier on test set: {:.2f}'

7.format(knn.score(X_test, y_test)))

训练集中K-NN 分类器的精确度:0.95

测试集中K-NN 分类器的精确度:1.00

线性判别分析

1from sklearn.discriminant_analysisimport LinearDiscriminantAnalysis

2lda= LinearDiscriminantAnalysis()

3lda.fit(X_train, y_train)

4print('Accuracy of LDA classifier on training set: {:.2f}'

5.format(lda.score(X_train, y_train)))

6print('Accuracy of LDA classifier on test set: {:.2f}'

7.format(lda.score(X_test, y_test)))

训练集中LDA分类器的精确度:0.86

测试集中LDA分类器的精确度:0.67

高斯朴素贝叶斯

1from sklearn.naive_bayesimport GaussianNB

2

3gnb= GaussianNB()

4gnb.fit(X_train, y_train)

5print('Accuracy of GNB classifier on training set: {:.2f}'

6.format(gnb.score(X_train, y_train)))

7print('Accuracy of GNB classifier on test set: {:.2f}'

8.format(gnb.score(X_test, y_test)))

训练集中GNB分类器的精确度:0.86

测试集中GNB分类器的精确度:0.67

支持向量机

1from sklearn.svmimport SVC

2

3svm= SVC()

4svm.fit(X_train, y_train)

5print('Accuracy of SVM classifier on training set: {:.2f}'

6.format(svm.score(X_train, y_train)))

7print('Accuracy of SVM classifier on test set: {:.2f}'

8.format(svm.score(X_test, y_test)))

训练集中SVM分类器的精确度:0.61

测试集中SVM分类器的精确度:0.33

KNN算法是我们尝试过的最精确的模型。混淆矩阵提供了在测试集上没有错误的指示。但是,测试集非常小。

1from sklearn.metricsimport classification_report

2from sklearn.metricsimport confusion_matrix

3pred= knn.predict(X_test)

4print(confusion_matrix(y_test, pred))

5print(classification_report(y_test, pred))

图8

绘制k-NN分类器的决策边界

01import matplotlib.cm as cm

02from matplotlib.colorsimport ListedColormap, BoundaryNorm

03import matplotlib.patches as mpatches

04import matplotlib.patches as mpatches

05X= fruits[['mass','width','height','color_score']]

06y= fruits['fruit_label']

07X_train, X_test, y_train, y_test= train_test_split(X, y, random_state=0)

08def plot_fruit_knn(X, y, n_neighbors, weights):

09X_mat= X[['height','width']].as_matrix()

10y_mat= y.as_matrix()

11# Create color maps

12cmap_light= ListedColormap(['#FFAAAA','#AAFFAA','#AAAAFF','#AFAFAF'])

13cmap_bold= ListedColormap(['#FF0000','#00FF00','#0000FF','#AFAFAF'])

14

15clf= neighbors.KNeighborsClassifier(n_neighbors, weights=weights)

16clf.fit(X_mat, y_mat)

17# Plot the decision boundary by assigning a color in the color map

18# to each mesh point.

19

20mesh_step_size= .01 # step size in the mesh

21plot_symbol_size= 50

22

23x_min, x_max= X_mat[:,0].min()- 1, X_mat[:,0].max()+ 1

24y_min, y_max= X_mat[:,1].min()- 1, X_mat[:,1].max()+ 1

25xx, yy= np.meshgrid(np.arange(x_min, x_max, mesh_step_size),

26np.arange(y_min, y_max, mesh_step_size))

27Z= clf.predict(np.c_[xx.ravel(), yy.ravel()])

28# Put the result into a color plot

29Z= Z.reshape(xx.shape)

30plt.figure()

31plt.pcolormesh(xx, yy, Z, cmap=cmap_light)

32# Plot training points

33plt.scatter(X_mat[:,0], X_mat[:,1], s=plot_symbol_size, c=y, cmap=cmap_bold, edgecolor= 'black')

34plt.xlim(xx.min(), xx.max())

35plt.ylim(yy.min(), yy.max())

36patch0= mpatches.Patch(color='#FF0000', label='apple')

37patch1= mpatches.Patch(color='#00FF00', label='mandarin')

38patch2= mpatches.Patch(color='#0000FF', label='orange')

39patch3= mpatches.Patch(color='#AFAFAF', label='lemon')

40plt.legend(handles=[patch0, patch1, patch2, patch3])

41plt.xlabel('height (cm)')

42plt.ylabel('width (cm)')

43plt.title("4-Class classification (k = %i, weights = '%s')"

44% (n_neighbors, weights))

45plt.show()

46plot_fruit_knn(X_train, y_train,5,'uniform')

图9

01k_range= range(1,20)

02scores= []

03

04for kin k_range:

05knn= KNeighborsClassifier(n_neighbors= k)

06knn.fit(X_train, y_train)

07scores.append(knn.score(X_test, y_test))

08plt.figure()

09plt.xlabel('k')

10plt.ylabel('accuracy')

11plt.scatter(k_range, scores)

12plt.xticks([0,5,10,15,20])

图10

对于这个特定的数据集,当k = 5时,我们获得了最高精确度。

结语

在这篇文章中,我们关注的是预测的准确度。我们的目标是学习一个具有良好泛化性能的模型。这样的模型使预测准确度最大化。通过比较不同的算法,我们确定了最适合当前问题的机器学习算法(即水果类型分类)。

创建这个帖子的源代码可以在这里找到。

源代码地址:https://github.com/susanli2016/Machine-Learning-with-Python/blob/master/Solving%20A%20Simple%20Classification%20Problem%20with%20Python.ipynb

来自ATYUN订阅号

python数据分析水果_用python解决简单的水果分类问题相关推荐

  1. python 数据分析 电信_基于Python的电信客户流失分析和预测

    一.项目背景 电信服务是生活中常见的消费服务,在现代社会,凡是使用手机打电话,或者在家看电视,都必须通过电信运营商提供的通话.网络等服务才能实现.本文采用来自kaggle平台的电信客户数据集,来分析人 ...

  2. python数据分析准备_使用Python进行数据分析I 环境准备

    下载Python包并安装:https://www.continuum.io/downloads Alan采用Mac OSX进行操作,这里下载最新版Python 3.5 在Terminal中输入pyth ...

  3. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  4. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  5. python主程序流程图_用Python编程绘制流程图,你用过吗?

    您一定听说过 "Graphviz"绘图软件吧.Graphviz (Graph Visualization Software) 是一个由AT&T实验室启动的开源工具包,它采用 ...

  6. Python数据分析初探项目 基于Python数据可视化的网易云音乐歌单分析系统 大学编程作业(TUST 天津科技大学 2022年)

    Python 数据分析初探项目 基于 Python 数据可视化的网易云音乐歌单分析系统 大学编程作业(TUST 天津科技大学 2022 年) Python 数据分析初探项目 基于 Python 数据可 ...

  7. 如何掌握好python数据分析技能_从负基础起步,掌握数据分析技能

    笔者向我们说明了学会数据分析对于运营人的重要性,并介绍了如何掌握数据分析技能. 作为一个在大学与统计.数学.计算机这些学科完全绝缘的语言学专业的毕业僧,同时又是一个从医疗销售半路出家转行到互联网的产品 ...

  8. python数据分析神器_牛逼啊!一个随时随地写Python代码的神器

    作者: Leoxin 公众号:菜鸟学Python 现在学Python的人越来越多,很多小伙伴都非常有激情.利用碎片时间随时随地学习Python, 大家知道Python是一门编程语言,但是学语言光看不练 ...

  9. python 数据分析库_五个 Python 常用数据分析库

    前言 Python 是常用是数据分析工具,常用的数据分析库有很多,下面主要介绍如下五个分析库:NumPy.Pandas.SciPy.StatsModels.Matplotlib. NumPy 是一个非 ...

  10. python高斯求和_利用Python进行数据分析(3)- 列表、元组、字典、集合

    本文主要是对Python的数据结构进行了一个总结,常见的数据结构包含:列表list.元组tuple.字典dict和集合set. image 索引 左边0开始,右边-1开始 通过index()函数查看索 ...

最新文章

  1. 生产环境一次诡异的NPE问题,反转了4次
  2. Fastjson反序列化漏洞研究
  3. YbtOJ#791-子集最值【三维偏序】
  4. php 查询and or,php – SQL查询多个AND和OR不起作用
  5. 密码学专题 鉴别协议|实际应用的混合协议
  6. LeetCode MySQL 614. 二级关注者
  7. html5酷炫表白代码_七夕表白代码,樱花特效+爱心特效+花瓣+评论留言功能等
  8. python环境及pycharm开发环境安装_Python PyQt5 Pycharm 环境搭建及配置详解(图文教程)...
  9. 一次SpringBoot AutoWired 注入服务为null的事件
  10. UVA1368 UVALive3602 ZOJ3132 DNA Consensus String【贪心】
  11. 全志A33 Android4.4 RTL8723DS WIFI/BT驱动调试
  12. 工程经济作业1答案_工程经济学1、2、3、4(作业1答案)
  13. 联系人管理系统 python版
  14. 基于 Python 的高考志愿高校及专业分析系统
  15. [算法学习no7]图的遍历
  16. React框架+cesium加载GeoWebCache发布4326WMTS服务的ArcGIS切片图层请求400问题
  17. css 设置MP4 video视频背景色透明
  18. 编译小米2s CyanogenMod 版本遇到的几个问题 (02.26更新)
  19. omni私链常用命令
  20. linux硬件级虚拟机系统 电脑安桌游戏多开完全去除vm标识去虚拟化

热门文章

  1. 【STM32F429】第9章 ThreadX GUIX移植到STM32F429(IAR)
  2. HTML-简单表单制作
  3. mac查看内存使用情况命令
  4. 51Nod1203 2012集训队答辩 JZPLCM
  5. html li 点图片,html中ul li前面小黑点样式 ul li一些样式
  6. 平面几何----用角平分线逆定理证明阿氏圆定理
  7. 关于冯诺依曼计算机语言,冯-诺依曼式程序语言浅论 -- NothingHere -- 编程爱好者博客...
  8. java中判断对象不为空字符串_Java判断对象是否为空(包括null ,)的方法
  9. Go 中文和unicode字符之间转换
  10. 显示计算机程序的表格如何打开,如何设置打开excel表格的一个窗口显示多标签...