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.
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)
ohe = OneHotEncoder()
one_hot = ohe.fit_transform(S.reshape(-1,1)).toarray()
[1 0 2]
[[ 0. 1. 0.]
[ 1. 0. 0.]
[ 0. 0. 1.]]
-1 is used to infer one missing length from the other. For example reshaping
(-1,10) is equivalent to reshaping to
6 is the only length that makes sense form the other inputs.