【发布时间】:2015-08-01 04:39:22
【问题描述】:
我正在尝试测试Matrix search operation using numpy and pandas 上给出的相同示例
在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]]
在处理大矩阵值时
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),)
而不是替换的矩阵值。
任何建议都会很有帮助。
【问题讨论】:
标签: python numpy matrix pandas