Seaborn Catplot is throwing error: truth value is ambiguous
Seaborn Catplot is throwing error: truth value is ambiguous
我正在尝试使用 seaborn 库为我的数据框中的所有分类变量绘制 catplot,但我可能会因真值不明确而出错。它通常发生在“&”值上,但我无法在这里找到根本原因。我的目标是连续变量。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
target = df[target_col]
features = df[df.columns.difference([target_col])]
cat_cols = features.select_dtypes(include=['object']).columns.to_list()
fig, axes = plt.subplots(round(len(cat_cols) / 3), 3, figsize=(15, 15))
for i, ax in enumerate(fig.axes):
if i < len(cat_cols):
sns.catplot(x=cat_cols[i], y=target, kind='bar',data=df, ax = ax)
但我收到以下错误。哪个部分导致此值错误?
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
sns.catplot 是网格级别的图,因此您不应将其插入子图。您可以将 facetgrid 与 barplot 结合使用:
例如这是你的数据:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({'y':np.random.uniform(0,1,50),'A':np.random.choice(['a1','a2'],50),
'B':np.random.choice(['b1','b2'],50),'C':np.random.randint(0,10,50),
'D':np.random.choice(['d1','d2'],50),'E':np.random.choice(['e1','e2'],50)})
target_col = "y"
cat_cols = df.columns[df.dtypes==object]
seaborn 在长格式下效果更好,因此您可以像这样旋转长数据:
df.melt(id_vars=target_col,value_vars=cat_cols)
y variable value
0 0.606734 A a1
1 0.603324 A a2
2 0.938280 A a2
3 0.718703 A a1
4 0.808013 A a1
列变量现在定义要绘制的方面,x 轴是您的值。我们直接调用这个:
g = sns.FacetGrid(df.melt(id_vars=target_col,value_vars=cat_cols),
col='variable', sharex=False,col_wrap=3)
g.map_dataframe(sns.barplot, x="value", y="y")
我正在尝试使用 seaborn 库为我的数据框中的所有分类变量绘制 catplot,但我可能会因真值不明确而出错。它通常发生在“&”值上,但我无法在这里找到根本原因。我的目标是连续变量。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
target = df[target_col]
features = df[df.columns.difference([target_col])]
cat_cols = features.select_dtypes(include=['object']).columns.to_list()
fig, axes = plt.subplots(round(len(cat_cols) / 3), 3, figsize=(15, 15))
for i, ax in enumerate(fig.axes):
if i < len(cat_cols):
sns.catplot(x=cat_cols[i], y=target, kind='bar',data=df, ax = ax)
但我收到以下错误。哪个部分导致此值错误?
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
sns.catplot 是网格级别的图,因此您不应将其插入子图。您可以将 facetgrid 与 barplot 结合使用:
例如这是你的数据:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({'y':np.random.uniform(0,1,50),'A':np.random.choice(['a1','a2'],50),
'B':np.random.choice(['b1','b2'],50),'C':np.random.randint(0,10,50),
'D':np.random.choice(['d1','d2'],50),'E':np.random.choice(['e1','e2'],50)})
target_col = "y"
cat_cols = df.columns[df.dtypes==object]
seaborn 在长格式下效果更好,因此您可以像这样旋转长数据:
df.melt(id_vars=target_col,value_vars=cat_cols)
y variable value
0 0.606734 A a1
1 0.603324 A a2
2 0.938280 A a2
3 0.718703 A a1
4 0.808013 A a1
列变量现在定义要绘制的方面,x 轴是您的值。我们直接调用这个:
g = sns.FacetGrid(df.melt(id_vars=target_col,value_vars=cat_cols),
col='variable', sharex=False,col_wrap=3)
g.map_dataframe(sns.barplot, x="value", y="y")