我正在通过一组概率从多项分布生成绘制向量probs
,其中每个绘制是probs
选择的条目的索引:
import numpy as np
def sample_mult(K, probs):
result = np.zeros(num_draws, dtype=np.int32)
for n in xrange(K):
draws = np.random.multinomial(1, probs)
result[n] = np.where(draws == 1)[0][0]
return result
Run Code Online (Sandbox Code Playgroud)
这可以加快吗?np.random.multinomial
一遍又一遍地调用似乎效率低下(也np.where
可能很慢).
timeit
说 The slowest run took 6.72 times longer than the fastest. This could mean that an intermediate result is being cached
100000 loops, best of 3: 18.9 µs per loop
有没有一种快速有效的方法来查找NxM Numpy数组中具有最高值的每一列中的行?
我目前正在通过Python中的嵌套循环执行此操作,这比较慢:
from PIL import Image
import numpy as np
img = Image.open('sample.jpg').convert('L')
width, height = size = img.size
y = np.asarray(img.getdata(), dtype=np.float64).reshape((height, width))
max_rows = [0]*width
for col_i in xrange(y.shape[1]):
max_vaue, max_row = max([(y[row_i][col_i], row_i) for row_i in xrange(y.shape[0])])
max_rows[col_i] = max_row
Run Code Online (Sandbox Code Playgroud)
对于640x480的图像,这大约需要5秒钟。虽然不是很大,但是在Numpy / PIL / C中完全实现的更复杂的图像操作(如模糊)需要0.01秒或更短的时间。我正在尝试对视频流执行此操作,因此这是一个巨大的瓶颈。如果不编写自己的C扩展名,如何加快速度?
是否可以通过一次调用numpy获得numpy.argmin和numpy.amin的结果?谢谢.
对于大小为60000行,10列的2D数组,我有如下阵列
[0 0 0 0 0 1 0 0 0 0]
[1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 1]
.......
Run Code Online (Sandbox Code Playgroud)
任何行只包含一个'1'
我必须将它减少到一个行或列向量,它显示我们有一个1的索引.例如,对于上面显示的行,我们必须最终得到
[6,1,10...]
超过第60,000个值.
如何在没有循环的Matlab中做到这一点?
我有以下numpy数组matrix
,
matrix = np.zeros((3,5), dtype = int)
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
Run Code Online (Sandbox Code Playgroud)
假设我有这个numpy的阵列indices
,以及
indices = np.array([[1,3], [2,4], [0,4]])
array([[1, 3],
[2, 4],
[0, 4]])
Run Code Online (Sandbox Code Playgroud)
问题:如何将1
s 分配给数组matrix
指定索引的元素indices
.期望矢量化实现.
为了更清晰,输出应如下所示:
array([[0, 1, 0, 1, 0], #[1,3] elements are changed
[0, 0, 1, 0, 1], #[2,4] elements are changed
[1, 0, 0, 0, 1]]) #[0,4] elements are changed
Run Code Online (Sandbox Code Playgroud) 我目前有一个(1631160,78)
np 数组作为神经网络的输入。我想尝试使用 LSTM,它需要 3D 结构作为输入数据。我目前正在使用以下代码来生成所需的 3D 结构,但速度非常慢(预计到达时间 > 1 天)。有没有更好的方法用 numpy 来做到这一点?
我当前生成数据的代码:
def transform_for_rnn(input_x, input_y, window_size):
output_x = None
start_t = time.time()
for i in range(len(input_x)):
if i > 100 and i % 100 == 0:
sys.stdout.write('\rTransform Data: %d/%d\tETA:%s'%(i, len(input_x), str(datetime.timedelta(seconds=(time.time()-start_t)/i * (len(input_x) - i)))))
sys.stdout.flush()
if output_x is None:
output_x = np.array([input_x[i:i+window_size, :]])
else:
tmp = np.array([input_x[i:i+window_size, :]])
output_x = np.concatenate((output_x, tmp))
print
output_y = input_y[window_size:]
assert len(output_x) == len(output_y)
return output_x, output_y
Run Code Online (Sandbox Code Playgroud) 我想从矩阵中选择不同数量的第一个元素.数字在数组中指定.结果是一维数组.例如:
a = np.arange(25).reshape([5, 5])
numbers = np.array([3, 2, 0, 1, 2])
Run Code Online (Sandbox Code Playgroud)
我想要这个结果:
[0, 1, 2, 5, 6, 15, 20, 21]
Run Code Online (Sandbox Code Playgroud)
没有for循环.
我有一个 3-d Numpy 数组flow
,如下所示:
flow = np.random.uniform(low=-1.0, high=1.0, size=(720,1280,2))
# Suppose flow[0] are x-coordinates. flow[1] are y-coordinates.
Run Code Online (Sandbox Code Playgroud)
需要计算每个 x,y 点的角度。这是我如何实施它:
def calcAngle(a):
assert(len(a) == 2)
(x, y) = a
# angle_deg = 0
angle_deg = np.angle(x + y * 1j, deg=True)
return angle_deg
fangle = np.apply_along_axis(calcAngle, axis=2, arr=flow)
# The above statement takes 14.0389318466 to execute
Run Code Online (Sandbox Code Playgroud)
每个点的角度计算需要14.0389318466 seconds
在我的 Macbook Pro 上执行。
有没有一种方法可以加快速度,可能是通过使用一些矩阵运算,而不是一次处理一个像素。
我想在井字游戏中检查所有可能的获胜者条件,我该如何以功能性方式重写?
board = numpy.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
player = 1
if any([(board[0, :] == player).all(),
(board[:, 0] == player).all(),
(board[1, :] == player).all(),
(board[:, 1] == player).all(),
(board[2, :] == player).all(),
(board[:, 2] == player).all()]):
print('Win')
Run Code Online (Sandbox Code Playgroud) 我有一个二进制数组,我想根据它们重复的长度翻转值.举个例子
Ar = [0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1];
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想翻转仅重复2次或更少次数的1,从而产生以下结果.
Ar = [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1];
Run Code Online (Sandbox Code Playgroud)
numpy ×8
python ×7
performance ×5
arrays ×3
matlab ×2
scipy ×2
indexing ×1
lstm ×1
optimization ×1
reduction ×1