If I have a set of indices stored in two Numpy arrays, my goal is to slice a given input array based on corresponding indices in those index arrays. For eg.
index_arr1 = np.asarray([2,3,4])
index_arr2 = np.asarray([5,5,6])
input_arr = np.asarray([1,2,3,4,4,5,7,2])
The output to my code should be [[3,4,4],[4,4],[4,5]] which is basically [input_arr[2:5], input_arr[3:5], input_arr[4:6]]
Can anybody suggest a way to solve this problem using numpy functions and avoiding any for loops to be as efficient as possible.
Do you mean:
[input_arr[x:y] for x,y in zip(index_arr1, index_arr2)]
Output:
[array([3, 4, 4]), array([4, 4]), array([4, 5])]
Or if you really want list of lists:
[[input_arr[x:y].tolist() for x,y in zip(index_arr1, index_arr2)]
Output:
[[3, 4, 4], [4, 4], [4, 5]]
Related
Given indexes for each row, how to return the corresponding elements in a 2-d matrix?
For instance, In array of np.array([[1,2,3,4],[4,5,6,7]]) I expect to see the output [[1,2],[4,5]] given indxs = np.array([[0,1],[0,1]]). Below is what I've tried:
a= np.array([[1,2,3,4],[4,5,6,7]])
indxs = np.array([[0,1],[0,1]]) #means return the elements located at 0 and 1 for each row
#I tried this, but it returns an array with shape (2, 2, 4)
a[idxs]
The reason you are getting two times your array is that when you do a[[0,1]] you are selecting the rows 0 and 1 from your array a, which are indeed your entire array.
In[]: a[[0,1]]
Out[]: array([[1, 2, 3, 4],
[4, 5, 6, 7]])
You can get the desired output using slides. That would be the easiest way.
a = np.array([[1,2,3,4],[4,5,6,7]])
a[:,0:2]
Out []: array([[1, 2],
[4, 5]])
In case you are still interested on indexing, you could also get your output doing:
In[]: [list(a[[0],[0,1]]),list(a[[1],[0,1]])]
Out[]: [[1, 2], [4, 5]]
The NumPy documentation gives you a really nice overview on how indexes work.
In [120]: indxs = np.array([[0,1],[0,1]])
In [121]: a= np.array([[1,2,3,4],[4,5,6,7]])
...: indxs = np.array([[0,1],[0,1]]) #
You need to provide an index for the first dimension, one that broadcasts with with indxs.
In [122]: a[np.arange(2)[:,None], indxs]
Out[122]:
array([[1, 2],
[4, 5]])
indxs is (2,n), so you need a (2,1) array to give a (2,n) result
is there a way to Binary Tree search an unsorted matrix? If yes, could you explain it as I am new to programming? I have tried implementing it using nested for i for j for loops but was wondering if there is a faster way.
import numpy as np
matrix = [[3, 6, 7], [9, 1, 2], [8, 4, 5]]
matrix = np.array(matrix)
matrix
array([[3, 6, 7],
[9, 1, 2],
[8, 4, 5]]) # how does one perform a binary tree search on an unsorted matrix?
You can use np.where for this.
matrix = np.array([[3,6,7],[9,1,2],[8, 8, 8]])
dim_1, dim_2 = np.where(matrix == 8)
#dim_1 = array([2, 2, 2], dtype=int64)
#dim_2 = array([0, 1, 2], dtype=int64)
#dim_1, dim_2, dim_3 = np.where(matrix == 8) if matrix had shape (3, )
num_8 = len(ret[0]) #total number of 8's
np.where returns a tuple of arrays separated by indexes based on the shape of your array. If you have a 3D array you will get 3 arrays in your tuple.
ret = (array([2, 2, 2], dtype=int64), array([0, 1, 2], dtype=int64))
ret[0] corresponds to the row values, and ret[1] corresponds to the column values.
So this means that the element 8 is present in matrix[2][0], matrix[2][1], matrix[2][2]
Does that help? You won't have to write your own routine for this. Pretty sure this will be faster than any search routine you will implement in pure python because NumPy built-in functions are highly optimized. You should consider using NumPy methods for NumPy arrays wherever possible.
In my project I use a library (root_numpy) which returns data as an array of arrays:
data = array([array([1, 2]), array([3, 4]), array([5, 6])], dtype=object)
I would like to turn this object to a regular 2d numpy array.
array([[1, 2],
[3, 4],
[5, 6]])
I already tried np.vstack but that was rather slow. Is there an efficient way to accomplish this task? Many thanks in advance.
I want to extract a part of a two-dimensional list (=list of lists) in Python. I use Mathematica a lot, and there it is very convenient to write
matrix[[2;;4,10;;13]]
which would extract the part of the matrix which is between the 2nd and 4th row as well as the 10th and 13th column.
In Python, I just used
[x[firstcolumn:lastcolumn+1] for x in matrix[firstrow:lastrow+1]]
Is there also a more elegant or efficient way to do this?
What you want is numpy arrays and the slice operator :.
>>> import numpy
>>> a = numpy.array([[1,2,3],[2,2,2],[5,5,5]])
>>> a
array([[1, 2, 3],
[2, 2, 2],
[5, 5, 5]])
>>> a[0:2,0:2]
array([[1, 2],
[2, 2]])
I have a 2D list something like
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
and I want to convert it to a 2d numpy array. Can we do it without allocating memory like
numpy.zeros((3,3))
and then storing values to it?
Just pass the list to np.array:
a = np.array(a)
You can also take this opportunity to set the dtype if the default is not what you desire.
a = np.array(a, dtype=...)
just use following code
c = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Then it will give you
you can check shape and dimension of matrix by using following code
c.shape
c.ndim
np.array() is even more powerful than what unutbu said above.
You also could use it to convert a list of np arrays to a higher dimention array, the following is a simple example:
aArray=np.array([1,1,1])
bArray=np.array([2,2,2])
aList=[aArray, bArray]
xArray=np.array(aList)
xArray's shape is (2,3), it's a standard np array. This operation avoids a loop programming.
I am using large data sets exported to a python file in the form
XVals1 = [.........]
XVals2 = [.........]
Each list is of identical length. I use
>>> a1 = np.array(SV.XVals1)
>>> a2 = np.array(SV.XVals2)
Then
>>> A = np.matrix([a1,a2])