小编Cli*_*pit的帖子

如何根据numpy中的条件拆分数组?

例如,我有一个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)

有没有更好的方法而不是遍历它?谢谢.

python numpy

18
推荐指数
1
解决办法
2万
查看次数

pix [x,y]在Python中意味着什么

当我正在阅读PIL文档时,我发现了一个奇怪的声明.

在1.1.6及更高版本中,load返回一个像素访问对象,可用于读取和修改像素.访问对象的行为类似于二维数组,因此您可以执行以下操作:

pix = im.load()
print pix[x, y]
pix[x, y] = value
Run Code Online (Sandbox Code Playgroud)

pix[x, y]这里的意思是什么?这不是切片语法,因为,使用而不是:.

python syntax tuples

7
推荐指数
1
解决办法
1075
查看次数

如何在PIL和numpy之间转换模式为“1”的图像?

我是 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

python numpy python-imaging-library

6
推荐指数
1
解决办法
6552
查看次数

如何导入__init__.py中定义的变量?

我对导入工作有点困惑.假设:

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__.

python

5
推荐指数
2
解决办法
1818
查看次数

如何在numpy中将ndarray的dtype更改为自定义的?

我做了一个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)

python numpy recarray

4
推荐指数
1
解决办法
481
查看次数

如何提高这个numpy迭代的效率?

我正在做一项关于通过抖动将灰度图像转换为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)

python numpy image-processing python-imaging-library

2
推荐指数
1
解决办法
493
查看次数