在这篇机器学习入门教程中,我们将使用Python中最流行的机器学习工具scikit- learn,在Python中实现几种机器学习算法。使用简单的数据集来训练分类器区分不同类型的水果。
这篇文章的目的是识别出最适合当前问题的机器学习算法。因此,我们要比较不同的算法,选择性能最好的算法。让我们开始吧!
数据
水果数据集由爱丁堡大学的Iain Murray博士创建。他买了几十个不同种类的橘子、柠檬和苹果,并把它们的尺寸记录在一张桌子上。密歇根大学的教授们对水果数据进行了些微的格式化,可以从这里下载。
下载地址:https://github.com/susanli2016/Machine-Learning-with-Python/blob/master/fruit_data_with_colors.txt
让我们先看一看数据的前几行。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fruits = pd.read_csv('fruit_data_with_colors.txt',sep='\t')
print(fruits.head())
数据集的每一行表示一个水果块,它由表中的几个特征表示。
在数据集中有59个水果和7个特征:
print(fruits.shape)
(59, 7)
在数据集中有四种水果:
print(fruits['fruit_name'].unique())
[“苹果”柑橘”“橙子”“柠檬”]
除了柑橘,数据是相当平衡的。我们只好接着进行下一步。
print(fruits.groupby('fruit_name').size())
import seaborn as sns
sns.countplot(fruits['fruit_name'],label="Count")
plt.show()
#每个数字变量的箱线图将使我们更清楚地了解输入变量的分布:
fruits.drop('fruit_label', axis=1).plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False, figsize=(9,9),
title='Box Plot for each input variable')
plt.savefig('fruits_box')
plt.show()
看起来颜色分值近似于高斯分布。
显示各属性的直方图
#显示各属性的直方图
import pylab as pl
fruits.drop('fruit_label' ,axis=1).hist(bins=30, figsize=(9,9))
pl.suptitle("Histogram for each numeric input variable")
plt.savefig('fruits_hist')
plt.show()
一些成对的属性是相关的(质量和宽度)。这表明了高度的相关性和可预测的关系。
#通过散点矩阵图查看属性之间的相关性
from pandas.plotting import scatter_matrix
from matplotlib import cm
feature_names = ['mass', 'width', &#