【发布时间】: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