基于matplotlib对iris数据集进行数据分析
- iris介紹
iris數據集也稱鳶尾花數據集。包括150個數據樣本,分為三類,每類五十個數據,每個數據具有四個屬性,可通過四個屬性預測鳶尾花屬于哪一類。
- 用到的python庫
matplotlib、pandas、sklearn、seaborn
/這里因為我沒有下載iris數據集,所以從sklearn里面導入,如果有數據集則用pandas.read_csv打開即可。/
有了數據集以后就直接作圖等操作就好了。 let‘s go!
導入數據集,看看數據集長啥樣子。
把數據集轉換為pandas的DataFrame類型便于操作(類似與二維表)
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
<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
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()
總結
以上是生活随笔為你收集整理的基于matplotlib对iris数据集进行数据分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 据悉,深圳某工程师沦为C语言笔试枪手
- 下一篇: 没有串口,如何打印单片机调试信息?