Order of elements in a numpy array

Abhinav

I have a 2-d array of shape(nx3), say arr1. Now consider a second array, arr2, of same shape as arr1 and has the same rows. However, the rows are not in the same order. I want to get the indices of each row in arr2 as they are in arr1. I am looking for fastest Pythonic way to do this as n is of the order of 10,000.

For example:

arr1 = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
arr2 = numpy.array([[4, 5, 6], [7, 8, 9], [1, 2, 3]])
ind = [1, 2, 0]

Note that the row elements need not be integers. In fact they are floats. I have found related answers that use numpy.searchsorted but they work for 1-D arrays only.

HYRY

If you are ensure that arr2 is a permutation of arr1, you can use sort to get the index:

import numpy as np

n = 100000
a1 = np.random.randint(0, 100, size=(n, 3))
a2 = a1[np.random.permutation(np.arange(n))]
idx1 = np.lexsort(a1.T)
idx2 = np.lexsort(a2.T)
idx = idx2[np.argsort(idx1)]
np.all(a1 == a2[idx])

if they don't have exact the same values, you can use kdTree in scipy:

n = 100000

a1 = np.random.uniform(0, 100, size=(n, 3))
a2 = a1[np.random.permutation(np.arange(n))] + np.random.normal(0, 1e-8, size=(n, 3))
from scipy import spatial
tree = spatial.cKDTree(a2)
dist, idx = tree.query(a1)
np.allclose(a1, a2[idx])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Order of elements in a numpy array

From Dev

Convert numpy.array to order of elements when duplicate values are present

From Dev

Numpy: np_array[index_order_array] changes the order of elements in an array?

From Dev

alternate the order of elements of array

From Dev

Reverse the order of elements in an array

From Dev

Order the elements of an array (Swift)

From Dev

Padding elements of a numpy array

From Java

Shift elements in a numpy array

From Dev

Vary the elements in a numpy array

From Dev

Manipulating array elements in NumPy

From Dev

Specifying Elements In A Numpy Array

From Dev

Manipulating array elements in NumPy

From Dev

Operations with numpy array elements

From Dev

Padding elements of a numpy array

From Dev

Concatenate elements of an array with numpy?

From Dev

Adding elements to array in alphebetical order

From Dev

Insertion order of array elements in MongoDB

From Dev

Arrange elements in an array in a specific order

From Dev

Sort elements of an array in ascending order

From Dev

binding elements of array with a specified order

From Dev

Printing array elements in reverse order

From Dev

Order of indexes in a Numpy multidimensional array

From Dev

Double th numpy array in order

From Dev

Rank elements of numpy list in descending order

From Dev

Rank elements of numpy list in descending order

From Dev

Iterating elements of a numpy array by cycling

From Dev

Numpy: Replace random elements in an array

From Dev

Numpy: Quick setting of array elements

From Dev

How to append elements to a numpy array