基于matplotlib对iris数据集进行数据分析

  • iris介绍

iris数据集也称鸢尾花数据集。包括150个数据样本,分为三类,每类五十个数据,每个数据具有四个属性,可通过四个属性预测鸢尾花属于哪一类。

  • 用到的python库
    matplotlib、pandas、sklearn、seaborn
    /这里因为我没有下载iris数据集,所以从sklearn里面导入,如果有数据集则用pandas.read_csv打开即可。/

有了数据集以后就直接作图等操作就好了。 let‘s go!

导入数据集,看看数据集长啥样子。
把数据集转换为pandas的DataFrame类型便于操作(类似与二维表)

import pandas as pd
from sklearn.datasets import load_iris
#因为没有iris数据集,只好从sklearn里面导入
import matplotlib.pyplot as plt
import seaborn as sns

iris=load_iris()

feature_names=['sepal length', 'sepal width', 'petal length', 'petal width']
#利用字典把数据转换为dataframe类型
#DataFrame指一种类似与excel的二维表的框架
iris=pd.DataFrame({
        feature_names[0]:[iris.data[i][0] for i in range(len(iris.data))],
        feature_names[1]:[iris.data[i][1] for i in range(len(iris.data))],
        feature_names[2]:[iris.data[i][2] for i in range(len(iris.data))],
        feature_names[3]:[iris.data[i][3] for i in range(len(iris.data))],
        'type':iris.target
})

print(iris)#150*5的二维表

在这里插入图片描述t这就是iris数据集的前20行,没有截完,然后我们来看看前五行和数据总体信息描述(type的0,1,2分别表示三种花setosa,versicolor,virginica)

print(iris.head())#前五条数据

petal length petal width sepal length sepal width type
0 1.4 0.2 5.1 3.5 0
1 1.4 0.2 4.9 3.0 0
2 1.3 0.2 4.7 3.2 0
3 1.5 0.2 4.6 3.1 0
4 1.4 0.2 5.0 3.6 0

print(iris.info())#总体信息

<class ‘pandas.core.frame.DataFrame’>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
petal length 150 non-null float64
petal width 150 non-null float64
sepal length 150 non-null float64
sepal width 150 non-null float64
type 150 non-null int32
dtypes: float64(4), int32(1)
memory usage: 5.4 KB
None

print(iris.describe())#数据描述
      petal length  petal width  sepal length sepal width type

count 150.000000 150.000000 150.000000 150.000000 150.000000
mean 3.758667 1.198667 5.843333 3.054000 1.000000
std 1.764420 0.763161 0.828066 0.433594 0.819232
min 1.000000 0.100000 4.300000 2.000000 0.000000
25% 1.600000 0.300000 5.100000 2.800000 0.000000
50% 4.350000 1.300000 5.800000 3.000000 1.000000
75% 5.100000 1.800000 6.400000 3.300000 2.000000
max 6.900000 2.500000 7.900000 4.400000 2.000000

然后分别画出箱线图、关系矩阵、核密度图。

#画各个特征的关系矩阵
#0,1,2分别代表setosa,versicolor,virginica
sns.pairplot(data=iris,hue='type')
plt.show()

在这里插入图片描述

#0,1,2分别代表setosa,versicolor,virginica
#绘制箱线图
for i in range(4):
    sns.boxplot(x="type",y=feature_names[i],data=iris)
    sns.kdeplot(iris[feature_names[i]])
    plt.show()

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

for i in range(4):
    sns.kdeplot(iris[feature_names[i]],shade=True)
plt.title("Nuclear density")
plt.show()

在这里插入图片描述

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
鸢尾花数据集-数据分析 from sklearn import datasets import pandas as pd import matplotlib.pyplot as plt import numpy as np # 获取鸢尾花数据集 lris_df = datasets.load_iris() # 输⼊特征 lris_df.data # ⽬标特征 lris_df.target data_DF = pd.DataFrame(lris_df.data) target_DF = pd.DataFrame(lris_df.target) # dataframe按列拼接 join_DF = pd.concat([data_DF,target_DF],axis=1) # 修改列名 join_DF.columns=['sepal-length','sepal-width','petal-length','petal-width','class'] # 查看数据分布 x_axis = lris_df.data[:,0] y_axis = lris_df.data[:,2] plt.scatter(x_axis,y_axis,c=lris_df.target) plt.show() # 输⼊特征直⽅图分布 join_DF.iloc[:,0:4].hist() plt.show() # 箱线图 join_DF.iloc[:,0:4].plot(kind='box',subplots=True,layout=(2,2),sharex=False,sharey=False) plt.show() ft_DF = join_DF.iloc[:,0:4] # 相关系数 ft_DF.corr() x_val=ft_DF['petal-width'] y_val=ft_DF['petal-length'] plt.scatter(x_val,y_val) data_array = join_DF.values from sklearn import model_selection # 数据集划分 X = data_array[:,0:4] Y = data_array[:,4] validation_size = 0.2 seed = 6 X_train,X_validation,Y_train,Y_validation = model_selection.train_test_split(X,Y,test_size=validation_size,random_state=seed) # KNN分类 from sklearn.neighbors import KNeighborsClassifier knn = KNeighborsClassifier() knn.fit(X_train,Y_train) knn.fit(X_train,Y_train) print(knn.score(X_validation,Y_validation)) # K折交叉验证 from sklearn.model_selection import cross_val_score scores = cross_val_score(knn,X,Y,cv=5,scoring='accuracy') print(scores) from sklearn.model_selection import KFold dfold = model_selection.KFold(n_splits=10,random_state=7) from sklearn import model_selection import matplotlib.pyplot as plt X=lris_df.data Y=lris_df.target k_range = range(1,31) k_scores = [] for k in k_range: knn = KNeighborsClassifier(n_neighbors=k) #调整K值 scores = model_selection.cross_val_score(knn,X,Y,cv=10,scoring='accuracy') k_scores.append(scores.mean()) plt.plot(k_range,k_scores) plt.xlabel('value of K for KNN') plt.ylabel('Cross-Validated Accuracy') plt.show()

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值