简体   繁体   English

在数组中查找值并有效地更改它们

[英]Find values in array and change them efficiently

I am working with a large array with around 300 000 values. 我正在处理大约30万个值的大型数组。 It has 100 000 rows and 3 columns. 它具有10万行和3列。 I am doing iterations with this array and if any value in the first column exceeds a limit of lets say 10, I want the number to be replaced. 我正在使用此数组进行迭代,如果第一列中的任何值超过了可以说10的限制,我希望替换该数字。 Is there any more efficient way than running something like this?: 有没有比运行类似这样更有效的方法?:

for i in range(N):
    if array[i][0]>10:
        array[i][0] = 0 

I need to repeat this sequense for the other two columns as well which included with all my other iterations makes my code pretty slow. 我还需要对其他两列重复此顺序,所有其他迭代中都包含该顺序,这会使我的代码运行缓慢。

Convert your array to a numpy array ( numpy.asarray ) then to replace the values you would use the following: 将您的数组转换为numpy数组( numpy.asarray ),然后使用以下内容替换值:

import numpy as np
N = np.asarray(N)

N[N > 10] = 0

numpy.asarray documentation numpy.asarray文档

I've assumed you may not want to use the same threshold/replacement value for each column. 我假设您可能不想为每列使用相同的阈值/替换值。 That being the case, you can pack the three items in a list of tuples and iterate through that. 在这种情况下,您可以将三个项目打包在一个元组列表中并进行遍历。

import numpy as np

arr = np.ndarray(your_array)
#Edited with your values, and a more than symbol
threshold = 10
column_id = 0
replace_value = 0
arr[arr[:, column_id] > threshold, column_id] = replace_value

Set threshold , column_id and replace_value as you require. 根据需要设置thresholdcolumn_idreplace_value

If I understand you correctly you`re looking for something like that: 如果我对您的理解正确,那么您正在寻找类似的东西:

>>> from numpy import array
>>> a = array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> a[a>5]=10       # <--- here the "magic" happens
>>> a
array([[ 1,  2,  3],
       [ 4,  5, 10],
       [10, 10, 10]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM