在 Pandas 中使用向量获取数据帧的点积,并返回数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15753916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get dot-product of dataframe with vector, and return dataframe, in Pandas
提问by Amelio Vazquez-Reina
I am unable to find the entry on the method dot()in the official documentation. However the method is there and I can use it. Why is this?
我无法dot()在官方文档中找到有关该方法的条目。但是方法在那里,我可以使用它。为什么是这样?
On this topic, is there a way compute an element-wise multiplication of every row in a data frame with another vector? (and obtain a dataframeback?), i.e. similar to dot()but rather than computing the dot product, one computes the element-wise product.
在这个主题上,有没有办法计算数据框中每一行与另一个向量的元素乘法?(并获得dataframe返回?),即类似于dot()但不是计算点积,而是计算元素乘积。
回答by Adam Hughes
mulis doing essentially an outer-product, while dotis an inner product. Let me expand on the accepted answer:
mul本质上是在做外积,而dot是内积。让我扩展已接受的答案:
In [13]: df = pd.DataFrame({'A': [1., 1., 1., 2., 2., 2.], 'B': np.arange(1., 7.)})
In [14]: v1 = np.array([2,2,2,3,3,3])
In [15]: v2 = np.array([2,3])
In [16]: df.shape
Out[16]: (6, 2)
In [17]: v1.shape
Out[17]: (6,)
In [18]: v2.shape
Out[18]: (2,)
In [24]: df.mul(v2)
Out[24]:
A B
0 2 3
1 2 6
2 2 9
3 4 12
4 4 15
5 4 18
In [26]: df.dot(v2)
Out[26]:
0 5
1 8
2 11
3 16
4 19
5 22
dtype: float64
So:
所以:
df.multakes matrix of shape (6,2) and vector (6, 1) and returns matrix shape (6,2)
df.mul接受形状为 (6,2) 的矩阵和向量 (6, 1) 并返回矩阵形状 (6,2)
While:
尽管:
df.dottakes matrix of shape (6,2) and vector (2,1) and returns (6,1).
df.dot采用形状为 (6,2) 的矩阵和向量 (2,1) 并返回 (6,1)。
These are not the same operation, they are outer and inner products, respectively.
这些不是相同的操作,它们分别是外积和内积。
回答by unutbu
Here is an example of how to multiply a DataFrame by a vector:
以下是如何将 DataFrame 乘以向量的示例:
In [60]: df = pd.DataFrame({'A': [1., 1., 1., 2., 2., 2.], 'B': np.arange(1., 7.)})
In [61]: vector = np.array([2,2,2,3,3,3])
In [62]: df.mul(vector, axis=0)
Out[62]:
A B
0 2 2
1 2 4
2 2 6
3 6 12
4 6 15
5 6 18
回答by Russ Clarke
It's quite hard to say with a degree of accuracy.
很难准确地说出来。
Often, a method exists and is undocumented because it's considered internal by the vendor, and may be subject to change.
通常,一种方法存在并且没有记录,因为它被供应商认为是内部的,并且可能会发生变化。
It could, of course, be a simple oversight by the folks who put together the documentation.
当然,这可能是整理文档的人的简单疏忽。
Regarding your second question; I don't really know about that - but it might be better to make a new S/O question for it. Just scanning the the API, could you do something with the DataFrame's .applymap(function) feature ?
关于你的第二个问题;我真的不知道 - 但最好为它提出一个新的 S/O 问题。只是扫描 API,你能用 DataFrame 的 .applymap(function) 功能做点什么吗?

