例如,我有一个ndarray是:
a = np.array([1, 3, 5, 7, 2, 4, 6, 8])
Run Code Online (Sandbox Code Playgroud)
现在我想a分成两部分,一部分是所有数字<5,另一部分是> = 5:
[array([1,3,2,4]), array([5,7,6,8])]
Run Code Online (Sandbox Code Playgroud)
当然,我可以遍历a并创建两个新阵列.但我想知道numpy提供了一些更好的方法吗?
类似地,对于多维数组,例如
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[2, 4, 7]])
Run Code Online (Sandbox Code Playgroud)
我想根据第一列<3和> = 3拆分它,结果是:
[array([[1, 2, 3],
[2, 4, 7]]),
array([[4, 5, 6],
[7, 8, 9]])]
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法而不是遍历它?谢谢.
当我正在阅读PIL文档时,我发现了一个奇怪的声明.
在1.1.6及更高版本中,load返回一个像素访问对象,可用于读取和修改像素.访问对象的行为类似于二维数组,因此您可以执行以下操作:
Run Code Online (Sandbox Code Playgroud)pix = im.load() print pix[x, y] pix[x, y] = value
pix[x, y]这里的意思是什么?这不是切片语法,因为,使用而不是:.
我是 Python 图像处理的新手,遇到了一个奇怪的问题。
例如,我有一个 2*2 的黑白位图图像,其像素如下:
黑,白
白色 黑色
使用 PIL 并将其转换为 numpy:
>>> import Image
>>> import numpy
>>> im = Image.open('test.bmp')
>>> im
<BmpImagePlugin.BmpImageFile image mode=1 size=2x2 at 0x95A54EC>
>>> numpy.asarray(im)
array([[ True, True],
[False, False]], dtype=bool)
>>>
Run Code Online (Sandbox Code Playgroud)
让我感到困惑的是数组中像素的顺序。为什么不[[True, False], [False, True]]呢?谢谢。
更新:位图在这里:http : //njuer.us/clippit/test.bmp
我对导入工作有点困惑.假设:
package/
__init__.py
file1.py
Run Code Online (Sandbox Code Playgroud)
在__init__.py:
from file1 import AClass
__version__ = '1.0'
Run Code Online (Sandbox Code Playgroud)
在file1.py:
Class AClass(object):
def bar():
# I want to use __version__here, but don't want to pass
# it through the constructor. Is there any way?
pass
Run Code Online (Sandbox Code Playgroud)
如果我使用from . import __version__中file1.py它只是说ImportError: cannot import name __version__.
我做了一个dtype:
mytype = np.dtype([('a',np.uint8), ('b',np.uint8), ('c',np.uint8)])
Run Code Online (Sandbox Code Playgroud)
所以使用这个dtype的数组:
test1 = np.zeros(3, dtype=mytype)
Run Code Online (Sandbox Code Playgroud)
test1是:
array([(0, 0, 0), (0, 0, 0), (0, 0, 0)],
dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])
Run Code Online (Sandbox Code Playgroud)
现在我有test2:
test2 = np.array([[1,2,3], [4,5,6], [7,8,9]])
Run Code Online (Sandbox Code Playgroud)
当我使用时test2.astype(mytype),结果不是我想要的结果:
array([[(1, 1, 1), (2, 2, 2), (3, 3, 3)],
[(4, 4, 4), (5, 5, 5), (6, 6, 6)],
[(7, 7, 7), (8, 8, 8), (9, 9, 9)]],
dtype=[('a', '|u1'), ('b', '|u1'), ('c', '|u1')])
Run Code Online (Sandbox Code Playgroud)
我希望结果如下:
array([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
dtype=[('a', …Run Code Online (Sandbox Code Playgroud) 我正在做一项关于通过抖动将灰度图像转换为1位二进制图像的任务.我正在尝试一个简单的4x4矩阵,使图像比原始图像大16倍.
dithering_matrix = array([[ 0, 8, 2, 10],
[12, 4, 14, 6],
[ 3, 11, 1, 9],
[15, 7, 13, 5]], dtype=uint8)
split_num = dithering_matrix.size + 1
Run Code Online (Sandbox Code Playgroud)
我读了一个512x512图像到imndarray并做了以下事情:
output = list()
for row in im:
row_output = list()
for pixel in row:
pixel_matrix = ((pixel / (256 / split_num)) > dithering_matrix) * 255
row_output.append(pixel_matrix)
output.append( hstack( tuple(row_output) ) )
output_matrix = vstack( tuple(output) )
Run Code Online (Sandbox Code Playgroud)
我发现输出需要8-10秒,我认为im上面的循环花了很多时间.在某些软件中,通常在闪存中完成相同的操作.那么有可能提高效率吗?
更新:@Ignacio Vazquez-Abrams我不熟悉探查器:(我试过cProfile,结果很奇怪.
1852971 function calls (1852778 primitive calls) in 9.127 seconds …Run Code Online (Sandbox Code Playgroud)