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:]])
Related
I had to make a matrix using numpy.array method. How can I now update every third element of my matrix? I have made a for loop for the problem but that is not the optimal solution. Is there a way to avoid loops? For example if I have this matrix:
matrix = np.array([[1,2,3,4],
[5,6,7,8],
[4,7,6,9]])
is there a way to add 1 to every third element and get this matrix:
[[2,2,3,5],[5,6,8,8],[4,8,6,9]]
Solution:
matrix = np.ascontiguousarray(matrix)
matrix.ravel()[::3] += 1
Why does the ascontiguousarray is needed? Because matrix may not be c-contiguous (for example matrix may have fortran-order - column major). It that case ravel returns a copy instead of a view so a simple inplace operation matrix.ravel()[::3] += 1 will not work as expected.
Example 1
import numpy as np
arr = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[4, 7, 6, 9]])
arr.ravel()[::3] += 1
print(arr)
Works as expected:
[[2 2 3 5]
[5 6 8 8]
[4 8 6 9]]
Example 2
But with fortran-order
import numpy as np
arr = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[4, 7, 6, 9]])
arr = np.asfortranarray(arr)
arr.ravel()[::3] += 1
print(arr)
produces:
[[1 2 3 4]
[5 6 7 8]
[4 7 6 9]]
Example 3
Will work as expected in both cases
import numpy as np
arr = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[4, 7, 6, 9]])
# arr = np.asfortranarray(arr)
arr = np.ascontiguousarray(arr)
arr.ravel()[::3] += 1
print(arr)
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 want replace last element of every row in an ndarray with a constant. Currently I can solve this by using loops, but i'm looking for an elegant solution. preferably using numpy functions.
for example i have a ndarray :
[1 3 4 5]
[4 2 4 1]
[3 2 7 3]
[7 9 4 3]
[6 9 7 2]
Here is the result i want, with last element of every row is replaced with 10
[1 3 4 10]
[4 2 4 10]
[3 2 7 10]
[7 9 4 10]
[6 9 7 10]
use numpy indexing for columns
import numpy as np
arr = np.array([[1,3,4,5],
[4,2,4,1],
[3,2,7,3],
[7,9,4,3],
[6,9,7,2]])
arr[:,-1]=10
arr
array([[ 1, 3, 4, 10],
[ 4, 2, 4, 10],
[ 3, 2, 7, 10],
[ 7, 9, 4, 10],
[ 6, 9, 7, 10]])
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]])
I have a numpy array created such as:
x = np.array([[1,2,3,4],[5,6,7,8]])
y = np.asarray([x])
which prints out
x=[[1 2 3 4]
[5 6 7 8]]
y=[[[1 2 3 4]
[5 6 7 8]]]
What I would like is an array such as
[0 [[1 2 3 4]
[5 6 7 8]]]
What's the easiest way to go about this?
Thanks!
To do what you're asking, just use the phrase
labeledArray = [0, x]
This way, you will get a standard list with 0 as the first element and a Numpy array as the second element.
However, in practice, you are probably trying to label for the purpose of later recall. In that case, I'd recommend you use a dictionary, as it is less confusing to keep track of:
myArrays = {}
myArrays[0] = x
Which can be used as follows:
>>> myArrays
{0: array([[1, 2, 3, 4],
[5, 6, 7, 8]])}
>>> myArrays[0]
array([[1, 2, 3, 4],
[5, 6, 7, 8]])