我需要找到一个独特的行numpy.array.
例如:
>>> a # I have
array([[1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 0, 0],
[1, 1, 1, 0, 0, 0],
[1, 1, 1, 1, 1, 0]])
>>> new_a # I want to get to
array([[1, 1, 1, 0, 0, 0],
[0, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 0]])
Run Code Online (Sandbox Code Playgroud)
我知道我可以在阵列上创建一个集合并循环,但我正在寻找一个有效的纯numpy解决方案.我相信有一种方法可以将数据类型设置为void然后我可以使用numpy.unique,但我无法弄清楚如何使其工作.
matplotlib.pyplot和matplotlib.pylab有什么区别?
哪种用法更受青睐?
我有点困惑,因为它似乎独立于我导入,我可以做同样的事情.我错过了什么?
我有一个2D NumPy数组,并希望将其中的所有值替换为大于或等于阈值T的255.0.据我所知,最基本的方式是:
shape = arr.shape
result = np.zeros(shape)
for x in range(0, shape[0]):
for y in range(0, shape[1]):
if arr[x, y] >= T:
result[x, y] = 255
Run Code Online (Sandbox Code Playgroud)
什么是最简洁和pythonic的方式来做到这一点?
有没有更快(可能不那么简洁和/或更少pythonic)的方式来做到这一点?
这将是用于人体头部的MRI扫描的窗口/水平调整子程序的一部分.2D numpy数组是图像像素数据.
我正在使用numpy.我有一个包含1列和N行的矩阵,我想从N个元素中获取一个数组.
例如,如果我有M = matrix([[1], [2], [3], [4]]),我想得到A = array([1,2,3,4]).
为了实现它,我使用A = np.array(M.T)[0].有谁知道更优雅的方式来获得相同的结果?
谢谢!
我有一个简单的问题,但无法找到一个好的解决方案.
我想拍摄一个代表灰度图像的numpy 2D数组,并在应用一些matplotlib色彩图时将其转换为RGB PIL图像.
我可以使用以下pyplot.figure.figimage命令获得合理的PNG输出:
dpi = 100.0
w, h = myarray.shape[1]/dpi, myarray.shape[0]/dpi
fig = plt.figure(figsize=(w,h), dpi=dpi)
fig.figimage(sub, cmap=cm.gist_earth)
plt.savefig('out.png')
Run Code Online (Sandbox Code Playgroud)
虽然我可以调整它以获得我想要的东西(可能使用StringIO来获取PIL图像),我想知道是否有更简单的方法来做到这一点,因为它似乎是一个非常自然的图像可视化问题.让我们说,像这样:
colored_PIL_image = magic_function(array, cmap)
Run Code Online (Sandbox Code Playgroud)
谢谢阅读!
python numpy matplotlib color-mapping python-imaging-library
在Matplotlib中,我按如下方式制作虚线网格线:
fig = pylab.figure()
ax = fig.add_subplot(1,1,1)
ax.yaxis.grid(color='gray', linestyle='dashed')
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何(或者甚至可能)使网格线在其他图形元素(如条形图)后面绘制.更改添加网格与添加其他元素的顺序没有区别.
是否有可能使网格线出现在其他所有的后面?
我使用外部模块(libsvm),它不支持numpy数组,只支持元组,列表和dicts.但是我的数据是2d numpy数组.我怎样才能将它转换为pythonic方式,又称没有循环.
>>> import numpy
>>> array = numpy.ones((2,4))
>>> data_list = list(array)
>>> data_list
[array([ 1., 1., 1., 1.]), array([ 1., 1., 1., 1.])]
>>> type(data_list[0])
<type 'numpy.ndarray'> # <= what I don't want
# non pythonic way using for loop
>>> newdata=list()
>>> for line in data_list:
... line = list(line)
... newdata.append(line)
>>> type(newdata[0])
<type 'list'> # <= what I want
Run Code Online (Sandbox Code Playgroud) 是否存在numpy中用于计算两个矩阵之间的均方误差的方法?
我试过搜索但没找到.它的名字不同吗?
如果没有,你怎么克服这个?你自己写的还是使用不同的lib?
我想加载彩色图像,将其转换为灰度,然后反转文件中的数据.
我需要的是:在OpenCV中迭代数组并使用此公式更改每个值(这可能是错误的但对我来说似乎是合理的):
img[x,y] = abs(img[x,y] - 255)
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么它不起作用:
def inverte(imagem, name):
imagem = abs(imagem - 255)
cv2.imwrite(name, imagem)
def inverte2(imagem, name):
for x in np.nditer(imagem, op_flags=['readwrite']):
x = abs(x - 255)
cv2.imwrite(name, imagem)
if __name__ == '__main__':
nome = str(sys.argv[1])
image = cv2.imread(nome)
gs_imagem = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
inverte(gs_imagem, "invertida.png")
inverte2(gs_imagem, "invertida2.png")
Run Code Online (Sandbox Code Playgroud)
我不想做一个明确的循环(我试图更加pythonic).我可以看到,在一张白色背景的图像中,它变成了黑色,但只有这一点看起来不像其他颜色有很多(如果有的话)变化.