Covert to sparse matrix - TypeError: no supported conversion for types: (dtype('0'),)

Covert to sparse matrix - TypeError: no supported conversion for types: (dtype('0'),)

我正在尝试这样做:

  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'),)

这是什么错误,我该如何解决?

这样做:

csr_matrix(features.astype(np.float))

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

您可以重新定义您的 numpy 数组,明确指定 dtype

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