针对数据集中的一个特定特征绘制多个特征的散点图
Scatter plot multiple features against one specifc feature in a dataset
已编辑:
我有一个包含 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`
我能知道我做错了什么吗?我该如何解决这个问题以获得我想要的输出?
解决原题和问题,错误三处:
- 用列表项而不是索引(整数)索引列表
- 在散点图中使用列表作为 y 参数,而不是列名
- 为数据参数使用特定的列,而不是完整的数据帧
此外,将 columns
属性不必要地转换为列表,然后遍历该列表,而不是直接遍历 columns
属性。
正确的代码删除了 cols_X
和 rows_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()
已编辑:
我有一个包含 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`
我能知道我做错了什么吗?我该如何解决这个问题以获得我想要的输出?
解决原题和问题,错误三处:
- 用列表项而不是索引(整数)索引列表
- 在散点图中使用列表作为 y 参数,而不是列名
- 为数据参数使用特定的列,而不是完整的数据帧
此外,将 columns
属性不必要地转换为列表,然后遍历该列表,而不是直接遍历 columns
属性。
正确的代码删除了 cols_X
和 rows_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()