首页 > 解决方案 > Numpy matmul - 当前不支持对象数组

问题描述

当我在代码中的某行调用 np.matmul 时出现此错误。这是我在解决 python 调试器中的错误时得到的信息:

> /home/marcos/Desktop/Machine_Learning_for_Alzheimer/createDataset.py(185)nonlinear_expand()
-> rsrvr_input = matmul(src_dataset, array(input_weights.todense()))
(Pdb) type(src_dataset)
<class 'numpy.ndarray'>
(Pdb) type(array(input_weights.todense()))
<class 'numpy.ndarray'>
(Pdb) matmul(array([2]),array([2]))
4
(Pdb) src_dataset.shape
(59, 36045)
(Pdb) array(input_weights.todense()).shape
(36045, 5000)
(Pdb) src_dataset
array([[0.34718266129493713, 0.43048959970474243, 0.31317993998527527,   ...,
    0.08795060217380524, 0.4226779639720917, 0.12640206515789032],
   [0.333339124917984, 0.30553877353668213, 0.33829280734062195, ...,
    0.1962795853614807, 0.5640064477920532, 0.13812369108200073],
   [0.31267049908638, 0.29710978269577026, 0.3062109649181366, ...,
    0.23275987803936005, 0.5658718943595886, 0.18722198903560638],
   ..., 
   [0.3225921392440796, 0.38019028306007385, 0.29140061140060425, ...,
    0.20768669247627258, 0.43566110730171204, 0.13893568515777588],
   [0.33491265773773193, 0.5155439972877502, 0.3620935082435608, ...,
    0.09752997010946274, 0.18645673990249634, 0.09302680939435959],
   [0.31085047125816345, 0.3607252836227417, 0.32846760749816895, ...,
    0.18291841447353363, 0.4967271387577057, 0.17849059402942657]], dtype=object)
(Pdb) type(array([2]))
<class 'numpy.ndarray'>
(Pdb) matmul(src_dataset, array(input_weights.todense()))
*** TypeError: Object arrays are not currently supported

所以,问题就在于此。我清楚地将两个 numpy.ndarray 相乘,它们的大小正确。与我做 matmul(array([2]),array([2])) 时所做的事情有什么不同?

谢谢!!

标签: pythonnumpy

解决方案


问题是您的数组被 numpy 视为包含对象而不是数字(这是dtype=object参数)。您需要通过执行类似的操作来转换它们

data = data.astype(float)

推荐阅读