形状不匹配:在绘制条形图的同时,无法广播对象
我正在尝试将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论