【问题标题】:Covert to sparse matrix - TypeError: no supported conversion for types: (dtype('0'),)隐蔽到稀疏矩阵 - TypeError:不支持类型转换:(dtype('0'),)
【发布时间】:2019-08-09 16:49:59
【问题描述】:

我正在尝试这样做:

  features = csr_matrix(features)

其中features<class 'numpy.ndarray'>,看起来像这样:

[[51 1]
 [49 2]
 [47 2]
 ...
 [2 6]
 [20 2]
 [16 1]]

但我收到以下错误:

TypeError: no supported conversion for types: (dtype('O'),)

这个错误是关于什么的,我该如何解决?

【问题讨论】:

  • 你为什么对这样的数据使用稀疏?我没有看到很多零。这个features 对象(数组?)到底应该代表什么?
  • @hpaulj,感谢您的提问。我使用稀疏数据是因为stackoverflow.com/questions/57362560/… 如果有意义的话。 features 只是一些(密集)特征以及 tf-idf 稀疏特征。

标签: python numpy scipy sparse-matrix


【解决方案1】:

这样做:

csr_matrix(features.astype(np.float))

如果这有错误,那么您的特征中就有一些不是数字的东西。

【讨论】:

    【解决方案2】:

    您可以重新定义您的 numpy 数组,指定 dtypeexplicitly

    features = np.array(features, dtype=float)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 2021-11-25
      • 2017-07-02
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      相关资源
      最近更新 更多