【问题标题】:How to plot the distribution of each in feature in cancer dataset如何绘制癌症数据集中每个特征的分布
【发布时间】:2020-01-16 23:04:45
【问题描述】:

我想使用 ggplot 获取癌症数据集中每个特征的分布,但它给了我错误。

#pip install plotnine
from plotnine import ggplot
from plotnine import *
from sklearn.datasets import load_breast_cancer

for i in cancer.feature_names:
    ggplot(cancer.data)+aes(x=i)+geom_bar(size=10)

这是我收到的错误消息

ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()

【问题讨论】:

  • 您准备好探索 matplotlib 或 seaborn 进行绘图了吗?
  • 既不是 matplotlib 也不是 seaborn,而是 plotnine 库中的 ggplot,实际上 ggplot 是 R 的一个功能,虽然它是 R 我们也可以在 python 中使用。

标签: python-3.x numpy scikit-learn python-ggplot


【解决方案1】:
from plotnine import ggplot
from plotnine import *
from sklearn.datasets import load_breast_cancer
cancer=load_breast_cancer()
import pandas as pd
import matplotlib.pyplot as plt
data=pd.DataFrame(cancer.data,columns=cancer.feature_names)


for i in data.columns:
    print(ggplot(data)+aes(x=i)+geom_density(size=1))
    print(ggplot(data)+aes(x=i)+geom_bar(size=10))

【讨论】:

    【解决方案2】:

    我建议使用seaborn。这是一个按目标绘制癌症数据集中每个特征分布的示例:

    import seaborn as sns
    import pandas as pd
    import numpy as np
    from sklearn.datasets import load_breast_cancer    
    # loading data
    cancer = load_breast_cancer()
    data = pd.DataFrame(np.c_[cancer['data'], cancer['target']],
                      columns= np.append(cancer['feature_names'], ['target']))
    
    df = data.melt(['target'], var_name='cols',  value_name='vals')
    g = sns.FacetGrid(df, col='cols', hue="target", palette="Set1", col_wrap=4)
    g = (g.map(sns.distplot, "vals", hist=True, ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-07
      • 2016-08-27
      • 2021-08-05
      • 2013-03-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-29
      相关资源
      最近更新 更多