I am trying to change the value in np array if there is a match
For example, a np array a and its shape is (50,4)
a.shape#(50,4)
I need to check if the np array a has this value [0,1,20,4] in the 1st axis, then I need to change that into [-1,-1,-1,-1].
I tried this format -
a[a==[0,1,20,4]]=[-1,-1,-1,-1]
But, this is not working,
How to do this modification?
This solves it for random array of shape (50,4)
b = np.random.randint(5,size=(50,4))
c = np.equal(b,[4,1,2,3])
ai = np.array(c.all(axis=1).nonzero())
np.put_along_axis(b,ai,[-1,-1,-1,-1],axis=0)
change [4,1,2,3] to [0,1,20,4]
Maybe try doing row-wise comparison with np.tile and np.argwhere like this:
import numpy as np
# Create dummy data
x = np.random.randint(29, size=(49, 4))
x = np.concatenate([[[0,1,20,4]], x])
print('Before --> \n', x)
# Compare row-wise
x[np.argwhere(np.all(x==np.reshape(np.tile([0,1,20,4], reps=x.shape[0]), x.shape),axis=1))] = [-1,-1,-1,-1]
print('After --> \n', x)
Before -->
[[ 0 1 20 4]
[ 1 17 5 2]
[19 8 24 17]
[ 1 23 16 3]
[ 0 15 0 20]
[14 23 9 23]
[ 1 27 5 27]
[15 24 24 17]
[ 2 28 8 4]
[26 26 6 10]
[18 13 5 28]
[10 25 18 15]
[ 6 17 8 2]
[ 4 26 26 15]
[18 16 18 24]
[ 0 11 15 22]
[20 27 0 0]
[ 9 22 16 2]
[22 11 8 23]
[21 10 6 23]
[14 16 0 10]
[14 27 22 9]
[ 4 0 10 15]
[12 0 28 25]
[ 8 28 9 28]
[12 3 26 24]
[23 3 26 25]
[ 0 6 16 4]
[ 1 20 1 19]
[11 5 9 11]
[20 15 18 5]
[25 0 17 27]
[20 24 3 19]
[13 12 17 4]
[ 1 13 25 22]
[27 10 11 18]
[ 4 5 12 8]
[11 19 17 15]
[26 7 3 10]
[21 14 27 21]
[26 12 8 13]
[27 8 1 17]
[20 28 0 20]
[ 1 12 11 16]
[ 4 0 3 22]
[11 12 3 8]
[15 24 3 8]
[ 4 23 17 20]
[21 1 23 12]
[ 0 27 3 22]
[15 5 17 28]]
After -->
[[-1 -1 -1 -1]
[ 1 17 5 2]
[19 8 24 17]
[ 1 23 16 3]
[ 0 15 0 20]
[14 23 9 23]
[ 1 27 5 27]
[15 24 24 17]
[ 2 28 8 4]
[26 26 6 10]
[18 13 5 28]
[10 25 18 15]
[ 6 17 8 2]
[ 4 26 26 15]
[18 16 18 24]
[ 0 11 15 22]
[20 27 0 0]
[ 9 22 16 2]
[22 11 8 23]
[21 10 6 23]
[14 16 0 10]
[14 27 22 9]
[ 4 0 10 15]
[12 0 28 25]
[ 8 28 9 28]
[12 3 26 24]
[23 3 26 25]
[ 0 6 16 4]
[ 1 20 1 19]
[11 5 9 11]
[20 15 18 5]
[25 0 17 27]
[20 24 3 19]
[13 12 17 4]
[ 1 13 25 22]
[27 10 11 18]
[ 4 5 12 8]
[11 19 17 15]
[26 7 3 10]
[21 14 27 21]
[26 12 8 13]
[27 8 1 17]
[20 28 0 20]
[ 1 12 11 16]
[ 4 0 3 22]
[11 12 3 8]
[15 24 3 8]
[ 4 23 17 20]
[21 1 23 12]
[ 0 27 3 22]
[15 5 17 28]]
For example for a random matrix
x = np.random.randint(1,11,size=(10,4))
>> array([[10, 1, 7, 7],
[ 3, 1, 3, 2],
[ 8, 10, 2, 9],
[ 4, 6, 4, 4],
[ 1, 4, 3, 5],
[10, 1, 5, 4],
[ 7, 10, 8, 8],
[ 5, 10, 8, 3],
[ 6, 5, 5, 3],
[ 7, 2, 5, 7]])
Check all rows that are equal to target row
rows_cond = np.all(x == [1,4,3,5], axis=1)
this returns a boolean array that can be used to modify the desired rows
x[rows_cond,:] = [-1,-1,-1,-1]
I want to convert an array of shape (a, b, c, d) into a shape (b*c*d, a).
The problem is the output of Array A and B is different:
x = np.array([np.arange(1,37)]) #1D array of 1 to 36.
a = 3
b = 2
c = 2
d = 3
Array = np.reshape(x, (a, b, c, d)) #shape (3, 2, 2, 3)
A = poop.reshape(a, b*c*d).T
B = poop.reshape(b*c*d, a)
print("Array shape:" + str(Array.shape))
print(Array)
print("#####")
print("Array A:")
print(A)
print("#####")
print("Array B:")
print(B)
I would of thought that Numpy's reshape takes (row, column) as input, so reversing then transposing would lead to the same result (column, row).T.
Changing the "Order" parameter does not seem to make array B match array A.
Thank you.
The output is:
Array shape:(3, 2, 2, 3)
[[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
[[[13 14 15]
[16 17 18]]
[[19 20 21]
[22 23 24]]]
[[[25 26 27]
[28 29 30]]
[[31 32 33]
[34 35 36]]]]
#####
Array A:
[[ 1 13 25]
[ 2 14 26]
[ 3 15 27]
[ 4 16 28]
[ 5 17 29]
[ 6 18 30]
[ 7 19 31]
[ 8 20 32]
[ 9 21 33]
[10 22 34]
[11 23 35]
[12 24 36]]
#####
Array B:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]
[13 14 15]
[16 17 18]
[19 20 21]
[22 23 24]
[25 26 27]
[28 29 30]
[31 32 33]
[34 35 36]]
This question already has an answer here:
Create a copy and not a reference of a NumPy array
(1 answer)
Closed 2 years ago.
I trying to sort a tuple as below
input: ROI:
[[191 60 23 18]
[143 60 23 19]
[ 95 52 24 21]
[237 51 24 21]
[ 47 38 27 22]
[281 35 25 22]
[ 4 17 26 24]
[324 13 22 21]]
Expected Output = S_ROI:
[[4 17 26 24]
[47 38 27 22]
[ 95 52 24 21]
[143 60 23 19]
[ 191 60 23 18]
[237 51 24 21]
[281 35 25 22]
[324 13 22 21]]
I have got intermediate array
column=[191 143 95 237 47 281 4 324]
I have tried this - But ROI is getting updated inside loop
sort_index = np.argsort(column)
column.sort()
sorted_led_ROI=ROI;
index=0
for y in sort_index:
sorted_led_ROI[index]=ROI[y]
index =index+1
print('sorted_led_ROI:', sorted_led_ROI)
Result:
sorted_led_ROI:
[[ 4 17 26 24]
[ 47 38 27 22]
[ 95 52 24 21]
[ 47 38 27 22]
[ 4 17 26 24]
[ 47 38 27 22]
[ 47 38 27 22]
[324 13 22 21]]
help me out to sort this in python using np or cv
Do you mean just this:
print(ROI[ROI[:,0].argsort()])
Output:
[[ 4 17 26 24]
[ 47 38 27 22]
[ 95 52 24 21]
[143 60 23 19]
[191 60 23 18]
[237 51 24 21]
[281 35 25 22]
[324 13 22 21]]
I have a numpy ndarray, something like this (my array is much bigger, I am only giving this in order to explain what I need to do):
a = [[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
I am looking for an elegant way to rearrange the array in order to get something like this:
a_new=[[[ 0 9 18]
[ 1 10 19]
[ 2 11 20]]
[[ 3 12 21]
[ 4 13 22]
[ 5 14 23]]
[[ 6 15 24]
[ 7 16 25]
[ 8 17 26]]]
Simply permute axes -
a.transpose(1,2,0)
# or np.moveaxis(a,0,2)
I wanted to create a 3D NumPy array with sequential numbers like so:
[[[11 27 43]
[12 28 44]
[13 29 45]
[14 30 46]]
[[15 31 47]
[16 32 48]
[17 33 49]
[18 34 50]]
[[19 35 51]
[20 36 52]
[21 37 53]
[22 38 54]]
[[23 39 55]
[24 40 56]
[25 41 57]
[26 42 58]]]
I did this: A = np.arange(11, 59).reshape((4, 4, 3)) but I got this instead:
[[[11 12 13]
[14 15 16]
[17 18 19]
[20 21 22]]
[[23 24 25]
[26 27 28]
[29 30 31]
[32 33 34]]
[[35 36 37]
[38 39 40]
[41 42 43]
[44 45 46]]
[[47 48 49]
[50 51 52]
[53 54 55]
[56 57 58]]]
So it's not the sequence that I wanted. I had done some additional steps to get the correct 3D array. First, I shaped the numbers into a 2D array: A = np.arange(11, 59).reshape((-1, 4)) to get this:
[[11 12 13 14]
[15 16 17 18]
[19 20 21 22]
[23 24 25 26]
[27 28 29 30]
[31 32 33 34]
[35 36 37 38]
[39 40 41 42]
[43 44 45 46]
[47 48 49 50]
[51 52 53 54]
[55 56 57 58]]
Then, I splitted and stacked the 2D array and got the 3D array that I wanted: A = np.dstack(np.vsplit(A, 3))
[[[11 27 43]
[12 28 44]
[13 29 45]
[14 30 46]]
[[15 31 47]
[16 32 48]
[17 33 49]
[18 34 50]]
[[19 35 51]
[20 36 52]
[21 37 53]
[22 38 54]]
[[23 39 55]
[24 40 56]
[25 41 57]
[26 42 58]]]
Now I'm wondering if there is a more elegant and straightforward way to achieve the same result. Thanks you.
Get the ranged array, reshape and then permute axes -
np.arange(11, 59).reshape(3,4,4).transpose(1,2,0)
Another way to permute axes would be to use np.moveaxis -
np.moveaxis(np.arange(11, 59).reshape(3,4,4),0,2)
Discussion : A general intuition to solving such problems.