how to get rows of 2d numpy array by index array - python

I have a 2-D array that looks like this:
my_array = np.array([[1,7]
[2,4]
[3,10]
[4,3]
[5,23]])
and I have an index array which looks like this:
index_array = np.array([0,2,3])
as an output I want to get a matrix containing only the rows from the index array
so the shape of the output matrix should be (3,2) and it should look like this:
[[1,7]
[3,10]
[4,3]]
The solution shouldn't use a for loop and should work with any 2-D matrix.
Thanks in advance :)

you can use numpy array slicing notation to select your rows :D
import numpy as np
my_array = np.array([[1,7],
[2,4],
[3,10],
[4,3],
[5,23]])
print(my_array[[0,2,3]])
my_array[index_array] would work as well

not sure if a list comprehension qualifies as a for loop, but you can have this one-line solution:
[list(my_array[i]) for i in index_array]
that will return [[1, 7], [3, 10], [4, 3]].

Related

Concatenate 2d list in python

I'm trying to create a 2d list with shape of [n,784] (the same shape as the MNIST image batches) using multiple [1,784] lists.
mylist.append(element) doesn't give me what I'm looking for, where mylist is the 2d [n,784] list and element is the [1,784] lists. It would return a list with shape [n,1,784].
I've also tried mylist[index].append(element), and I got a [784] 1d list instead.
Any idea how to solve my problem?
Thanks a lot
import numpy as np
myarray = np.array(mylist)
newarray = np.concatenate((myarray, element))
And if you want to turn it back into a list:
newlist = newarray.tolist()
a = [[1,1],[2,2]]
b = np.concatenate([a, a], axis=1).tolist()
The output will be:
[[1, 1, 1, 1], [2, 2, 2, 2]]

Putting 2 dimensional numpy arrays into a 3 dimensional array

I want to keep adding numpy arrays to another array in python.
let's say I have the following arrays:
arraytotal = np.array([])
array1 = np.array([1,1,1,1,1])
array2 = np.array([2,2,2,2,2])
and I want to append array1 and array2 into arraytotal. However, when I use:
arraytotal.append[array1]
it tells me:
'numpy.ndarray' object has no attribute 'append'
how can I append array1 and array2 into arraytotal?
Unfortunately, there is no way to manipulate arrays quite like that. Instead, make a list with the same name, and append the two arrays and change it to a numpy array like so:
arraytotal[]
array1 = np.array([1,1,1,1,1])
arraytotal.append[array1]
np.array(arraytotal)
You could use np.concatenate() like this :
arraytotal = np.concatenate(([array1], [array2]))
This results in the following 2D array.
array([[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2]])
Hope this is what you were looking for.
You should append the arrays onto a regular python list and then convert the list to a numpy array at the end:
import numpy as np
total = []
for i in range(5,15):
thisArray = np.arange(i)
total.append(thisArray)
total = np.asarray(total)
That loop makes a 2D array; you'd nest loops to produce higher dimensional arrays.

How to substitute values from array to values in list in Python?

I have an array (of numpy.ndarray type) like
arr=array([[1, 5, 1],
[4, 2, 0]])
and a list of values:
values=['father','mother','sister','brother','aunt','uncle']
And I'd like to substitute numbers in array arr with items from list values using array's items as indices of list values:arr[0,0]=values[arr[0,0]]
Here an example of what I'd like to have
arr=array([['mother', 'uncle', 'mother'],
['aunt', 'sister', 'father']])
Is there any elegant pythonic way to do this?
Thanks for helping in advance =)
You can convert the values to a numpy array then use a simple indexing:
>>> values = np.array(values)
>>>
>>> values[arr]
array([['mother', 'uncle', 'mother'],
['aunt', 'sister', 'father']],
dtype='|S7')
Read more about indexing: http://docs.scipy.org/doc/numpy-1.10.0/reference/arrays.indexing.html
use the numpy take function :
In [64]: np.take(values,arr)
Out[64]:
array([['mother', 'uncle', 'mother'],
['aunt', 'sister', 'father']],
dtype='<U7')
The conversion is then automatic.

Convert row vector to column vector in NumPy

import numpy as np
matrix1 = np.array([[1,2,3],[4,5,6]])
vector1 = matrix1[:,0] # This should have shape (2,1) but actually has (2,)
matrix2 = np.array([[2,3],[5,6]])
np.hstack((vector1, matrix2))
ValueError: all the input arrays must have same number of dimensions
The problem is that when I select the first column of matrix1 and put it in vector1, it gets converted to a row vector, so when I try to concatenate with matrix2, I get a dimension error. I could do this.
np.hstack((vector1.reshape(matrix2.shape[0],1), matrix2))
But this looks too ugly for me to do every time I have to concatenate a matrix and a vector. Is there a simpler way to do this?
The easier way is
vector1 = matrix1[:,0:1]
For the reason, let me refer you to another answer of mine:
When you write something like a[4], that's accessing the fifth element of the array, not giving you a view of some section of the original array. So for instance, if a is an array of numbers, then a[4] will be just a number. If a is a two-dimensional array, i.e. effectively an array of arrays, then a[4] would be a one-dimensional array. Basically, the operation of accessing an array element returns something with a dimensionality of one less than the original array.
Here are three other options:
You can tidy up your solution a bit by allowing the row dimension of the vector to be set implicitly:
np.hstack((vector1.reshape(-1, 1), matrix2))
You can index with np.newaxis (or equivalently, None) to insert a new axis of size 1:
np.hstack((vector1[:, np.newaxis], matrix2))
np.hstack((vector1[:, None], matrix2))
You can use np.matrix, for which indexing a column with an integer always returns a column vector:
matrix1 = np.matrix([[1, 2, 3],[4, 5, 6]])
vector1 = matrix1[:, 0]
matrix2 = np.matrix([[2, 3], [5, 6]])
np.hstack((vector1, matrix2))
Subsetting
The even simpler way is to subset the matrix.
>>> matrix1
[[1 2 3]
[4 5 6]]
>>> matrix1[:, [0]] # Subsetting
[[1]
[4]]
>>> matrix1[:, 0] # Indexing
[1 4]
>>> matrix1[:, 0:1] # Slicing
[[1]
[4]]
I also mentioned this in a similar question.
It works somewhat similarly to a Pandas dataframe. If you index the dataframe, it gives you a Series. If you subset or slice the dataframe, it gives you a dataframe.
Your approach uses indexing, David Z's approach uses slicing, and my approach uses subsetting.

N dimensional arrays - Python/Numpy

just wondering if there is any clever way to do the following.
I have an N dimensional array representing a 3x3 grid
grid = [[1,2,3],
[4,5,6],
[7,8,9]]
In order to get the first row I do the following:
grid[0][0:3]
>> [1,2,3]
In order to get the first column I would like to do something like this (even though it is not possible):
grid[0:3][0]
>> [1,4,7]
Does NumPy support anything similar to this by chance?
Any ideas?
Yes, there is something like that in Numpy:
import numpy as np
grid = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
grid[0,:]
# array([1, 2, 3])
grid[:,0]
# array([1, 4, 7])
You can use zip to transpose a matrix represented as a list of lists:
>>> zip(*grid)[0]
(1, 4, 7)
Anything more than just that, and I'd use Numpy.
To get the columns in Python you could use:
[row[0] for row in grid]
>>> [1,4,7]
You could rewrite your code for getting the row as
grid[0][:]
because [:] just copies the whole array, no need to add the indices.
However, depending on what you want to achieve, I'd say it's better to just write a small matrix class to hide this implementation stuff.

Categories

Resources