【问题标题】:Using seaborn.boxplot() for unbalanced data frames对不平衡数据帧使用 seaborn.boxplot()
【发布时间】:2022-12-17 20:28:43
【问题描述】:

使用以下代码,我想使用 seaborn.boxplot() 创建一个箱线图。

print(result)
print(result.info())
sns.boxplot(y=result);

输出显示以下错误:

             M1      M2
0       21846.0   49152
1         136.0    8192
2       21846.0      68
3         136.0  196608
4        1366.0   65536
...         ...     ...
141959      NaN  116736
141960      NaN      68
141961      NaN   29184
141962      NaN  116736
141963      NaN    8192

[141964 rows x 2 columns]
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 141964 entries, 0 to 141963
Data columns (total 2 columns):
 #   Column   Non-Null Count   Dtype  
---  ------   --------------   -----  
 0   3d-unet  113183 non-null  float64
 1   bert     141964 non-null  int64  
dtypes: float64(1), int64(1)
memory usage: 2.2 MB
None
Traceback (most recent call last):
  File "cta_dist.py", line 26, in <module>
    sns.boxplot(y=result);
  File "/home/u2/.local/lib/python3.8/site-packages/seaborn/_decorators.py", line 46, in inner_f
    return f(**kwargs)
  File "/home/u2/.local/lib/python3.8/site-packages/seaborn/categorical.py", line 2240, in boxplot
    plotter = _BoxPlotter(x, y, hue, data, order, hue_order,
  File "/home/u2/.local/lib/python3.8/site-packages/seaborn/categorical.py", line 406, in __init__
    self.establish_variables(x, y, hue, data, orient, order, hue_order)
  File "/home/u2/.local/lib/python3.8/site-packages/seaborn/categorical.py", line 156, in establish_variables
    orient = infer_orient(
  File "/home/u2/.local/lib/python3.8/site-packages/seaborn/_core.py", line 1305, in infer_orient
    y_type = None if y is None else variable_type(y)
  File "/home/u2/.local/lib/python3.8/site-packages/seaborn/_core.py", line 1222, in variable_type
    if pd.isna(vector).all():
  File "/home/u2/.local/lib/python3.8/site-packages/pandas/core/generic.py", line 1442, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我猜这是因为数据点不平衡,因为 M1 的数据值比 M2 少。有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: python-3.x dataframe seaborn boxplot


    【解决方案1】:

    如果我做对了,您需要对整个数据帧使用数据参数。 y 参数仅用于存储在数据框中的专用变量:

    print(result)
    print(result.info())
    sns.boxplot(y=result["M1"]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 2019-01-25
      相关资源
      最近更新 更多