def plot_LSA(test_data, test_labels, savepath="PCA_demo.csv", plot=True):
lsa = TruncatedSVD(n_components=2) # Truncated SVD works on term count/tf-idf matrices as returned by the vectorizers in sklearn.feature_extraction.text. In that context, it is known as latent semantic analysis (LSA).
lsa.fit(np.array(test_data).reshape(1,1118))
lsa_scores = lsa.transform(np.array(test_data).reshape(1,1118))
color_mapper = {label:idx for idx,label in enumerate(set(test_labels))}
color_column = [color_mapper[label] for label in test_labels]
print ('colormapper=',color_mapper)
#print ('colorColumn=',color_column)
colors = ['blue','green','red']
if plot:
plt.scatter(lsa_scores[:,0], lsa_scores[:,1], s=8, alpha=.8, c=test_labels, cmap=matplotlib.colors.ListedColormap(colors))
red_patch = mpatches.Patch(color='red', label='Negative')
blue_patch = mpatches.Patch(color='blue', label='Neutral')
green_patch = mpatches.Patch(color='green', label='Positive')
plt.legend(handles=[red_patch, green_patch, blue_patch], prop={'size': 30})
```fig = plt.figure(figsize=(16, 16))
plot_LSA(X_train, y_train)
plt.show()
ValueError Traceback (most recent call last)
<ipython-input-101-f45ff1a9f7db> in <module>
22
23 fig = plt.figure(figsize=(5, 5))
---> 24 plot_LSA(X_train, y_train)
25 plt.show()
26
<ipython-input-101-f45ff1a9f7db> in plot_LSA(test_data, test_labels, savepath, plot)
7 def plot_LSA(test_data, test_labels, savepath="PCA_demo.csv", plot=True):
8 lsa = TruncatedSVD(n_components=2) # Truncated SVD works on term count/tf-idf matrices as returned by the vectorizers in sklearn.feature_extraction.text. In that context, it is known as latent semantic analysis (LSA).
----> 9 lsa.fit(np.array(test_data).reshape(1,1118))
10 lsa_scores = lsa.transform(np.array(test_data).reshape(1,1118))
11 color_mapper = {label:idx for idx,label in enumerate(set(test_labels))}
ValueError: cannot reshape array of size 128764 into shape (1,1118)
<Figure size 360x360 with 0 Axes>
之前出现的问题是python是1D array,没有办法显示2D array,然后我按照网上的指导解决了,但是以上错误不知道怎么修改,麻烦大家给我一点建议。谢谢!