代码之家  ›  专栏  ›  技术社区  ›  Marcos Galletero Romero

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

  •  2
  • Marcos Galletero Romero  · 技术社区  · 7 年前

    当我在代码中的某一行调用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]))时,我所做的事情有什么不同?

    谢谢您!!

    1 回复  |  直到 7 年前
        1
  •  7
  •   blue_note    7 年前

    问题是numpy认为数组包含 物体 数字 (那是 dtype=object 参数)。你需要通过做一些类似的事情来改变他们

    data = data.astype(float)