numpy优化 [英] Numpy optimization

查看:125
本文介绍了numpy优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根据情况分配值的函数.我的数据集大小通常在30-50k范围内.我不确定这是否是使用numpy的正确方法,但是当数字超过5k时,它会变得很慢.有没有更好的方法可以使其更快?

I have a function that assigns value depending on the condition. My dataset size is usually in the range of 30-50k. I am not sure if this is the correct way to use numpy but when it's more than 5k numbers, it gets really slow. Is there a better way to make it faster ?

import numpy as np 
N = 5000; #dataset size
L = N/2;
d=0.1; constant = 5;

x=constant+d*np.random.random(N);

matrix = np.zeros([L,N]);

print "Assigning matrix"
for k in xrange(L):
    for i in xrange(k+1):
        matrix[k,i] = random.random()

    for i in xrange(k+1,N-k-1):
        if ( x[i] > x[i-k-1] ) and ( x[i] > x[i+k+1] ):
            matrix[k,i] = 0
        else:
            matrix[k,i] = random.random()

    for i in xrange(N-k-1,N):
        matrix[k,i] = random.random()

推荐答案

如果使用for循环,您将失去numpy的速度.提高速度的方法是使用numpys函数和矢量化操作.有没有一种方法可以创建随机矩阵:

If you are using for loops, you are going to lose the speed in numpy. The way to get speed is to use numpys functions and vectorized operations. Is there a way you can create a random matrix:

matrix = np.random.randn(L,k+1)

然后对这个矩阵做一些操作以获得所需的0位置?您能否详细说明将条目设置为0的条件?例如,您可以制作矩阵,然后执行以下操作:

Then do something to this matrix to get the 0's positioned you want? Can you elaborate on the condition for setting an entry to 0? For example, you can make the matrix then do:

matrix[matrix > value]

保留高于阈值的所有值.如果该条件可以表示为某些布尔索引器或算术运算,则可以加快该速度.如果必须在for循环中(即,取决于循环周期周围的值),则可能无法向量化.

To retain all values above a threshold. If the condition can be expressed as some boolean indexer or arithmetic operation, you can speed it up. If it has to be in the for loop (ie it depends on the values surrounding it as the loop cycles) it may not be able to be vectorized.

这篇关于numpy优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