对于具有相同维度的两个矩阵,pandas 矩阵点积失败

标签 pandas dot-product

我刚刚收到此错误,但我不知道原因是什么。可以帮忙看看吗?

这两个数据帧都是 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/

相关文章:

python - 绘制排除 pandas 或 matplotlib 中缺失值的图表

python - tensorflow 中两个向量的点积

python-3.x - 来自 Flask 中 FileStorage 的 Pandas read_csv

pandas - 如何旋转 pandas 数据框?

r - 计算数据框中所有列对之间的点积

python - 具有 3D 向量的两个 numpy 数组的点积

algorithm - 3D 空间 : following the direction that an object is pointing towards, 使用鼠标指针

c - 如何优化点积的 AVX 实现?

python - 按另一个索引的顺序对 Pandas Dataframe 进行排序

python - pandas 中时间戳错误 : dtype ('<m8[D]' ) 的两个连续行之间的天数