iris数据_从iris数据集入门scikit-learn

iris数据集是由三种鸢尾花,各50组数据构成的数据集。每个样本包含4个特征,分别为萼片(sepals)的长和宽、花瓣(petals)的长和宽。

7857138efe684590362baf29e1ec7c6a.png

1. 载入iris数据


你还可以通过python的csv模块,或者NumPy的loadtxt函数,或者Pandas的read_csv()函数读取从UCI Iris dataset下载的csv文件。

from sklearn.datasets import load_iris
iris = load_iris()
type(iris)

sklearn.datasets.base.Bunch

print iris.feature_names
print iris.target_names

['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
['setosa' 'versicolor' 'virginica']

在scikit-learn中对数据有如下要求:

  • 特征和标签要用分开的对象存储
  • 特征和标签要是数字
  • 特征和标签都要使用numpy的array来存储
print type(iris.data)
print type(iris.target)

<type 'numpy.ndarray'>
<type 'numpy.ndarray'>

print iris.data.shape
print iris.target.shape

(150, 4)
(150,)

# store features matrix in "X"
X = iris.data

# store response vector in "y"
y = iris.target

回顾iris数据集

from IPython.display import HTML
HTML('<iframe src=http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data width=300 height=200></iframe>')

绘制iris的2d图

%matplotlib inline
import matplotlib.pyplot as plt
X_sepal = X[:, :2]
plt.scatter(X_sepal[:, 0], X_sepal[:, 1], c=y, cmap=plt.cm.gnuplot)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')

<matplotlib.text.Text at 0x554f850>

301eb02ee2661abd8c8426a489a07e94.png
X_petal = X[:, 2:4]
plt.scatter(X_petal[:, 0], X_petal[:, 1], c=y, cmap=plt.cm.gnuplot)
plt.xlabel('Petal length')
plt.ylabel('Petal width')

<matplotlib.text.Text at 0x5705e90>

31ea8dd3f4543eea31583f918c90a955.png

2. 使用K近邻进行分类


KNN分类的基本步骤:

  1. 选择K的值
  2. 在训练数据集中搜索K个距离最近的观测值
  3. 使用最多的那个标签作为未知数据的预测


下面给出KNN的演示图例, 分别是训练数据、K=1时的KNN分类图、K=5时的KNN分类图

67eeea5708bd1d65378fdf9c6aaada27.png

60c71b7f3853392899ca6f298995f893.png

811b0ab083f8750d3e7952976fa2f8da.png

scikit-learn进行模型匹配的4个一般步骤:
第一步:载入你要使用的模型类

from sklearn.neighbors import KNeighborsClassifier

第二步:实例化分类器

# looking for the one nearest neighbor
knn = KNeighborsClassifier(n_neighbors=1)
print knn

KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_neighbors=1, p=2, weights='uniform')

第三步:用数据来拟合模型(进行模型的训练)

knn.fit(X, y)

KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_neighbors=1, p=2, weights='uniform')

第四步:对新的观测值进行预测

knn.predict([3, 5, 4, 2])

array([2])

X_new = [[3, 5, 4, 2], [5, 4, 3, 2]]
knn.predict(X_new)

array([2, 1])

使用不同的K值

knn5 = KNeighborsClassifier(n_neighbors=5)

knn5.fit(X, y)

knn5.predict(X_new)

array([1, 1])

依照同样的流程,使用不同的分类模型

# import the class
from sklearn.linear_model import LogisticRegression

# instantiate the model (using the default parameters)
logreg = LogisticRegression()

# fit the model with data
logreg.fit(X, y)

# predict the response for new observations
logreg.predict(X_new)

array([2, 0])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值