How to split an two-dimension array by the second dimension?(Python) - python

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

Related

How to modify every third element in matrix?

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)

Sorting/Cluster a 2D numpy array in ordered sequence based on multiple columns

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.

How to insert tensor array into tensor matrix after every second position

I have a tensor array a and a tensor matrix m. Now I want to insert a into m after every second position started at index 0 ending with len(m)-2. Let's make an equivalent example using numpy and plain python:
# define m
m = np.array([[3,7,6],[4,3,1],[8,4,2],[2,8,7]])
print(m)
#[[3 7 6]
# [4 3 1]
# [8 4 2]
# [2 8 7]]
# define a
a = np.array([1,2,3])
#[1 2 3]
# insert a into m
result = []
for i in range(len(m)):
result.append(a)
result.append(m[i])
print(np.array(result))
#[[1 2 3]
# [3 7 6]
# [1 2 3]
# [4 3 1]
# [1 2 3]
# [8 4 2]
# [1 2 3]
# [2 8 7]]
I am looking for a solution in tensorflow. I am convinced that there is a solution that doesn't need a loop but I am not able to find one. I hope someone can help me out with this!
You can concatenate your target vector at the beginning of each line of your matrix, and then reshape it.
import tensorflow as tf
initial_array = tf.constant([
[3, 7, 6],
[4, 3, 1],
[8, 4, 2],
[2, 8, 7],
])
vector_to_add = [1, 2, 3]
concat = tf.concat([[vector_to_add] * initial_array.shape[0], initial_array], axis=1) # Concatenate vector_to_add to each vector of initial_array
output = tf.reshape(concat, (2 * initial_array.shape[0], initial_array.shape[1])) # Reshape
This should work,
np.ravel(np.column_stack((m, np.tile(a, (4, 1))))).reshape(8, 3)
For idea, please refer to Interweaving two numpy arrays. Apply any solution described there, and reshape.

Swap two rows in a numpy array in python [duplicate]

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

How to use numpy's hstack?

I have one large numpy.ndarray array that I want to extract the 4th and 5th columns out of and put those columns into a 2D array. The [i,0] element should be the value on the 4th column and [i,1] should be the element from the 5th column.
I trying to use the numpy.hstack function to do this.
a = numpy.asarray([1, 2, 3, 4, 5])
for i in range(5):
a = numpy.vstack([a, numpy.asarray([1, 2, 3, 4, 5])])
combined = np.hstack([a[:,3], a[:,4]])
However, this simply gives me an nx1 array. I have tried multiple approaches using concatenate that look like these examples:
combined = np.concatenate([a[:,3], a[:,4]])
combined = np.concatenate([a[:,3], a[:,4]], axis=1)
combined = np.concatenate([a[:,3].T, a[:,4].T])
I feel like hstack is the function I want, but I can't seem to figure out how to make it give me an nx2 array. Can anyone point me in the right direction? Any help is appreciated.
Just slice out your data as follows:
X = [[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]]
slicedX = X[:,3:5]
results in:
[[3 4]
[3 4]
[3 4]
[3 4]]
I think this will do what you want:
a[:,[3,4]]
You can also use zip:
>>> c = numpy.array( zip( a[:, 3], a[:, 4]) )
>>> c
array([[4, 5],
[4, 5],
[4, 5],
[4, 5],
[4, 5],
[4, 5]])

Categories