6

Always confused how numpy reshape handle negative shape parameter, here is an example of code and output, could anyone explain what happens for reshape [-1, 1] here? Thanks.

Related document, using Python 2.7.

http://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html

import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

S = np.array(['box','apple','car'])
le = LabelEncoder()
S = le.fit_transform(S)
print(S)
ohe = OneHotEncoder()
one_hot = ohe.fit_transform(S.reshape(-1,1)).toarray()
print(one_hot)

[1 0 2]
[[ 0.  1.  0.]
 [ 1.  0.  0.]
 [ 0.  0.  1.]]
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • Does this answer your question? [What does -1 mean in numpy reshape?](https://stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape) – LudvigH Feb 07 '21 at 19:49

2 Answers2

10

-1 is used to infer one missing length from the other. For example reshaping (3,4,5) to (-1,10) is equivalent to reshaping to (6,10) because 6 is the only length that makes sense form the other inputs.

Julien
  • 13,986
  • 5
  • 29
  • 53
  • Thanks Julien, in your example, it is 3 dimensional array 3*4*5? So, if after reshape we need to have # of columns to be 10, the rows need to be 6, is that the correct calculation? Thanks. – Lin Ma Aug 22 '16 at 05:01
  • 1
    Yes: the input array has `3*4*5 = 60` elements. The output will have `n_rows * 10` elements, and so we need to solve for `n_rows * 10 = 60` which gives `n_rows = 6`. – Julien Aug 22 '16 at 05:04
  • Thanks Julien, but how do they handle if multiple -1? – Lin Ma Aug 22 '16 at 05:05
  • 1
    It can't because it is ambiguous, as mentioned in the docs, or by the error thrown if you try... – Julien Aug 22 '16 at 05:06
  • Thanks Julien for the help, going to mark your reply as answer. :) – Lin Ma Aug 22 '16 at 05:07
5

From reshape docs:

One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

In your case it is used for the common task of transforming the (3,) S into a (3,1) array. I think in this particular case using S[:, None] would have the same effect.

Aguy
  • 7,851
  • 5
  • 31
  • 58
  • Thanks Aguy, when you say `(3,)`, a bit confused by the syntax, what does the `,` mean here? – Lin Ma Aug 22 '16 at 05:06
  • 1
    Just to design a tuple. `(3)` is `3`, `(3,)` is the tuple with a single element `3`. (shapes are tuples, even if they contain only one element) – Julien Aug 22 '16 at 05:07
  • 1
    You can think about `(n,)` numpy object as a vector and `(n,1)` as a matrix of just 1 column. – Aguy Aug 22 '16 at 05:09
  • Thanks Aguy, but if `(3,)` means only one element in a tuple, how you are going to transform it to be shape of `(3,1)`? I think `(3,1)` means 3 rows * 1 column, which is 3 elements? – Lin Ma Aug 22 '16 at 05:12
  • @JulienBernu, but if `(3,)` means only one element in a tuple, how you are going to transform it to be shape of `(3,1)`? I think `(3,1)` means 3 rows * 1 column, which is 3 elements? – Lin Ma Aug 22 '16 at 05:13
  • `(3,)` still means 3 elements, just like `(3,1)` or `(3,1,1,1,...,1)`. `(3,)` means the object is a 1d array. `(3,1)` means it is a 2d array (although the second dimension is 1). It is actually a column vector. `(1,3)` would represent a row vector (but still as a 2d array). Makes sense? You can try to print all those versions and you'll see some subtle differences in where/how many brackets appear. – Julien Aug 22 '16 at 05:18