Savvy在英国 2020-10-16 05:03 采纳率: 33.3%
浏览 112
已采纳

麻烦大家帮我debug一个reshape array的问题

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,然后我按照网上的指导解决了,但是以上错误不知道怎么修改,麻烦大家给我一点建议。谢谢!
  • 写回答

1条回答 默认 最新

  • 忧王 2020-10-16 10:46
    关注

    ?什么意思,128754个元素要怎么转成 1 x 1118的格式啊?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题