形状不匹配:在绘制条形图的同时,无法广播对象

发布于 2025-02-08 19:05:11 字数 2198 浏览 1 评论 0原文

我正在尝试将y_train和y_test数据的图形绘制,但是我会得到价值不匹配错误。有人可以帮助我修复它吗?

#Read the dataset - Amazon fine food reviews
reviews = pd.read_csv(r"Reviews.csv")
#get only 2 columns - Text, Score
#drop the NAN values
reviews = reviews[['Text', 'Score']]
reviews.head()
reviews = reviews.dropna()
reviews.drop(reviews[reviews['Score'] == 3].index, inplace = True)
reviews.loc[reviews['Score'] <= 2, 'Score'] = 0
reviews.loc[reviews['Score'] > 3, 'Score'] = 1
reviews
#remove HTML from the Text column and save in the Text column only
from bs4 import BeautifulSoup
#reviews['Text'] = reviews['Text'].str.replace(r'<[^<>]*>', '', regex=True)
reviews['Text'] = [BeautifulSoup(Text,"lxml").get_text() for Text in reviews['Text'] ]
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(reviews['Text'],reviews['Score'] , test_size=0.20,stratify=reviews['Score'],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.figure(figsize = (10, 5))
plt.bar(y_train), y_test), color ='maroon')

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-6ec38d3b391c> in <module>()
      2 import matplotlib.pyplot as plt
      3 plt.figure(figsize = (10, 5))
----> 4 plt.bar(np.array(y_train), np.array(y_test), color ='maroon')
      5 

4 frames
<__array_function__ internals> in broadcast_arrays(*args, **kwargs)

/usr/local/lib/python3.7/dist-packages/numpy/lib/stride_tricks.py in _broadcast_shape(*args)
    418     # use the old-iterator because np.nditer does not handle size 0 arrays
    419     # consistently
--> 420     b = np.broadcast(*args[:32])
    421     # unfortunately, it cannot handle 32 or more arguments directly
    422     for pos in range(32, len(args), 31):

ValueError: shape mismatch: objects cannot be broadcast to a single shape

I am trying to bar plot for y_train and y_test data, But i am getting value mismatch error. Can someone help me fix it.

#Read the dataset - Amazon fine food reviews
reviews = pd.read_csv(r"Reviews.csv")
#get only 2 columns - Text, Score
#drop the NAN values
reviews = reviews[['Text', 'Score']]
reviews.head()
reviews = reviews.dropna()
reviews.drop(reviews[reviews['Score'] == 3].index, inplace = True)
reviews.loc[reviews['Score'] <= 2, 'Score'] = 0
reviews.loc[reviews['Score'] > 3, 'Score'] = 1
reviews
#remove HTML from the Text column and save in the Text column only
from bs4 import BeautifulSoup
#reviews['Text'] = reviews['Text'].str.replace(r'<[^<>]*>', '', regex=True)
reviews['Text'] = [BeautifulSoup(Text,"lxml").get_text() for Text in reviews['Text'] ]
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(reviews['Text'],reviews['Score'] , test_size=0.20,stratify=reviews['Score'],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.figure(figsize = (10, 5))
plt.bar(y_train), y_test), color ='maroon')

Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-6ec38d3b391c> in <module>()
      2 import matplotlib.pyplot as plt
      3 plt.figure(figsize = (10, 5))
----> 4 plt.bar(np.array(y_train), np.array(y_test), color ='maroon')
      5 

4 frames
<__array_function__ internals> in broadcast_arrays(*args, **kwargs)

/usr/local/lib/python3.7/dist-packages/numpy/lib/stride_tricks.py in _broadcast_shape(*args)
    418     # use the old-iterator because np.nditer does not handle size 0 arrays
    419     # consistently
--> 420     b = np.broadcast(*args[:32])
    421     # unfortunately, it cannot handle 32 or more arguments directly
    422     for pos in range(32, len(args), 31):

ValueError: shape mismatch: objects cannot be broadcast to a single shape

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文