我写了一些非常基本的代码,以便练习使用matmul函数,即使它尽可能简单也行不通。
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(0,100,size=(4999, 1128)))
tab = np.random.rand(1128)
np.matmul(df,tab)
它给出以下错误:
Traceback (most recent call last):
File "/Users/jk/Documents/programowanie19/movie_recommendation/venv/lib/python3.7/site-packages/pandas/core/internals/managers.py", line 1651, in create_block_manager_from_blocks
placement=slice(0, len(axes[0])))]
File "/Users/jk/Documents/programowanie19/movie_recommendation/venv/lib/python3.7/site-packages/pandas/core/internals/blocks.py", line 3095, in make_block
return klass(values, ndim=ndim, placement=placement)
File "/Users/jk/Documents/programowanie19/movie_recommendation/venv/lib/python3.7/site-packages/pandas/core/internals/blocks.py", line 87, in __init__
'{mgr}'.format(val=len(self.values), mgr=len(self.mgr_locs)))
ValueError: Wrong number of items passed 1, placement implies 1128
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test2.py", line 9, in <module>
np.matmul(df,tab)
File "/Users/jk/Documents/programowanie19/movie_recommendation/venv/lib/python3.7/site-packages/pandas/core/generic.py", line 1909, in __array_wrap__
return self._constructor(result, **d).__finalize__(self)
File "/Users/jk/Documents/programowanie19/movie_recommendation/venv/lib/python3.7/site-packages/pandas/core/frame.py", line 424, in __init__
copy=copy)
File "/Users/jk/Documents/programowanie19/movie_recommendation/venv/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 167, in init_ndarray
return create_block_manager_from_blocks([values], [columns, index])
File "/Users/jk/Documents/programowanie19/movie_recommendation/venv/lib/python3.7/site-packages/pandas/core/internals/managers.py", line 1660, in create_block_manager_from_blocks
construction_error(tot_items, blocks[0].shape[1:], axes, e)
File "/Users/jk/Documents/programowanie19/movie_recommendation/venv/lib/python3.7/site-packages/pandas/core/internals/managers.py", line 1691, in construction_error
passed, implied))
ValueError: Shape of passed values is (4999, 1), indices imply (4999, 1128)
两个矩阵的尺寸应匹配- df为(4999,1128),而制表符为(1128,1)。有人知道这是什么问题吗?