Day 22 [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
insurance_filepath = "./insurance.csv"

# Read the file into a variable insurance_data
insurance_data = pd.read_csv(insurance_filepath)

读取完资料後,可以将其前5笔资料印出

insurance_data.head()

散布图

要创建一个简单的散布图,需要先设定x轴跟y轴需要的资料

sns.scatterplot(x=insurance_data['bmi'], y=insurance_data['charges'])
<AxesSubplot:xlabel='bmi', ylabel='charges'>

以上图来说,BMI越高的人,被收的费用理论上也会越多

可以在图表中多加一条回归线(regression line),可以确保猜测是对的

sns.regplot(x=insurance_data['bmi'], y=insurance_data['charges'])
<AxesSubplot:xlabel='bmi', ylabel='charges'>

Color-coded scatter plots

若我们想在图表中看出吸菸(smoke)跟BMI还有收费(charge)之间的关系,可以将图表中加入颜色

sns.scatterplot(x=insurance_data['bmi'], y=insurance_data['charges'], hue=insurance_data['smoker'])
<AxesSubplot:xlabel='bmi', ylabel='charges'>

这时我们可以使用sns.lmplot来看出这两个区间的回归线差别

sns.lmplot(x="bmi", y="charges", hue="smoker", data=insurance_data)
<seaborn.axisgrid.FacetGrid at 0x7f894623c080>

可以看出吸菸者的回归线比没有吸菸的人高陡峭很多

sns.lmplot跟之前遇到的产生图表的方法有些许的差异

  • 之前取x轴的方法为x=insurance_data['bmi'],在这个方法中只需要用x="bmi"
  • y轴跟hue也是
  • 使用data=insurance_data可以读取档案

若是想做categorical scatter plot的图表,可以使用swarmplot来绘制图表

sns.swarmplot(x=insurance_data['smoker'],
              y=insurance_data['charges'])
/opt/conda/lib/python3.6/site-packages/seaborn/categorical.py:1296: UserWarning: 67.3% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)





<AxesSubplot:xlabel='smoker', ylabel='charges'>


<<:  Day 20 : 案例分享(6.3) 人事、差勤与薪资 - 线上请休假及签到退

>>:  Day.27 实务应用 - 实作表自动分区管理( event / procedure / partition )_2

DAY1- 密码学在干嘛?

我快要猜到你的密码了。等一下,为什麽是我的生日。 我是谁? 徐老师 开玩笑的。我是陈彦龙 ,主修经济...

DAY22 - [React] 资料夹结构概述

今日文章目录 资料夹结构概述 参考资料 今天写一点我对 React资料结构的基础了解,在写Reac...

爬虫怎麽爬 从零开始的爬虫自学 DAY1 爬虫怎麽爬

我是谁 我是一个资讯相关科系的大学生,也是资讯方面的小小新手,这次不仅因为想要挑战自我,更因为学校有...

前言 | ML#Day1

AI和资料科学在最近几年是非常火红的话题,日常已经可以随处可见各种广告或者企业将AI挂在嘴边,声称其...

激励函数

  上一篇有提到激励函数,但只了解到用来引入非线性资料。可是什麽是非线性资料?sigmoid, ta...