This question already has an answer here:
Row exchange in Numpy [duplicate]
(1 answer)
Closed 4 years ago.
How to swap xth and yth rows of the 2-D NumPy array? x & y are inputs provided by the user.
Lets say x = 0 & y =2 , and the input array is as below:
a = [[4 3 1]
[5 7 0]
[9 9 3]
[8 2 4]]
Expected Output :
[[9 9 3]
[5 7 0]
[4 3 1]
[8 2 4]]
I tried multiple things, but did not get the expected result. this is what i tried:
a[x],a[y]= a[y],a[x]
output i got is:
[[9 9 3]
[5 7 0]
[9 9 3]
[8 2 4]]
Please suggest what is wrong in my solution.
Put the index as a whole:
a[[x, y]] = a[[y, x]]
With your example:
a = np.array([[4,3,1], [5,7,0], [9,9,3], [8,2,4]])
a
# array([[4, 3, 1],
# [5, 7, 0],
# [9, 9, 3],
# [8, 2, 4]])
a[[0, 2]] = a[[2, 0]]
a
# array([[9, 9, 3],
# [5, 7, 0],
# [4, 3, 1],
# [8, 2, 4]])
Related
I have a numpy array in this form:
n = [[[5 0 2]
[8 9 7]
[2 2 2]
[5 9 5] <-- target value
[4 1 5]]
[[5 3 9]
[4 2 7]
[7 0 7]
[4 9 6] <-- target value
[3 8 5]]]
I want to get all the values except the 3rd row from each individual array. ie. the results should be in this way:
[[[5 0 2]
[8 9 7]
[2 2 2]
[4 1 5]]
[[5 3 9]
[4 2 7]
[7 0 7]
[3 8 5]]]
I tried using indexing, but I cannot achieve it. I have a large numpy array where this operation needs to be done. Is there any efficient method to do it, other than making a copy of it and removing it using delete method. Also, I don't want to utilize much space by creating a copy, I just want to use indexing to ignore the particular column for a while.
I tried this and it worked:
You can try using this method:
t = np.array([[[5, 0, 2],
[8, 9, 7],
[2, 2, 2],
[5, 9, 5],
[4, 1, 5]],
[[5, 3, 9],
[4, 2, 7],
[7, 0, 7],
[4, 9, 6],
[3, 8, 5]]])
t[:,[0,1,2,4]]
You can create a list of selected elements, popping the target one:
import numpy as np
n = np.array([[[5,0,2],
[8,9,7],
[2,2,2],
[5,9,5],
[4,1,5]],
[[5,3,9],
[4,2,7],
[7,0,7],
[4,9,6],
[3,8,5]]])
target_element = 3
s = list(range(len(n[0])))
s.pop(target_element)
print(n[:,s])
or
s = list(range(len(n[0])))
print(n[:,s[:target_element] + s[target_element+1:]])
I have a 2D numpy array like this:
[[4 5 2]
[5 5 1]
[5 4 5]
[5 3 4]
[5 4 4]
[4 3 2]]
I would like to sort/cluster this array by finding the sequence in array like this row[0]>=row[1]>=row[2], row[0]>=row[2]>row[1]... so the row of the array is in ordered sequence.
I tried to use the code: lexdf = df[np.lexsort((df[:,2], df[:,1],df[:,0]))][::-1], however it is not I want.
The output of lexsort:
[[5 5 1]
[5 4 5]
[5 4 4]
[5 3 4]
[4 5 2]
[4 3 2]]
The output I would like to have:
[[5 5 1]
[5 4 4]
[4 3 2]
[5 4 5]
[5 3 4]
[4 5 2]]
or cluster it into three parts:
[[5 5 1]
[5 4 4]
[4 3 2]]
[[5 4 5]
[5 3 4]]
[[4 5 2]]
And I would like to apply this to an array with more columns, so it would be better to do it without iteration. Any ideas to generate this kind of output?
I don't know how to do it in numpy, except maybe with some weird hacks of function numpy.split.
Here is a way to get your groups with python lists:
from itertools import groupby, pairwise
def f(sublist):
return [x <= y for x,y in pairwise(sublist)]
# NOTE: itertools.pairwise requires python>=3.10
# For python<=3.9, use one of those alternatives:
# * more_itertools.pairwise(sublist)
# * zip(sublist, sublist[1:])
a = [[4, 5, 2],
[5, 5, 1],
[5, 4, 5],
[5, 3, 4],
[5, 4, 4],
[4, 3, 2]]
b = [list(g) for _,g in groupby(sorted(a, key=f), key=f)]
print(b)
# [[[4, 3, 2]],
# [[5, 4, 5], [5, 3, 4], [5, 4, 4]],
# [[4, 5, 2], [5, 5, 1]]]
Note: The combination groupby+sorted is actually slightly subefficient, because sorted takes n log(n) time. A linear alternative is to group using a dictionary of lists. See for instance function itertoolz.groupby from module toolz.
I have an array of points, and I want to split these into two arrays by the second dimension:
points_right = points[points[:, 0] > p0[0]]
points_left = points[points[:, 0] < p0[0]]
how can I split these points in one loop?
I think np.split is what you're looking for, just use axis=1.
Example splitting a 2x4 matrix:
import numpy as np
pts = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
left_pts, right_pts = np.split(pts, indices_or_sections=2, axis=1)
The original matrix (pts):
[[1 2 3 4]
[5 6 7 8]]
left_pts:
[[1 2]
[5 6]]
right_pts:
[[3 4]
[7 8]]
https://numpy.org/doc/stable/reference/generated/numpy.split.html
Suppose we want to generate the same random number between 1 and 10 each time. Then when I run the following I get the same random number each time:
import os
import numpy as np
import random
random.seed(30)
random.randint(1, 10)
9
random.seed(30)
random.randint(1, 10)
9
But if I want to generate the same random 4x4 matrix with numbers between 1 and 10 each time, I get different results:
random.seed(30)
np.random.randint(10, size=(4,4))
array([[8, 2, 6, 4],
[3, 3, 3, 5],
[6, 2, 6, 6],
[8, 7, 1, 1]])
random.seed(30)
np.random.randint(10, size=(4,4))
array([[9, 2, 1, 6],
[4, 3, 3, 8],
[1, 1, 6, 6],
[0, 2, 3, 5]])
Question. How do I get the same array each time using random.seed() ?
Added. I added the import statements.
You need to use numpy.random.seed and not random.seed.
Now, you mix 2 different modules i.e. numpy and random.
import numpy as np
for i in range(5):
np.random.seed(30)
print(np.random.randint(10, size=(4,4)))
[[5 5 4 7]
[2 5 1 3]
[9 7 7 1]
[1 3 2 2]]
[[5 5 4 7]
[2 5 1 3]
[9 7 7 1]
[1 3 2 2]]
[[5 5 4 7]
[2 5 1 3]
[9 7 7 1]
[1 3 2 2]]
[[5 5 4 7]
[2 5 1 3]
[9 7 7 1]
[1 3 2 2]]
[[5 5 4 7]
[2 5 1 3]
[9 7 7 1]
[1 3 2 2]]
This question already has answers here:
Multiplying across in a numpy array
(7 answers)
Closed 3 years ago.
I have a (n,1) np-array, for example array([1 2 3]) that I would like to multiply element wise with a np-matrix (n,m), for example array([[1 1 1], [2 2 2], [3 3 3]]) so that I will get:
array([[1 1 1], [4 4 4], [9 9 9]])
How can I do that?
I have tried with np.multiply and np.dot.
Reshape your vector so that it contains 3 rows instead of 3 columns:
v = np.array([1, 2, 3])
m = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
u = v.reshape(*v.shape, 1)
u * m # results in [[1, 1, 1], [4, 4, 4], [9, 9, 9]]