Numpy for循环每次都会产生不同的结果

时间:2021-01-12 21:22:21

First time publishing in here, here it goes:

第一次在这里发布,这里是:

I have two sets of data(v and t), each one has 46 values. The data is imported with "pandas" module and coverted to a numpy array in order to do the calculation.

我有两组数据(v和t),每组有46个值。使用“pandas”模块导入数据并将其转换为numpy数组以进行计算。

I need to set ml_min1[45], ml_min2[45], and so on to the value "0". The problem is that each time I ran the script, the values corresponding to the position 45 of ml_min1 and ml_min2 are different. This is the piece of code that I have:

我需要将ml_min1 [45],ml_min2 [45]等设置为值“0”。问题是每次运行脚本时,对应于ml_min1和ml_min2的位置45的值都不同。这是我的一段代码:

t1 = fil_copy.t1.as_matrix()
t2 = fil_copy.t2.as_matrix()
v1 = fil_copy.v1.as_matrix()
v2 = fil_copy.v2.as_matrix()

ml_min1 = np.empty(len(t1))
l_h1 = np.empty(len(t1))

ml_min2 = np.empty(len(t2))
l_h2 = np.empty(len(t2))

for i in range(0, (len(v1) - 1)):

    if (i != (len(v1) - 1)) and (v1[i+1] > v1[i]):
        ml_min1[i] = v1[i+1] - v1[i]
        l_h1[i] = ml_min1[i] * (60/1000)
    elif i == (len(v1)-1):
        ml_min1[i] = 0
        l_h1[i] = 0
        print(i, ml_min1[i])
    else:
        ml_min1[i] = 0
        l_h1[i] = 0
        print(i, ml_min1[i])

for i in range(0, (len(v2) - 1)):

    if (i != (len(v2) - 1)) and (v2[i+1] > v2[i]):
        ml_min2[i] = v2[i+1] - v2[i]
        l_h2[i] = ml_min2[i] * (60/1000)
    elif i == (len(v2)-1):
        ml_min2[i] = 0
        l_h2[i] = 0
        print(i, ml_min2[i])
    else:
        ml_min2[i] = 0
        l_h2[i] = 0
        print(i, ml_min2[i])

1 个解决方案

#1


2  

Your code as it is currently written doesn't work because the elif blocks are never hit, since range(0, x) does not include x (it stops just before getting there). The easiest way to solve this is probably just to initialize your output arrays with numpy.zeros rather than numpy.empty, since then you don't need to do anything in the elif and else blocks (you can just delete them).

当前编写的代码不起作用,因为elif块永远不会被命中,因为范围(0,x)不包括x(它在到达之前停止)。解决这个问题最简单的方法可能只是用numpy.zeros而不是numpy.empty初始化你的输出数组,因为那时你不需要在elif和else块中做任何事情(你可以删除它们)。

That said, it's generally a design error to use loops like yours in numpy code. Instead, you should use numpy's broadcasting features to perform your mathematical operations to a whole array (or a slice of one) at once.

也就是说,在numpy代码中使用像你这样的循环通常是一个设计错误。相反,您应该使用numpy的广播功能一次对整个数组(或一个切片)执行数学运算。

If I understand correctly, the following should be equivalent to what you wanted your code to do (just for one of the arrays, the other should work the same):

如果我理解正确,以下内容应该等同于您希望代码执行的操作(仅适用于其中一个数组,另一个应该相同):

ml_min1 = np.zeros(len(t1)) # use zeros rather than empty, so we don't need to assign any 0s
diff = v1[1:] - v1[:-1]     # find the differences between all adjacent values (using slices)
mask = diff > 0             # check which ones are positive (creates a Boolean array)
ml_min1[:-1][mask] = diff[mask]   # assign with mask to a slice of the ml_min1 array
l_h1 = ml_min1 * (60/1000)  # create l_h1 array with a broadcast scalar multiplication

#1


2  

Your code as it is currently written doesn't work because the elif blocks are never hit, since range(0, x) does not include x (it stops just before getting there). The easiest way to solve this is probably just to initialize your output arrays with numpy.zeros rather than numpy.empty, since then you don't need to do anything in the elif and else blocks (you can just delete them).

当前编写的代码不起作用,因为elif块永远不会被命中,因为范围(0,x)不包括x(它在到达之前停止)。解决这个问题最简单的方法可能只是用numpy.zeros而不是numpy.empty初始化你的输出数组,因为那时你不需要在elif和else块中做任何事情(你可以删除它们)。

That said, it's generally a design error to use loops like yours in numpy code. Instead, you should use numpy's broadcasting features to perform your mathematical operations to a whole array (or a slice of one) at once.

也就是说,在numpy代码中使用像你这样的循环通常是一个设计错误。相反,您应该使用numpy的广播功能一次对整个数组(或一个切片)执行数学运算。

If I understand correctly, the following should be equivalent to what you wanted your code to do (just for one of the arrays, the other should work the same):

如果我理解正确,以下内容应该等同于您希望代码执行的操作(仅适用于其中一个数组,另一个应该相同):

ml_min1 = np.zeros(len(t1)) # use zeros rather than empty, so we don't need to assign any 0s
diff = v1[1:] - v1[:-1]     # find the differences between all adjacent values (using slices)
mask = diff > 0             # check which ones are positive (creates a Boolean array)
ml_min1[:-1][mask] = diff[mask]   # assign with mask to a slice of the ml_min1 array
l_h1 = ml_min1 * (60/1000)  # create l_h1 array with a broadcast scalar multiplication