簡體   English   中英

如何為每個數字列按標簽(分類變量)繪制密度圖?

[英]How to plot density plot by label (categorical variable) for each numeric column?

我嘗試使用地雷和岩石數據( http://archive.ics.uci.edu/ml/datasets/connectionist+bench+(sonar,+mines+vs.+rocks) )進行EDA。 我放置了以下代碼,可以繪制每個數字列的密度圖。

有沒有一種方法可以為數據集中的每個數字變量繪制相同的圖表,但是根據密度是M還是R(最后一列),每個密度圖中有兩條線。 因此,我們可以看到哪個變量顯示了標簽M與R的不同分布。

import pandas as pd

# import file
file = 'https://archive.ics.uci.edu/ml/machine-learning- 
databases/undocumented/connectionist-bench/sonar/sonar.all-data'
mr_df = pd.read_table(file, sep=',', header=None)

mr_df.plot(kind='density', subplots=True, layout=(8,8), sharex=False, legend=False, fontsize=1, figsize=(12,12))
plt.savefig('density plot.png')

在此處輸入圖片說明

plt.subplots(nrows=8, ncols=8, figsize=(12,12))
for i in range(1, 61):
    plt.subplot(8, 8, i)
    mr_df.loc[mr_df[60] == 'R', i-1].plot(kind='density')
    mr_df.loc[mr_df[60] == 'M', i-1].plot(kind='density')

plt.subplot_tool() # allows easy adjustment of the subplot spacing

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM