0

Actually I am trying to plot a count plot for my numpy array with the shape of (5216,1) by using seaborn library. When I try like this

train_y.shape
sns.set(style="darkgrid")
ax = sns.countplot(x="class", data=train_y)

But It throughs out the like this

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'

anyone please help me to solve this error

asked Jul 9, 2020 at 12:13
1
  • what do you want the first line to do? Commented Jul 9, 2020 at 12:17

1 Answer 1

1

If you want to use a numpy array instead of a pandas.Dataframe, you can simply pass the array as either the x or y argument to countplot.

E.g.

import numpy
import seaborn
data = numpy.array([1, 2, 2, 3, 3, 3])
ax = seaborn.countplot(x=data)

This does not seem to work with multidimensional arrays. Without knowing more about what data you are plotting, it is hard to say for sure how to produce the particular plot you want. However, since your array has only length 1 in the second dimension, how about simply reshaping the array to be one dimensional?

E.g.

train_y.shape = len(train_y)
ax = sns.countplot(x=train_y)

As an aside, it is preferred to use pandas.DataFrames instead of numpy.arrays. The examples in the docs, which I assume you have tried to emulate here, use DataFrames. You can convert your array into a DataFrame, and specify the name of the variable you will later plot.

E.g.

import numpy
import seaborn as sns
import pandas
data = numpy.array([1, 2, 2, 3, 3, 1, 1, 1, 2])
df = pandas.DataFrame(data=data, columns=["variable"])

Here, "variable" is the name of a column in the df 'table'. Then when you plot with countplot, specify this column as the argument to x.

ax = sns.countplot(x="variable", data=df)
answered Jul 9, 2020 at 13:09
1
  • Thank you, Actually I get some clear intuition from your answer. If we use array to plot a count plot we want to give our input shape as (n, ) or we will convert our numpy array as pandas data frame as you mentioned above. Commented Jul 11, 2020 at 6:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.