python - Numpy matmul - 当前不支持对象数组

标签 python numpy

当我在代码中的某行调用 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.ndarrays 相乘,它们的大小正确。我在执行 matmul(array([2]),array([2])) 时与我正在做的事情有什么不同?

谢谢!!

最佳答案

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

data = data.astype(float)

关于python - Numpy matmul - 当前不支持对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51521063/

相关文章:

python - 时间作为 Python 字典的关键

javascript - 自然语言处理数据库查询

python - 从 Alpine Linux Docker 连接到 MS SQL 时遇到问题

python - 如何将数据框中拼写错误的字符串值匹配并替换为第二个数据框中拼写正确的字符串值?

python - numpy 中矩阵的 One-hot 表示

python - 获取另一个 numpy 数组中一个 numpy 数组的索引

python - 如何在 django 中包装管理 View ?

java - Python 中是否有类似 Java IllegalStateException 的东西?

python - Pandas 在 groupby 中按条件过滤

python - Numpy 中的行索引