Day 23 [Python ML、资料视觉化] 直方图、密度图

设定jupyter notebook

import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
print("Setup Complete")
Setup Complete

读取资料和显示资料

# Path of the file to read
iris_filepath = "./iris.csv"

# Read the file into a variable iris_data
iris_data = pd.read_csv(iris_filepath, index_col="Id")

# Print the first 5 rows of the data
iris_data.head()

直方图

可以使用sns.distplot来绘制直方图

# Histogram
sns.distplot(a=iris_data['Petal Length (cm)'], kde=False)
/opt/conda/lib/python3.6/site-packages/seaborn/distributions.py:2551: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)





<AxesSubplot:xlabel='Petal Length (cm)'>

sns.distplot中有两个参数

  • a=选择一个plot(在这个case中,我们选择Petal Length (cm))
  • kde=False在绘制直方图的时候,通常会绘制另一个图表(密度图),不过这边暂时不需要

密度图

下一个图表是密度图,可以当作是比较缓和的直方图

# KDE plot
sns.kdeplot(data=iris_data['Petal Length (cm)'], shade=True)
<AxesSubplot:xlabel='Petal Length (cm)', ylabel='Density'>

  • data=代表说需要用到的资料
  • shade=代表说要在图表下方加上颜色

二维密度图(2D KDE plots)

我们并不限制於单一columns的密度图,也可以创造二维的密度图

# 2D KDE plot
sns.jointplot(x=iris_data['Petal Length (cm)'], y=iris_data['Sepal Width (cm)'], kind="kde", shade=True)
<seaborn.axisgrid.JointGrid at 0x7f405a194710>

彩色编码图

在这个部分我们要将不同种类的图加在一起

先将资料分为三个部分读取

# Paths of the files to read
iris_set_filepath = "./iris_setosa.csv"
iris_ver_filepath = "./iris_versicolor.csv"
iris_vir_filepath = "./iris_virginica.csv"

# Read the files into variables 
iris_set_data = pd.read_csv(iris_set_filepath, index_col="Id")
iris_ver_data = pd.read_csv(iris_ver_filepath, index_col="Id")
iris_vir_data = pd.read_csv(iris_vir_filepath, index_col="Id")

# Print the first 5 rows of the Iris versicolor data
iris_ver_data.head()

我们使用sns.distplot三次来显示三种资料

并且用label=来将资料的名称显示瑜说明栏

# Histograms for each species
sns.distplot(a=iris_set_data['Petal Length (cm)'], label="Iris-setosa", kde=False)
sns.distplot(a=iris_ver_data['Petal Length (cm)'], label="Iris-versicolor", kde=False)
sns.distplot(a=iris_vir_data['Petal Length (cm)'], label="Iris-virginica", kde=False)

# Add title
plt.title("Histogram of Petal Lengths, by Species")

# Force legend to appear
plt.legend()
<matplotlib.legend.Legend at 0x7f405a5ab9b0>

为了要让说明栏显示,要加上plt.legend()

我们也可以使用kdeplot来呈现多个图表

# KDE plots for each species
sns.kdeplot(data=iris_set_data['Petal Length (cm)'], label="Iris-setosa", shade=True)
sns.kdeplot(data=iris_ver_data['Petal Length (cm)'], label="Iris-versicolor", shade=True)
sns.kdeplot(data=iris_vir_data['Petal Length (cm)'], label="Iris-virginica", shade=True)

# Add title
plt.title("Distribution of Petal Lengths, by Species")
Text(0.5, 1.0, 'Distribution of Petal Lengths, by Species')


<<:  day21 开分支,浅谈kotlin paging3 with flow

>>:  Day-25 Hash Function(杂凑函数), 乘法杂凑法, 除法杂凑法

# Day 11 Cache and TLB Flushing Under Linux (三)

废话不多说,我们直接看文件~XD 文件 文件原文:Cache and TLB Flushing Un...

[Day 23] 究竟AI能不能预测股价?

一、究竟AI能不能预测股价? 不能 好了,被我骗进来的可以按上一页了(X 结论已经讲了,如果你对原因...

Day21:21 - 结帐服务(5) - 後端 - 结帐 X PayPal Python Checkout SDK

Salom,我是Charlie! 在Day20的时候我们完成了createOrder跟Capture...

[想试试看JavaScript ] 事件处理

事件处理 事件处理就是当使用者对画面做了一个动作,我们的程序必须侦测这个动作,并且做出反应。 如果事...

Day 25 - Watch os 开发学习2(Button)

今天我们继续学习watch os的开发。 正文 上面所展示的是按下Button之後会将下面的Text...