我有:
>>> a
array([[1, 2],
[3, 4]])
>>> type(l), l # list of scalers
(<type 'list'>, [0, 1])
>>> type(i), i # a numpy array
(<type 'numpy.ndarray'>, array([0, 1]))
>>> type(j), j # list of numpy arrays
(<type 'list'&g
来自Matlab/Octave的背景,我一直在努力学习numpy。一件让我一次又一次地被绊倒的事情是向量和多维数组之间的区别。对于这个问题,我会给出一个具体的问题,但是如果有人也能解释一下numpy中一维数组背后的更一般的图片,为什么你首先想要它们,如何避免在混合单维数组和多维数组时遇到麻烦,等等,我会非常感激。总之,问题是:
我有一个叫做X的二维数组:
X = numpy.arange(10).reshape(2,5)
我想把X的最后一列存储成另一个二维数组(也就是一个列向量),叫做Y。我唯一能想到的方法是:
Y = numpy.atleast_2d(X[:,4]).T
但我不喜欢这样有几个
样本代码:
import numpy as np
a = np.zeros((5,5))
a[[0,1]] = 1 #(list of indices)
print('results with list based indexing\n', a)
a = np.zeros((5,5))
a[(0,1)] = 1 #(tuple of indices)
print('results with tuple based indexing\n',a)
结果:
results with list based indexing
[[ 1. 1. 1.
现在,我正在尝试理解索引numpy结构化数组的可能方法,而且我有点被它困住了。仅举几个简单的例子:
import numpy as np
arr = np.array(zip(range(5), range(5, 10)), dtype=[('a', int), ('b', int)])
arr[0] # first row (record)
arr[(0,)] # the same, as expected
arr['a'] # field 'a' of each record
arr[('a',)] # &
我有个Numpy张量器 X = np.arange(64).reshape((4,4,4)) 我想要获取这个张量的第一维的2,3,4项,你可以这样做, Y = X[[1,2,3],:,:] 这是不是比显式地写出索引1,2,3更简单的方式呢?我尝试了像1,:这样的东西,它给我一个错误。 上下文:对于我的实际应用程序,张量的形状类似于(30000,100,100)。我想抓住这个张量的最后一个(10000,100,100)到(30000,100,100)。
作为联合的ML代理的一部分,提供给强化学习代理的图像可以转换为灰度,如下所示:
def _process_pixels(image_bytes=None, bw=False):
s = bytearray(image_bytes)
image = Image.open(io.BytesIO(s))
s = np.array(image) / 255.0
if bw:
s = np.mean(s, axis=2)
s = np.reshape(s, [s.shape[0], s.shape[1], 1])
return s