I am working with a large array with around 300 000 values. It has 100 000 rows and 3 columns. 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. 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:
import numpy as np
N = np.asarray(N)
N[N > 10] = 0
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.
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]])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.