Python 與數據資料分析3.2-資料視覺化-從 seabon 的函數分類來看

46 篇文章 1 订阅
7 篇文章 0 订阅

“Talk is cheap. Show me the code.”
― Linus Torvalds

老子第41章
上德若谷
大白若辱
大方無隅
大器晚成
大音希聲
大象無形
道隱無名

拳打千遍, 身法自然

110_1_高中週期性課程: Python程式入門與資料分析初探, 道明高中

本系列文章之連結

  • Python程式與數據資料分析1 link
  • Python程式與數據資料分析1.1 Kaggle站免費教學的路徑圖 link
  • Python 與數據資料分析2-資料視覺化-Matplotlib.pyplot 入門 link
  • Python 與數據資料分析3.1-資料視覺化-基本圖表類型 link
  • Python 與數據資料分析3.2-資料視覺化-從 seabon 的函數分類來看 link
  • Python與資料分析3.3-資料視覺化-seaborn 補充 link
  • Python與資料分析4-資料視覺化-鳶尾花 link
  • Python與資料分析 5-入門級競賽分析-鐵達尼號 link


各種圖表從 seaborn 的函數分類來看

在剛學資料分析時, 會被眼花撩亂的圖表及呈現方式弄得見樹不見林, 抓不到起始之入口及學習之路徑, 請網路上更會參雜進各種機器學習人工智能大數據等相關的學習路徑, 讓人莫衷一是, 其實一開始不用好高騖遠,

  • 可以先把基本的各種統計圖表及視覺化的方式作一全面的了解
    我們可以藉由seaborn 的函數分類, 來對基本的各種統計圖表及視覺化的方式作一全面的了解.

而更後續的學習, 請參考我們系列的第一篇講的過程建議, Python程式與數據資料分析1, link.

seaborn 區分圖表為四大類型: relationships, distributions, categorical, regression

簡單而言, 圖表可以區分為, 就是呈現 關係, 分布, 分類, 回歸(曲線擬合) 四大類.

另外還有四大類之外的 jointplot(), pairplot(),
pairplot()應該是 seaborn 最有名的指令,
jointplot()呈現兩個屬性的聯合分布.

類型說明figure-levelaxes-level
關係呈現數據之間的關係relplotscatterplot, lineplot
分布呈現數據的分布: 稀疏或密集等displothistplot, kdeplot, ecdfplot, rugplot, distplot
分類呈現數據的分類catplotstripplot, swarmplot, boxplot, violinplot, boxenplot, pointplot, barplot, countplot
回歸給定一堆點, 找尋最適合之曲線(直線)能(盡量)穿過各點lmplotregplot, residplot
四大類的 seaborn 例子seaborn 的教學tutorial網頁 : https://seaborn.pydata.org/tutorial.html link
四大類之外的 jointplot(), pairplot()pairplot()應該是 seaborn 最有名的指令, jointplot()呈現兩個屬性的聯合分布jointplot(), pairplot()兩者都是 figure-level functions

註: The regplot() and lmplot() functions are closely related, but the former is an axes-level function while the latter is a figure-level function that combines regplot() and FacetGrid. link

註: seaborn 函數分 axes-level(專門型), figure-level(通用型)兩種, 請看下一節的說明.

seaborn 函數分 axes-level(專門型), figure-level(通用型)兩種

在學seaborn 時, 在網路上的說明, 常常 同樣的效果,各家的指令都不同, 會讓初學者感到混亂, 其實可以先了解 seaborn 的設計, 繪圖函數 基本上分通用型 figure-level 與專門型 axes-level兩種類別.
參考下圖, 最上方的函數, 屬於 figure-level 的函數, 之後的較小字體的則是 axes-levle functions,
seaborn 的函數類別分 axes-level, figure-level 兩種

  • figure-level 函數function: 可視為通用型的, 例如 distplot(), 加上例如 kind=“kde”, 就會呈現跟 kdeplot()一樣的效果, 透過更換 kind=" " 的指定, 可以更換它下方的任一個繪圖, 例如 distplot() 涵蓋 histplot(), kdeplot(), ecdfplot(), rugplot().
  • axes-levle funciton: 可視為專門型的, 指執行特定的繪圖, 下圖較小字體的都是 axes-levle functions, 如上一節的圖中第一直行呈現.

