attributeError:' numpy.ndarray'对象没有属性' value_counts'
我正在尝试使用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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在
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