我刚刚收到此错误,但我不知道原因是什么。可以帮忙看看吗?
这两个数据帧都是 4 x 4。我不明白为什么它们无法获得点积。 np.dot
将在这里工作。谢谢。
df1
i1 i2 i3 i4
0 1 0.00000 0.000000 0.000000e+00
1 0 0.85935 0.080730 2.948807e-07
2 0 0.00000 0.832259 9.775291e-02
3 0 0.00000 0.002086 8.392081e-01
df2
i1 i2 i3 i4
0 1 0.00000 0.000000 0.000000e+00
1 0 0.83891 0.090042 3.365395e-07
2 0 0.00000 0.809407 1.080944e-01
3 0 0.00000 0.001577 8.176372e-01
df1.点(df2)
---------------------------------------------------------------------------
ValueError
<ipython-input-329-95627ce5e4d3> in <module>()
----> 1 df1.dot(df2)
packages\pandas\core\frame.pyc in dot(self, other)
786 if (len(common) > len(self.columns) or
787 len(common) > len(other.index)):
--> 788 raise ValueError('matrices are not aligned')
789
790 left = self.reindex(columns=common, copy=False)
ValueError: matrices are not aligned
最佳答案
您需要在此处添加T
(Pandas 的点基于索引和列)
df1.dot(df2.T)
Out[634]:
0 1 2 3
0 1.0 0.000000 0.000000 0.000000
1 0.0 0.728186 0.065343 0.000128
2 0.0 0.074938 0.684203 0.081239
3 0.0 0.000188 0.092402 0.686171
关于对于具有相同维度的两个矩阵,pandas 矩阵点积失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48771306/