- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试创建已排序的 numpy 数组的差异,这样如果我记录第一行的值和差异,我可以重新创建原始表但存储较少的数据。
下面是表格的示例:
my_array = numpy.array([(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1),
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2),
(9, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 34),
(9, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 35),
(9, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36)
],'uint8,uint8,uint8,uint8,uint8,uint8,uint8,uint8,uint8,uint8,uint8,uint8,uint8,uint8')
在运行 numpy.diff(my_array) 之后,我会期待这样的事情:
[(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1),
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1),
(9, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 32),
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1),
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
]
Note: The data above comes from the first & last three rows of the 'real' data, which is much much larger. With the full dataset, most of the rows after a diff would be 0,0,0,0,0,0,0,0,0,0,0,0,1 -- which can a) be stored in a much smaller struct, and b) will compress fantastically well on disk since most rows contain very similar data.
I should probably point out that the reason I have a whole bunch of uint8's in the first place, is because I needed to store an array of extremely large numbers, in the smallest amount of memory possible. The largest number was 185439173519100986733232011757860, which is too big for uint64. In fact, the smallest number of bits to store it would be 108 bits, or 14 bytes (to the nearest byte). So to fit these large numbers into numpy, i use the following two functions:
def large_number_to_numpy(number,columns):
return tuple((number >> (8*x)) & 255 for x in range(columns-1,-1,-1))
def numpy_to_large_number(numbers):
return sum([y << (8*x) for x,y in enumerate(numbers[::-1])])Which is used like this:
>>> large_number_to_numpy(185439173519100986733232011757860L,14)
(9L, 36L, 146L, 73L, 36L, 146L, 73L, 36L, 146L, 73L, 36L, 146L, 73L, 36L)
numpy_to_large_number((9L, 36L, 146L, 73L, 36L, 146L, 73L, 36L, 146L, 73L, 36L, 146L, 73L, 36L))
185439173519100986733232011757860LWith the array created like this:
my_array = numpy.zeros(TOTAL_ROWS,','.join(14*['uint8']))
And then populated with:
my_array[x] = large_number_to_numpy(large_number,14)
但我得到的是:
>>> my_array
array([(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1),
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2),
(9, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 34),
(9, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 35),
(9, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36)],
dtype=[('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'u1'), ('f4', 'u1'), ('f5', 'u1'), ('f6', 'u1'), ('f7', 'u1'), ('f8', 'u1'), ('f9', 'u1'), ('f10', 'u1'), ('f11', 'u1'), ('f12', 'u1'), ('f13', 'u1')])
>>> numpy.diff(my_array)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/numpy/lib/function_base.py", line 1567, in diff
return a[slice1]-a[slice2]
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype([('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'u1'), ('f4', 'u1'), ('f5', 'u1'), ('f6', 'u1'), ('f7', 'u1'), ('f8', 'u1'), ('f9', 'u1'), ('f10', 'u1'), ('f11', 'u1'), ('f12', 'u1'), ('f13', 'u1')]) dtype([('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'u1'), ('f4', 'u1'), ('f5', 'u1'), ('f6', 'u1'), ('f7', 'u1'), ('f8', 'u1'), ('f9', 'u1'), ('f10', 'u1'), ('f11', 'u1'), ('f12', 'u1'), ('f13', 'u1')]) dtype([('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'u1'), ('f4', 'u1'), ('f5', 'u1'), ('f6', 'u1'), ('f7', 'u1'), ('f8', 'u1'), ('f9', 'u1'), ('f10', 'u1'), ('f11', 'u1'), ('f12', 'u1'), ('f13', 'u1')])
最佳答案
问题是您有一个结构化数组而不是常规二维数组,因此 numpy
不知道如何从一个元组中减去另一个元组。
将结构化数组转换为常规数组 ( from this SO question ):
my_array = my_array.view(numpy.uint8).reshape((my_array.shape[0], -1))
然后执行 numpy.diff(my_array, axis=0)
。
或者,如果可以的话,通过将 my_array
定义为
numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
[9, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 34],
[9, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 35],
[9, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36, 146, 73, 36]],
dtype=numpy.uint8)
关于python - Numpy 和 diff(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38102566/
我在我们的系统中使用了多种不同的查询,我们想要获取分页的“总”记录。 我所遵循的所有地方都遵循这种结构 - var query1 = "select SQL_CALC_FOUND_ROWS ...."
我是 diff 的新手。我过去用过它。但我想知道,是否可以使用 diff 来跟踪希伯来语中两个文件之间的差异?我想比较具有元音点和重音符号的圣经希伯来文文件。 最佳答案 我不知道有任何“差异”(文件比
在Word中,有一个选项可以比较两个文档并找出两个文档之间的差异。我正在寻找一个类似的功能来比较两个Powerpoint文档。理想情况下,它将列出不同的单个幻灯片,每个幻灯片之间版本之间的差异以及进行
Bizzaro-Diff!!! 有没有办法做一个只显示一组文件中相同部分的 bizzaro/inverse-diff? (即远超过 three files ) 奇怪的问题,我知道...但我正在将某人的
我将 git 与默认的 Ubuntu 12.04 软件包一起使用: git --version git version 1.7.9.5 我找不到任何可以让 git diff 打开外部差异工具的机制,它
这是我的树 ├── test │ ├── dir1 │ └── dir2 │ ├── file │ └── file2 └── test2 └── dir2
我在 source forge ( cocoa.diff ) 上找到了这个补丁,这意味着我可以使用 cocoa.diff 文件进行补丁。但是,我似乎无法弄清楚如何使用 .diff 文件。 谢谢你的帮助
我修改了某个文件的第 494 行,并使用 cvs diff -u4 来查看我修改的内容,cvs 输出如下内容: @@ -490,9 +490,9 @@ if (!(hPtr->hSta
我需要知道这两个补丁是否有效相同。 我有一个旧补丁文件和使用 unix diff 命令创建的新补丁文件。由于补丁创建时的时间戳,只是比较补丁会报告差异。 有没有办法(有差异?)可以可靠地告诉我这两个补
我有这样的东西 src/sim/simulate.cc 41d40 public: > 61,62c60,61 a.patch 用于创建补丁文件,尽管也可能会抛出一些其他开关(-N?)
我想知道是否有工具可以显示与 debian 打包相关的补丁中使用的 *.diff 文件。我需要从该工具中获得的是它可以只读取 diff 文件并显示随更改的行更改的实际文件,就像 kdiff 或 mel
主要有3个git diff版本: git diff - difference between WORKING DIRECTORY & STAGE git diff --staged - differe
根据 Ender 的 Applied Econometric Time Series ,变量 y 的二阶差分定义为: Pandas 提供了 diff 函数,它接收“periods”作为参数。尽管如此,
这是我的差异的开始部分。 #!/usr/bin/env python import fileinput import difflib import subprocess import sys # fo
有没有办法在 svn diff 或任何其他工具(基于 linux)到 仅显示空格/制表符更改 ?. 目的,我不希望 checkin 这些差异。如果工具可以捕获这些差异,我可以在 checkin 之前将
我正在尝试比较忽略回车符的文件 - diff -b 在任何其他 unix 上的表现令人钦佩。但是在这个 AIX 5.3 机器上: tst1:tst2$ od -c testfile 00000
这个问题已经有答案了: What does the “@@…@@” meta line with at signs in svn diff or git diff mean? (3 个回答) 已关闭
我在使用 git diff --word-diff 时遇到了问题。问题是当 diff 获取没有换行符的文件(单行文件)时,它会逐行区分。我想逐字区分。 以下是当我在没有换行符的情况下比较文件时发生的情
这个问题在这里已经有了答案: Highlight changed lines and changed bytes in each changed line (12 个答案) 关闭 7 年前。 给定一
我正在尝试使用两个大文件夹(〜7GB)创建一个补丁。 这是我的做法: $ diff -Naurbw . ../other-folder > file.patch 但可能由于文件大小而导致未创建补丁并给
我是一名优秀的程序员,十分优秀!