Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to add a singleton dimension to a NumPy vector so that slice assignments work

Tags:

python

numpy

In NumPy, how can you efficiently make a 1-D object into a 2-D object where the singleton dimension is inferred from the current object (i.e. a list should go to either a 1xlength or lengthx1 vector)?

 # This comes from some other, unchangeable code that reads data files.
 my_list = [1,2,3,4]

 # What I want to do:
 my_numpy_array[some_index,:] = numpy.asarray(my_list)

 # The above doesn't work because of a broadcast error, so:
 my_numpy_array[some_index,:] = numpy.reshape(numpy.asarray(my_list),(1,len(my_list)))

 # How to do the above without the call to reshape?
 # Is there a way to directly convert a list, or vector, that doesn't have a
 # second dimension, into a 1 by length "array" (but really it's still a vector)?
like image 360
ely Avatar asked Mar 01 '12 03:03

ely


People also ask

How do I add an extra dimension to a NumPy array?

You can add new dimensions to a NumPy array ndarray (= unsqueeze a NumPy array) with np. newaxis , np. expand_dims() and np. reshape() (or reshape() method of ndarray ).

Can you slice NumPy arrays?

You can slice a numpy array is a similar way to slicing a list - except you can do it in more than one dimension. As with indexing, the array you get back when you index or slice a numpy array is a view of the original array.

How does NumPy Expand_dims work?

NumPy: expand_dims() function The expand_dims() function is used to expand the shape of an array. Insert a new axis that will appear at the axis position in the expanded array shape. Input array. IPosition in the expanded axes where the new axis is placed.

What is indexing and slicing in NumPy?

Numpy with Python Three types of indexing methods are available − field access, basic slicing and advanced indexing. Basic slicing is an extension of Python's basic concept of slicing to n dimensions. A Python slice object is constructed by giving start, stop, and step parameters to the built-in slice function.


3 Answers

In the most general case, the easiest way to add extra dimensions to an array is by using the keyword None when indexing at the position to add the extra dimension. For example

my_array = numpy.array([1,2,3,4])

my_array[None, :] # shape 1x4

my_array[:, None] # shape 4x1
like image 52
DaveP Avatar answered Oct 10 '22 13:10

DaveP


Why not simply add square brackets?

>> my_list
[1, 2, 3, 4]
>>> numpy.asarray([my_list])
array([[1, 2, 3, 4]])
>>> numpy.asarray([my_list]).shape
(1, 4)

.. wait, on second thought, why is your slice assignment failing? It shouldn't:

>>> my_list = [1,2,3,4]
>>> d = numpy.ones((3,4))
>>> d
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])
>>> d[0,:] = my_list
>>> d[1,:] = numpy.asarray(my_list)
>>> d[2,:] = numpy.asarray([my_list])
>>> d
array([[ 1.,  2.,  3.,  4.],
       [ 1.,  2.,  3.,  4.],
       [ 1.,  2.,  3.,  4.]])

even:

>>> d[1,:] = (3*numpy.asarray(my_list)).T
>>> d
array([[  1.,   2.,   3.,   4.],
       [  3.,   6.,   9.,  12.],
       [  1.,   2.,   3.,   4.]])
like image 5
DSM Avatar answered Oct 10 '22 13:10

DSM


import numpy as np
a = np.random.random(10)
sel = np.at_least2d(a)[idx]
like image 3
Steven Kearnes Avatar answered Oct 10 '22 14:10

Steven Kearnes