This question already has answers here:
How to return all the minimum indices in numpy
(5 answers)
Closed 1 year ago.
I have an array for example:
[1 2 3 4
2 3 4 0
5 4 0 6]
And I want to find the indexes of all the values that are closer to to the value 3.9 (in my example,4)
I tried using :
import numpy as np
def find_nearest(array, value):
idx = (np.abs(array - value)).argmin()
and
np.where(array== array.min())
but none of the options gives me the correct answer.
I do excpect to get:
(3,1),(1,2),(2,1)
In my original code, I iterate an array with the shape of 3648X5472 so "FOR" loops might be too heavy.
hope to get some help here, thank you
You can use:
a = np.array([[1, 2, 3, 4],
[2, 3, 4, 0],
[5, 4, 0, 6]])
v = 3.9
b = abs(a-v)
xs, ys = np.where(b == np.min(b))
output:
>>> xs
array([0, 1, 2])
>>> ys
array([3, 2, 1])
Alternative output:
>>> np.c_[np.where(b == np.min(b))]
array([[0, 3],
[1, 2],
[2, 1]])
# or
>>> np.argwhere(b==np.min(b))
You are pretty close. When you found the index of the first closest value, you can find indices of the equal values with `np.argwhere:
closest = array[np.abs(array - value).argmin()]
np.argwhere(array == closest)
Is there any program possible with a complexity less than O(mn)? The input is in the form as the first line contains MN and the next M lines each containing N integers
For example
4 4
1 0 3 4
0 0 0 0
4 0 6 8
4 0 2 4
The output should be:
1 3 4
4 6 8
4 2 4
You can do this by individually filtering rows and columns with all values equal to 1 but checking if set(row or column)!={0}
arr = [[1, 0, 3, 4],
[0, 0, 0, 0],
[4, 0, 6, 8],
[4, 0, 2, 4]]
rows = [i for i in arr if set(i)!={0}]
cols = [i for i in zip(*rows) if set(i)!={0}]
arr_new = [list(i) for i in zip(*cols)]
print(arr_new)
[[1, 3, 4],
[4, 6, 8],
[4, 2, 4]]
EDIT:
If you are ok with using numpy then you can do this a bit more easily -
import numpy as np
arr = np.array(arr)
arr[~(arr==0).all(0)][:,~(arr==0).all(1)]
array([[1, 3, 4],
[4, 6, 8],
[4, 2, 4]])
I have a array of [2 2 3 4 4 5 6 6 6], and want to delete all minimum values from it.
The output should be [3 4 4 5 6 6 6].
I tried like following code, but it deleted a single 2, and left [2 3 4 4 5 6 6 6].
import numpy as np
a = np.array([2,2,3,4,4,5,6,6,6])
b= np.delete(a, a.argmin())
Python has a built-in function for finding min
val = min(values)
b =[v for v in values if v != val]
Use Boolean or “mask” index arrays
Numpy: Indexing
min() or not to a.min() depends on the array size.
For small arrays, use python built-in min
For large arrays, use numpy min
Test the timing for your particular usage.
b = a[a > min(a)]
[b]:
array([3, 4, 4, 5, 6, 6, 6])
The size of numpy arrays is immutable but you can create a copy of this array using this simple oneliner:
arr = np.array([2, 2, 3, 4, 4, 5, 6, 6, 6])
arr[arr!=np.min(arr)]
Output:
array([3, 4, 4, 5, 6, 6, 6])
You can get the indices that are greater than minimum value and slice the array as in this answer.
out = a[a>a.min()]
Notice this is faster than using np.delete as explained in the linked answer.
This question already has answers here:
Interweaving two numpy arrays
(13 answers)
Closed 4 years ago.
I'm trying to interleave arrays as below.
import numpy as np
x = np.array([1,2,3,4,5])
y = np.array([4,6,2,6,9],[5,9,8,7,4],[3,2,5,4,9])
Desired result:
[[1,2,3,4,5],[4,6,2,6,9],[1,2,3,4,5],[5,9,8,7,4],[1,2,3,4,5],[3,2,5,4,9]]
Is there an elegant way to do this?
This is my the way I wrote, but I was looking to improve this line. data=np.array([x,y[0],x,y[1],x,y[2]]) Any other way to write this?
x=np.array([1,2,3,4,5])
y=np.array([[4,6,2,6,9],[5,9,8,7,4],[3,2,5,4,9]])
data=np.array([x,y[0],x,y[1],x,y[2]])
print(data)
You can try to use np.insert
import numpy as np
x = np.array([1,2,3,4,5])
y = np.array([[4,6,2,6,9],[5,9,8,7,4],[3,2,5,4,9]])
np.insert(y, obj=(0, 1, 2), values=x, axis=0)
array([[1, 2, 3, 4, 5],
[4, 6, 2, 6, 9],
[1, 2, 3, 4, 5],
[5, 9, 8, 7, 4],
[1, 2, 3, 4, 5],
[3, 2, 5, 4, 9]])
(0, 1, 2) refers to the indexes in y that you would like to insert into before insertion.
EDIT : One can use obj=range(y.shape[0]) for arbitrary length of y. Thanks for Chiel's suggestion.
Please see the tutorial for more information.
Adapted from the answer https://stackoverflow.com/a/5347492/7505395 to Interweaving two numpy arrays:
import numpy as np
x=np.array([1,2,3,4,5])
y=np.array([[4,6,2,6,9],[5,9,8,7,4],[3,2,5,4,9]]) # fixed missing []
x_enh = np.array([x]*len(y)) # blow up x to y size
c = np.empty((y.size * 2,), dtype=y.dtype).reshape(y.size,5) # create correctly sized empty
c[0::2] = x_enh # interleave using 2 steps starting on 0
c[1::2] = y # interleave using 2 steps starting on 1
print(c)
Output:
[[1 2 3 4 5]
[4 6 2 6 9]
[1 2 3 4 5]
[5 9 8 7 4]
[1 2 3 4 5]
[3 2 5 4 9]]
This question already has answers here:
Indexing one array by another in numpy
(4 answers)
Closed 6 years ago.
consider the array a
np.random.seed([3,1415])
a = np.random.choice(np.arange(8), (2, 4), False)
print(a)
[[7 1 4 5]
[6 2 3 0]]
I'll create another array b that holds the results of np.argsort along each row.
b = a.argsort(1)
print(b)
[[1 2 3 0]
[3 1 2 0]]
I want to produce the sorted version of a by doing an appropriate slice of a with b. It should look like this
idx0 = np.arange(a.shape[0]).repeat(a.shape[1])
print(a[idx0, b.ravel()].reshape(a.shape))
[[1 4 5 7]
[0 2 3 6]]
question
what is the appropriate way to slice an 2 x 4 array with another 2 x 4 array in the fashion described above?
Advanced-indexing for the help -
a[np.arange(b.shape[0])[:,None],b]
Sample run -
In [10]: a
Out[10]:
array([[7, 1, 4, 5],
[6, 2, 3, 0]])
In [11]: b
Out[11]:
array([[1, 2, 3, 0],
[3, 1, 2, 0]])
In [12]: a[np.arange(b.shape[0])[:,None],b]
Out[12]:
array([[1, 4, 5, 7],
[0, 2, 3, 6]])