使用numpy pandas 进行矩阵运算 [英] matrix operation using numpy pandas
问题描述
我正在尝试测试使用numpy和熊猫
在3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:32:08 UTC 2012 i686 i686 i686 GNU/Linux
上与python 2.7.3, numpy 1.9.2 and pandas 0.15.2
对于这个小例子:
ds1 = [[ 4, 13, 6, 9],
[ 7, 12, 5, 7],
[ 7, 0, 4, 22],
[ 9, 8, 12, 0]]
ds2 = [[ 4, 1],
[ 5, 3],
[ 6, 1],
[ 7, 2],
[ 8, 2],
[ 9, 3],
[12, 1],
[13, 2],
[22, 3]]
ds1= pd.DataFrame(ds1)
ds2= pd.DataFrame(ds2)
C = np.where(ds1.values.ravel()[:, None] == ds2.values[:, 0])
print C
给出错误的结果
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14]),
array([ 0, 7, 2, 5, 3, 6, 1, 3, 3, 0, 8, 5, 4, 6]))
预期输出为
output = [[1, 2, 1, 3],
[2, 1, 3, 2],
[2, 0, 1, 3],
[3, 2, 1, 0]]
并且在处理较大的矩阵值时
and while working with large matrix values
ds1 = pd.read_table('https://gist.githubusercontent.com/karimkhanp/9527bad750fbe75e072c/raw/ds1', sep=' ', header=None)
ds2 = pd.read_table('https://gist.githubusercontent.com/karimkhanp/1692f1f76718c35e939f/raw/6f6b348ab0879b702e1c3c5e362e9d2062e9e9bc/ds2', header=None, sep=' ')
C = np.where(ds1.values.ravel()[:, None] == ds2.values[:, 0])
print C
给出
(1000, 1001) (4000, 2)
(array([], dtype=int32),)
而不是替换后的矩阵值.
instead of the replaced matrix value.
任何建议都会很有帮助.
Any suggestion would be much helpful.
推荐答案
我同意@Anthony Lethuillier的回答,我只是猜测IndexError
可能是由不同版本引起的.似乎是@nlper的情况,C
是(array([], dtype=int32),)
,这意味着在ds1.values.ravel()[:, None] == ds2.values[:, 0]
中什么都没有发现,这显然与@Anthony的不同.找不到任何内容,因此C
是仅包含1个元素的元组,因此在访问C[1]
时会触发IndexError
.
I agree with @Anthony Lethuillier 's answer and I just guess the IndexError
may be caused by different version. It seem's in @nlper 's situation, C
is (array([], dtype=int32),)
which means nothing found in ds1.values.ravel()[:, None] == ds2.values[:, 0]
, and this is obviously different from @Anthony 's. Nothing found, thus C
is a tuple which only contains 1 element, so an IndexError
is triggered when you accessing C[1]
.
这在我的机器上也可以使用,所以我不知道为什么C
为空.我建议您详细打印ds1.values.ravel()
和ds2.values[:, 0]
并查看为什么没有相等的内容.
This also works on my machine so I don't know why C
is empty. I recommend you to print ds1.values.ravel()
and ds2.values[:, 0]
in detail and see why nothing equals.
此外,我使用python 2.7.9,numpy 1.9.2和pandas 0.16.1
Besides, I use python 2.7.9, numpy 1.9.2 and pandas 0.16.1
这篇关于使用numpy pandas 进行矩阵运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!