Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/numpy issue with array/vector with empty second dimension

I have what seems to be an easy question.

Observe the code:

In : x=np.array([0, 6])
Out: array([0, 6])
In : x.shape
Out: (2L,)

Which shows that the array has no second dimension, and therefore x is no differnet from x.T.

How can I make x have dimension (2L,1L)? The real motivation for this question is that I have an array y of shape [3L,4L], and I want y.sum(1) to be a vector that can be transposed, etc.

like image 692
Lepidopterist Avatar asked Apr 17 '15 01:04

Lepidopterist


People also ask

How do you create a two dimensional empty NumPy array?

Create an empty 2D Numpy array using numpy.empty() To create an empty 2D Numpy array we can pass the shape of the 2D array ( i.e. row & column count) as a tuple to the empty() function.

Can NumPy arrays have more than 2 dimensions?

numpy arrays can have 0, 1, 2 or more dimensions. C. shape returns a tuple of the dimensions; it may be of 0 length, () , 1 value, (81,) , or 2 (81,1) .

What is a 2 dimensional NumPy array?

2D array are also called as Matrices which can be represented as collection of rows and columns. In this article, we have explored 2D array in Numpy in Python. Numpy is a library in Python adding support for large multidimensional arrays and matrices along with high level mathematical functions to operate these arrays.


2 Answers

While you can reshape arrays, and add dimensions with [:,np.newaxis], you should be familiar with the most basic nested brackets, or list, notation. Note how it matches the display.

In [230]: np.array([[0],[6]])
Out[230]: 
array([[0],
       [6]])
In [231]: _.shape
Out[231]: (2, 1)

np.array also takes a ndmin parameter, though it add extra dimensions at the start (the default location for numpy.)

In [232]: np.array([0,6],ndmin=2)
Out[232]: array([[0, 6]])
In [233]: _.shape
Out[233]: (1, 2)

A classic way of making something 2d - reshape:

In [234]: y=np.arange(12).reshape(3,4)
In [235]: y
Out[235]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

sum (and related functions) has a keepdims parameter. Read the docs.

In [236]: y.sum(axis=1,keepdims=True)
Out[236]: 
array([[ 6],
       [22],
       [38]])
In [237]: _.shape
Out[237]: (3, 1)

empty 2nd dimension isn't quite the terminology. More like a nonexistent 2nd dimension.

A dimension can have 0 terms:

In [238]: np.ones((2,0))
Out[238]: array([], shape=(2, 0), dtype=float64)

If you are more familiar with MATLAB, which has a minimum of 2d, you might like the np.matrix subclass. It takes steps to ensure that most operations return another 2d matrix:

In [247]: ym=np.matrix(y)
In [248]: ym.sum(axis=1)
Out[248]: 
matrix([[ 6],
        [22],
        [38]])

The matrix sum does:

np.ndarray.sum(self, axis, dtype, out, keepdims=True)._collapse(axis)

The _collapse bit lets it return a scalar for ym.sum().

like image 86
hpaulj Avatar answered Sep 24 '22 08:09

hpaulj


There is another point to keep dimension info:

In [42]: X
Out[42]: 
array([[0, 0],
       [0, 1],
       [1, 0],
       [1, 1]])

In [43]: X[1].shape
Out[43]: (2,)

In [44]: X[1:2].shape
Out[44]: (1, 2)

In [45]: X[1]
Out[45]: array([0, 1])

In [46]: X[1:2]  # this way will keep dimension
Out[46]: array([[0, 1]])
like image 45
Belter Avatar answered Sep 23 '22 08:09

Belter