Ref: seaborn 有所有函數與指令的列表, 可以參考官網 API reference:
https://seaborn.pydata.org/api.html link

seaborn 網頁 關係 類的圖表教學 Visualizing statistical relationships

在seaborn 網頁講解呈現 relationships 關係圖表 這裡, 完全以 relplot() 來執行, relplot() 是一個 figure-level 通用型的函數, 可以涵蓋 axes-levle funciton專門型的 scatterplot(), lineplot() 兩個函數.

預設是散點圖: The scatterplot() is the default kind in relplot() (it can also be forced by setting kind=“scatter”):

先從 seaborn 載入內部已有的資料集 tips:

tips = sns.load_dataset("tips")

這是一個 waiter 在餐廳收集的資料, 有關帳單, 小費, 是否吸菸, 用餐人數等等, 有關的資料集

print(tips.head())

輸出為

>>> 
= RESTART: C:/data/NEW/網路免費軟體資料/csdn_講義/csdn_Python_講義/202109_高中週期性_Python程式與資料分析_csdn/seaborn.relplot_1.py
   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4

hue="smoker", 來呈現, 吸菸與不吸菸者

我們可以加上 hue=“smoker”, 來呈現, 吸菸與不吸菸者, 在付帳單跟小費時的差異,

sns.relplot(x="total_bill", y="tip", hue="smoker", data=tips);
plt.show()

hue="smoker", 來呈現, 吸菸與不吸菸者, 在付帳單跟小費時的差異
從上圖, 吸菸與不吸菸者, 在付帳單跟小費時, 並沒有呈現明顯的差異, 倒是,

  • 藍色點代表吸菸者, 在小費與帳單之比值中, 似乎較混亂, 呈現一種較隨興的狀況,
  • 而紅色點代表非吸菸者, 則呈現小費與帳單大致呈現正比之關係.

size="size", 來呈現, 用餐人數 size 決定點的大小

# The third kind of semantic variable changes the size of each point:
# 把用餐人數 size 決定點的大小
sns.relplot(x="total_bill", y="tip", size="size", data=tips);
plt.show()

用餐人數 size 決定點的大小
可以將點的大小呈現之相對級距加大, 讓點的大小差異, 在視覺上更明顯

# 可以將點的大小呈現之相對級距加大, 讓點的大小差異, 在視覺上更明顯
sns.relplot(x="total_bill", y="tip", size="size", sizes=(15, 200), data=tips)
plt.show()

可以將點的大小呈現之相對級距加大, 讓點的大小差異, 在視覺上更明顯
從上圖,用餐人數之size 大小, 在付帳單跟小費時, 並沒有呈現絕對的與消費額度呈現正比的關係, 但是, 大致上, 還是呈現一定的正比, 也就是說, 用餐人數多, 則帳單與小費基本上也是呈現較多的趨勢.

Ref: Visualizing statistical relationships, https://seaborn.pydata.org/tutorial/relational.html#relating-variables-with-scatter-plots link

seaborn 網頁 分布 類的圖表教學 Visualizing distributions of data

Ref: Visualizing distributions of data,
https://seaborn.pydata.org/tutorial/distributions.html link

seaborn 網頁 分類 的圖表教學 Plotting with categorical data

ax = sns.stripplot(, jitter=True,)就是散點圖

網頁上說明:
Saving the resulting axes as ax each time causes the resulting plot to be shown on top of the previous axes
意思就是, 可以將圖存為 ax, 如此, 後面的圖會疊在已先產生的圖上.
stripplot(, jitter=True,) 的 jitter, 會讓 點, 稍微左右挪動, 不會落在通一條垂直線上, 可以讓點的分布更清楚呈現,

