Need to sort a nested tuple with numbers [duplicate] - python

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]]

Related

How can I get uneven submatrices from NxN matrix?

I have a large NxN matrix that I'm looking to retrieve multiple submatrices from. Each of these submatrices can be different sizes but they can't overlap (see attached pic). Is there a function in Python that could remotely do what I'm looking to achieve?
example of submatrices in NxN matrix
This is what I've written so far; however, it doesn't give me back a square submatrix
import numpy as np
# Create a 10x10 matrix
matrix = np.arange(0, 100).reshape((10, 10))
print(matrix)
# Define the sizes of the submatrices
submatrix_sizes = [4, 4, 5]
# Calculate the starting and ending indices for each submatrix
starts = np.cumsum([0] + submatrix_sizes[:-1])
ends = np.cumsum(submatrix_sizes)
# Split the matrix into submatrices of the specified sizes
submatrices = np.split(matrix, ends, axis=1)[:-1]
# Print the submatrices
for i, submatrix in enumerate(submatrices):
print(f"Submatrix {i+1}:")
print(submatrix)
Output
[[ 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 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 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
Submatrix 1:
[[ 0 1 2 3]
[10 11 12 13]
[20 21 22 23]
[30 31 32 33]
[40 41 42 43]
[50 51 52 53]
[60 61 62 63]
[70 71 72 73]
[80 81 82 83]
[90 91 92 93]]
Submatrix 2:
[[ 4 5 6 7]
[14 15 16 17]
[24 25 26 27]
[34 35 36 37]
[44 45 46 47]
[54 55 56 57]
[64 65 66 67]
[74 75 76 77]
[84 85 86 87]
[94 95 96 97]]
Submatrix 3:
[[ 8 9]
[18 19]
[28 29]
[38 39]
[48 49]
[58 59]
[68 69]
[78 79]
[88 89]
[98 99]]
Your starts and ends are not calculated correctly:
It is impossible to have index of 13 on any axis on a 10x10 matix.
you don't use the calculated starts while slicing
starts = np.cumsum([0] + submatrix_sizes[:-1])
# has to be disiced how to calculate these correctly
ends = np.cumsum(submatrix_sizes)
breaks = list(zip(starts, ends))
# slicing x and y axis not only x
submatrix_sizes = [matrix[elem[0]:elem[1], elem[0]:elem[1]] for elem in breaks]

Is there a numpy function which takes from each line i of a matrix an element on the column y[i] and puts them all into an array?

So basically, I need a numpy function which will do this or something similar to this:
correct_answers = np.array([scores[i][y[i]] for i in range(num_train)])
but using numpy, because Python list comprehension is too slow for me
scores is a num_train X columns matrix and y is an array of length num_train and takes values from 0 to columns - 1 inclusive
Is there a workaround using arange or something similar? Thanks.
import numpy as np
y = np.arange(81).reshape(9, 9)
correct_answers = y[np.arange(9), np.arange(9)]
output:
y =
[[ 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]
[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 59 60 61 62]
[63 64 65 66 67 68 69 70 71]
[72 73 74 75 76 77 78 79 80]]
correct_answers =
[ 0 10 20 30 40 50 60 70 80]
correct_answers = scores[np.arange(num_train), y[np.arange(num_train)]]
This does the thing I wanted to do, props to the other dude which gave me the idea

Create 3D NumPy array with sequential number

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.

How to reshape a 3D array

I'm new in python world. I need a help for a problem. I need to reshape a 3D array.
This is an example:
I have:
[[[ 1 16 31]
[ 2 17 32]
[ 3 18 33]
[ 4 19 34]
[ 5 20 35]]
[[ 6 21 36]
[ 7 22 37]
[ 8 23 38]
[ 9 24 39]
[10 25 40]]
[[11 26 41]
[12 27 42]
[13 28 43]
[14 29 44]
[15 30 45]]]
I need to reshape it in:
[[[ 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 37 38 39 40]
[41 42 43 44 45]]]
Thanks for your help!
With the Einstein notation to switch axes
np.einsum("ijk->kij", arr)
Here is a nice introduction Einstein Summation in Numpy

Append columns from a DataFrame to a list

Is it possible to append columns from a dataframe into an empty list?
Example of a random df is produced:
df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD'))
The output is:
A B C D
0 25 27 34 77
1 85 62 39 49
2 90 51 2 97
3 39 19 86 59
4 33 79 64 73
5 36 66 29 78
6 22 27 84 41
7 0 26 22 22
8 44 57 29 37
9 0 31 96 90
If I had an empty list or lists, could you append the columns by each row? So A,C to a list and B,Dto a list. An example output would be:
empty_list = [[],[]]
empty_list[0] = [[25,34],
[85,39]
[90,2]
[39,86]
[33,64]
[36,29]
[22,84]
[0,22]
[44,29]
[0,96]]
Or would you have to go through and convert each column to a list with df['A'].tolist() and then go through an append by row?
Try this
d=df[['A','C']]
d.values.tolist()
Output
[[0, 93], [58, 14], [79, 18], [40, 26], [91, 14], [25, 18], [22, 25], [35, 99], [12, 82], [48, 72]]
So the solution would be :
empty_list = [[],[]]
empty_list[0]=df[['A','C']].values.tolist()
empty_list[1]=df[['B','D']].values.tolist()
My df was :
df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD'))
df
A B C D
0 0 60 93 94
1 58 52 14 33
2 79 84 18 1
3 40 21 26 32
4 91 19 14 8
5 25 34 18 68
6 22 37 25 10
7 35 58 99 80
8 12 38 82 8
9 48 56 72 66

Categories