attributeError:' numpy.ndarray'对象没有属性' value_counts'

发布于 2025-02-08 14:34:32 字数 1205 浏览 1 评论 0 原文

我正在尝试使用bar绘制y_train,我会遇到以下错误。请帮助我修复它 自昨天以来,我无法将此错误绘制。

from sklearn.model_selection import train_test_split
import numpy as np
X = reviews['Text']
y= reviews['Score'].values
X_train, X_test, y_train, y_test =train_test_split(X,y ,test_size=0.20,stratify=y,random_state=33)

检查数据拆分的形状

print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
(80000,) (80000,)
(20000,) (20000,)

#plot条形图y_train和y_test

import matplotlib.pyplot as plt
plt.bar([1,0],y_train.value_counts().values,color ='green')

 
plt.xlabel("Count")
plt.ylabel("y_train values")
plt.title("Distribution of y_train")
plt.show()

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-62460aedca56> in <module>()
      1 #plot bar graphs of y_train and y_test
      2 import matplotlib.pyplot as plt
----> 3 plt.bar(y_train.value_counts().values,color ='green')
      4 
      5 plt.xlabel("Count")

AttributeError: 'numpy.ndarray' object has no attribute 'value_counts'

I am trying to plot y_train using bar, I am getting the below error. Kindly help me fix it
I am unable to plot this due some error since yesterday.

from sklearn.model_selection import train_test_split
import numpy as np
X = reviews['Text']
y= reviews['Score'].values
X_train, X_test, y_train, y_test =train_test_split(X,y ,test_size=0.20,stratify=y,random_state=33)

checking the shape of split of data

print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)
(80000,) (80000,)
(20000,) (20000,)

#plot bar graphs of y_train and y_test

import matplotlib.pyplot as plt
plt.bar([1,0],y_train.value_counts().values,color ='green')

 
plt.xlabel("Count")
plt.ylabel("y_train values")
plt.title("Distribution of y_train")
plt.show()

error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-18-62460aedca56> in <module>()
      1 #plot bar graphs of y_train and y_test
      2 import matplotlib.pyplot as plt
----> 3 plt.bar(y_train.value_counts().values,color ='green')
      4 
      5 plt.xlabel("Count")

AttributeError: 'numpy.ndarray' object has no attribute 'value_counts'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫相离 2025-02-15 14:34:32

问题出在 y =评论['Score']。值的情况下,根据文档返回数据框的数字表示。

您正在尝试在numpy类型上调用此方法,该方法未由numpy提供。 value_counts 方法由PANDAS库支持DataFrames。

尝试将代码更改为以下内容,并且可能有效:
y =评论['Scorce']

y的类型更改为 pandas.core.series.series.series ,您可以调用以下代码块。

api/pandas.dataframe.values.html

The problem occurs with y = reviews['Score'].values, according to the documentation it returns a Numpy representation of the DataFrame.

You are trying to call this method on a Numpy type, which is not provided by numpy. The value_counts method is supported by Pandas library for DataFrames.

Try to change your code to the following and it might work:
y = reviews['Score']

The type of y changed to pandas.core.series.Series and you might able to call your following code blocks.

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.values.html
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.value_counts.html?highlight=value_counts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文