# Saving the resulting axes as ax each time causes the resulting plot 
# to be shown on top of the previous axes
ax = sns.boxplot(x="Species", y="PetalLengthCm", data=iris)
ax = sns.stripplot(x="Species", y="PetalLengthCm", data=iris, jitter=True, edgecolor="gray")

ax = sns.stripplot(, jitter=True,)

sns.stripplot() 就是散點圖

seaborn.stripplot(*, x=None, y=None, hue=None, data=None, order=None, hue_order=None, jitter=True, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)
Draw a scatterplot where one variable is categorical.
就是散點圖, 適合跟 boxplot(), violinplot() 等結合使用
上圖就是跟 boxplot()結合使用之例子!

Ref: sns.stripplot() link

violinplot() 提琴圖, 其實就是把 boxplot() 跟 stripplot() 合起來的圖

上面跟 stripplot() 跟 boxplot()結合使用之例子, 可以用 violinplot()一個指令來實現.
violinplot()`提琴圖, 其實就是把 boxplot() 跟 stripplot() 合起來的圖

# A violin plot combines the benefits of the previous two plots 
# and simplifies them
# Denser regions of the data are fatter, and sparser thiner in 
# a violin plot
# 提琴圖, 其實就是把 boxplot() 跟 stripplot() 合起來的圖:
sns.violinplot(x="Species", y="PetalLengthCm", data=iris, size=6)
plt.show()

Ref: sns.violinplot() https://seaborn.pydata.org/generated/seaborn.violinplot.html#seaborn.violinplot link

seaborn 網頁 回歸 類的圖表教學 Visualizing regression models

pairplot()

jointplot()

Reference

高中生自學成為 Kaggle Master

  • 17岁!Kaggle史上最年轻Grandmaster诞生:高中自学3年登顶, https://www.sohu.com/a/251612669_473283 link,
    This high school kid taught himself to be an AI wizard, https://mashable.com/article/16-year-old-ai-genius link,
    這個高中生僅靠在線學習成為Kaggle AI大牛,引起Google注意, 原文網址:https://kknews.cc/tech/bqme52m.html link.

  • 00后学霸获5金9银3铜,华人高中生在Kaggle取得大师称号!他是怎么做到的?| 独家专访, 作者:DeepTech深科技, 链接:https://xueqiu.com/5983518614/132545068 link

  • 00后再夺冠!中国高中生10个月拿下Kaggle Master, https://posts.careerengine.us/p/5d2d2bd79aad47269c232d25 link

  • Python程式與數據資料分析1, link.

seaborn

  • seaborn 的詳細介紹可以參考此篇: herr_kun, python-seaborn画图-(matploytlib)更高级的数据绘图工具, https://blog.csdn.net/herr_kun/article/details/87697639?utm_term=python%E7%94%BB%E5%9B%BE%E5%BA%93seaborn&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allsobaiduweb~default-6-87697639&spm=3001.4430 link

  • Kaggle 此處的教學就是使用 seaborn: Data Visualization 資料視覺化教學, https://www.kaggle.com/learn/data-visualization link

  • seaborn 的教學: https://seaborn.pydata.org/tutorial.html link

  • seaborn 有所有函數與指令的列表, 可以參考官網 API reference
    https://seaborn.pydata.org/api.html link

  • 深入的 jointplot()屬性的使用, 點選 API reference jointplot 位置, 會連結到 jointplot 的詳細說明文件處
    https://seaborn.pydata.org/generated/seaborn.jointplot.html#seaborn.jointplot link

  • 在seaborn 網頁講解呈現 relationships 關係圖表: Visualizing statistical relationships, https://seaborn.pydata.org/tutorial/relational.html#relating-variables-with-scatter-plots link

  • stripplot(), https://seaborn.pydata.org/generated/seaborn.stripplot.html#seaborn.stripplot link

  • sns.violinplot() https://seaborn.pydata.org/generated/seaborn.violinplot.html#seaborn.violinplot link

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值