【问题标题】:Scatter plot multiple features against one specifc feature in a dataset针对数据集中的一个特定特征散点图多个特征
【发布时间】:2021-12-27 09:48:22
【问题描述】:

已编辑:

我有一个包含 10 个特征和一个二元分类列的数据集。

数据集如下所示:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 100 entries, 0 to 99
Data columns (total 11 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   x1      100 non-null    float64
 1   x2      100 non-null    float64
 2   x3      100 non-null    float64
 3   x4      100 non-null    float64
 4   x5      100 non-null    float64
 5   x6      100 non-null    float64
 6   x7      100 non-null    float64
 7   x8      100 non-null    float64
 8   x9      100 non-null    float64
 9   x10     100 non-null    float64
 10  y       100 non-null    int64  
dtypes: float64(10), int64(1)
memory usage: 8.7 KB
time: 41.6 ms (started: 2021-12-27 10:30:27 +00:00)

我已经在一对图中将这些特征与一个特定特征x10 进行了对比。如下图所示:

但是,我想将这些图分开并有多个散点图(x10 功能针对所有其他 9 个功能一次一个功能)

我已经写了下面的代码:

# Generate some data
df = pd.DataFrame({
    'x1': np.random.normal(0, 1, 100),
    'x2': np.random.normal(0, 1, 100),
    'x3': np.random.normal(0, 1, 100),
    'x4': np.random.normal(0, 1, 100),
    'x5': np.random.normal(0, 1, 100),
    'x6': np.random.normal(0, 1, 100),
    'x7': np.random.normal(0, 1, 100),
    'x8': np.random.normal(0, 1, 100),
    'x9': np.random.normal(0, 1, 100),
    'x10': np.random.normal(0, 1, 100),
    'y': np.random.choice([0, 1], 100)})


# split data into X and y
X = df.iloc[:, :10]

# specifying columns and rows for the plot
X_cols = X.columns
y_rows = ['x10']

# # pair plot
# sns_plot = sns.pairplot(data = df, x_vars=X_cols, y_vars=y_rows, hue = 'y', palette='RdBu')

# multiple scatter plots
for feature in X_cols:
   sns.scatterplot(data = df[feature], x=feature , y='x10', hue = 'y', palette='RdBu')
   plt.show()

我收到此错误:

ValueError                                Traceback (most recent call last)
<ipython-input-24-ad3cca752a2e> in <module>()
     26 # multiple scatter plots
     27 for feature in X_cols:
---> 28    sns.scatterplot(data = df[feature], x=feature , y='x10', hue = 'y', palette='RdBu')
     29    plt.show()
     30 

5 frames
/usr/local/lib/python3.7/dist-packages/seaborn/_core.py in _assign_variables_longform(self, data, **kwargs)
    901 
    902                 err = f"Could not interpret value `{val}` for parameter `{key}`"
--> 903                 raise ValueError(err)
    904 
    905             else:

ValueError: Could not interpret value `x1` for parameter `x`

我能知道我做错了什么吗?以及如何解决这个问题以获得我想要的输出?

【问题讨论】:

  • 请注意,您有for feature in cols_X 后跟x=cols_X[feature],这实际上没有意义。第二部分也许只是想要x=feature。此外,除此之外,您不需要cols_X = X.columns.to_list(),只需cols_X = X.columns 就足够了。或者甚至删除该行,并移至for feature in X.columns,然后在下一行添加..., x=feature, ...
  • @9769953 我已经按照您的建议编辑了代码,但现在出现以下错误`ValueError:无法解释参数address 的值x`
  • 哦,也许首先更改为在您的散点图调用中使用y='time(并删除行rows_y = ['time']:不需要它,y 参数也不需要是列表在这里)。
  • 出现第三个错误。使用sns.scatterplot(data = df, x=feature , y='x10', hue = 'y', palette='RdBu')。注意data=df,而不是data=df[feature]:传递完整的数据框,而不是单个列。

标签: python seaborn scatter-plot


【解决方案1】:

解决原来的问题和问题,有三个错误:

  • 使用列表项而不是索引(整数)索引列表
  • 在散点图中使用 y 参数列表,而不是列名
  • 为数据参数使用特定列,而不是完整的数据框

此外,columns 属性不必要地转换为列表,然后迭代该列表,而不是直接迭代 columns 属性。

正确的代码删除了cols_Xrows_y 的分配,并将循环简化为以下内容:

for feature in cols_X.columns:
    sns.scatterplot(data=normalized_df, x=feature, y='time', hue='binary result', palette='RdBu')
    plt.show()

(请注意,cols_X 必须是normalized_df 的子集(按列),因此至少它不包括“时间”列,以避免创建“时间”与“时间”的散点图“时间”。或者可以通过在 sns.scatterplot 行上方的快速 if feature == "time": continue 忽略这种情况。)


为了比较,这是原始代码:

# relatively irrelevant above omitted

cols_X = X.columns.to_list()
rows_y = ['time']

for feature in cols_X:
  sns.scatterplot(data = normalized_df[feature], x= cols_X[feature], y= rows_y , hue = 'binary result', palette='RdBu')
  plt.show()

【讨论】:

    猜你喜欢
    • 2019-06-13
    • 2022-04-26
    • 2019-12-05
    • 2015-08-16
    • 2017-09-12
    • 2021-01-03
    • 2019-04-19
    • 2013-10-30
    • 2016-08-05
    相关资源
    最近更新 更多