Python 属性错误:';numpy.ndarray和#x27;对象没有属性';获取';

Python 属性错误:';numpy.ndarray和#x27;对象没有属性';获取';,python,numpy,matplotlib,data-visualization,seaborn,Python,Numpy,Matplotlib,Data Visualization,Seaborn,实际上,我正试图使用seaborn库为我的numpy数组绘制一个(5216,1)形状的计数图。当我试着这样做的时候 train_y.shape sns.set(style="darkgrid") ax = sns.countplot(x="class", data=train_y) AttributeError Traceback (most recent call last) <ipython

实际上,我正试图使用seaborn库为我的numpy数组绘制一个(5216,1)形状的计数图。当我试着这样做的时候

train_y.shape
sns.set(style="darkgrid")
ax = sns.countplot(x="class", data=train_y)
AttributeError                            Traceback (most recent call last)
<ipython-input-33-44c4401caea5> in <module>
    1 sns.set(style="darkgrid")
----> 2 ax = sns.countplot(x="class", data=train_y)

/opt/conda/lib/python3.7/site-packages/seaborn/categorical.py in countplot(x, y, hue, data, order, hue_order, orient, color, palette, saturation, dodge, ax, **kwargs)
 3553                           estimator, ci, n_boot, units, seed,
 3554                           orient, color, palette, saturation,
-> 3555                           errcolor, errwidth, capsize, dodge)
 3556 
 3557     plotter.value_label = "count"

/opt/conda/lib/python3.7/site-packages/seaborn/categorical.py in __init__(self, x, y, hue, data, order, hue_order, estimator, ci, n_boot, units, seed, orient, color, palette, saturation, errcolor, errwidth, capsize, dodge)
 1613         """Initialize the plotter."""
 1614         self.establish_variables(x, y, hue, data, orient,
-> 1615                                  order, hue_order, units)
 1616         self.establish_colors(color, palette, saturation)
 1617         self.estimate_statistic(estimator, ci, n_boot, seed)

/opt/conda/lib/python3.7/site-packages/seaborn/categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
  141             # See if we need to get variables from `data`
  142             if data is not None:
--> 143                 x = data.get(x, x)
  144                 y = data.get(y, y)
  145                 hue = data.get(hue, hue)

AttributeError: 'numpy.ndarray' object has no attribute 'get'
但它像这样贯穿整个世界

train_y.shape
sns.set(style="darkgrid")
ax = sns.countplot(x="class", data=train_y)
AttributeError                            Traceback (most recent call last)
<ipython-input-33-44c4401caea5> in <module>
    1 sns.set(style="darkgrid")
----> 2 ax = sns.countplot(x="class", data=train_y)

/opt/conda/lib/python3.7/site-packages/seaborn/categorical.py in countplot(x, y, hue, data, order, hue_order, orient, color, palette, saturation, dodge, ax, **kwargs)
 3553                           estimator, ci, n_boot, units, seed,
 3554                           orient, color, palette, saturation,
-> 3555                           errcolor, errwidth, capsize, dodge)
 3556 
 3557     plotter.value_label = "count"

/opt/conda/lib/python3.7/site-packages/seaborn/categorical.py in __init__(self, x, y, hue, data, order, hue_order, estimator, ci, n_boot, units, seed, orient, color, palette, saturation, errcolor, errwidth, capsize, dodge)
 1613         """Initialize the plotter."""
 1614         self.establish_variables(x, y, hue, data, orient,
-> 1615                                  order, hue_order, units)
 1616         self.establish_colors(color, palette, saturation)
 1617         self.estimate_statistic(estimator, ci, n_boot, seed)

/opt/conda/lib/python3.7/site-packages/seaborn/categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
  141             # See if we need to get variables from `data`
  142             if data is not None:
--> 143                 x = data.get(x, x)
  144                 y = data.get(y, y)
  145                 hue = data.get(hue, hue)

AttributeError: 'numpy.ndarray' object has no attribute 'get'
AttributeError回溯(最近一次调用)
在里面
1 sns.set(style=“darkgrid”)
---->2 ax=sns.countplot(x=“class”,data=train\u y)
/countplot中的opt/conda/lib/python3.7/site-packages/seaborn/category.py(x、y、色调、数据、顺序、色调、方向、颜色、调色板、饱和度、减淡、ax、**kwargs)
3553估计器,ci,n_引导,单位,种子,
3554方向、颜色、调色板、饱和度、,
->3555 errcolor、errwidth、倾覆、减淡)
3556
3557 plotter.value\u label=“计数”
/opt/conda/lib/python3.7/site-packages/seaborn/categorical.py in_uuuuuuinit_uuuu(self、x、y、色调、数据、顺序、色调顺序、估计器、ci、n_u引导、单位、种子、方向、颜色、调色板、饱和度、errcolor、errwidth、翻转、减淡)
1613“初始化绘图仪”
1614.建立自变量(x、y、色调、数据、方向、,
->1615订单,顺化订单,单位)
1616.自行确定颜色(颜色、调色板、饱和度)
1617.自我估计统计(估计器、ci、n_引导、种子)
/opt/conda/lib/python3.7/site-packages/seaborn/categorical.py在建立变量(self、x、y、色调、数据、方向、顺序、色调顺序、单位)中
141#看看我们是否需要从`数据'中获取变量`
142如果数据不是无:
-->143 x=data.get(x,x)
144 y=数据。获取(y,y)
145色调=数据。获取(色调,色调)
AttributeError:'numpy.ndarray'对象没有属性'get'

如果您想使用
numpy
数组而不是
pandas.Dataframe
,请帮助我解决此错误,您只需将数组作为
x
y
参数传递到
countplot

例如

这似乎不适用于多维数组。如果不了解正在绘制的数据,就很难确定如何生成所需的特定绘图。但是,由于数组在第二维度中只有长度1,那么简单地将数组重塑为一维怎么样

例如

另外,最好使用
pandas.DataFrames
而不是
numpy.array
。中的示例(我假设您已经尝试在这里进行模拟)使用了
DataFrames
。您可以将数组转换为
数据帧
,并指定稍后打印的变量名称

例如

这里,
“variable”
df
“表”中的一列的名称。然后,当使用
countplot
进行打印时,将此列指定为
x
的参数

ax = sns.countplot(x="variable", data=df)

你想让第一行做什么?谢谢,实际上我从你的回答中得到了一些清晰的直觉。如果我们使用数组来绘制计数图,我们希望将输入形状设置为
(n,)
,或者我们将把numpy数组转换为如上所述的pandas数据帧。
ax = sns.countplot(x="variable", data=df)