maxframe.dataframe.DataFrame.dot#

DataFrame.dot(other)#

计算 DataFrame 与 other 之间的矩阵乘法。

此方法计算 DataFrame 与另一个 Series、DataFrame 或 numpy 数组的值之间的矩阵乘积。

在 Python >= 3.5 中,也可以使用 self @ other 调用此方法。

参数:

other (Series, DataFrame or array-like) -- 用于计算矩阵乘积的另一个对象。

返回:

如果 other 是一个 Series,则返回 self 与 other 的矩阵乘积作为一个 Series。如果 other 是一个 DataFrame 或 numpy.array,则返回 self 与 other 的矩阵乘积,结果为一个 DataFrame 或 np.array。

返回类型:

Series or DataFrame

参见

Series.dot

Series 的类似方法。

备注

为了计算矩阵乘法,DataFrame 和 other 的维度必须兼容。此外,DataFrame 的列名和 other 的索引必须包含相同的值,因为它们将在乘法之前对齐。

Series 的 dot 方法计算的是内积,而不是这里的矩阵乘积。

示例

这里我们将一个 DataFrame 与一个 Series 相乘。

>>> import maxframe.tensor as mt
>>> import maxframe.dataframe as md
>>> df = md.DataFrame([[0, 1, -2, -1], [1, 1, 1, 1]])
>>> s = md.Series([1, 1, 2, 1])
>>> df.dot(s).execute()
0    -4
1     5
dtype: int64

这里我们将一个 DataFrame 与另一个 DataFrame 相乘。

>>> other = md.DataFrame([[0, 1], [1, 2], [-1, -1], [2, 0]])
>>> df.dot(other).execute()
    0   1
0   1   4
1   2   2

注意,dot 方法给出的结果与 @ 相同

>>> (df @ other).execute()
    0   1
0   1   4
1   2   2

如果 other 是一个 np.array,dot 方法也可以工作。

>>> arr = mt.array([[0, 1], [1, 2], [-1, -1], [2, 0]])
>>> df.dot(arr).execute()
    0   1
0   1   4
1   2   2

注意对象的打乱不会改变结果。

>>> s2 = s.reindex([1, 0, 2, 3])
>>> df.dot(s2).execute()
0    -4
1     5
dtype: int64