Skip to content Skip to sidebar Skip to footer

Python Valueerror: Non-broadcastable Output Operand With Shape (124,1) Doesn't Match The Broadcast Shape (124,13)

I would like to normalize a training and test data set using MinMaxScaler in sklearn.preprocessing. However, the package does not appear to be accepting my test data set. import pa

Solution 1:

The partitioning of train/test data must be specified in the same order as the input array to the train_test_split() function for it to unpack them corresponding to that order.

Clearly, when the order was specified as X_train, y_train, X_test, y_test, the resulting shapes of y_train (len(y_train)=54) and X_test (len(X_test)=124) got swapped resulting in the ValueError.

Instead, you must:

# Split into train/test data.#                   _________________________________
#                   |       |                        \
#                   |       |                         \
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)                                        
# |          |                                      /
# |__________|_____________________________________/
# (or)
# y_train, y_test, X_train, X_test = train_test_split(y, X, test_size=0.3, random_state=0)

# Normalize features using min-max scaling.
from sklearn.preprocessing import MinMaxScaler
mms = MinMaxScaler()
X_train_norm = mms.fit_transform(X_train)
X_test_norm = mms.transform(X_test)

produces:

X_train_norm[0]array([ 0.72043011,  0.20378151,  0.53763441,  0.30927835,  0.33695652,
        0.54316547,  0.73700306,  0.25      ,  0.40189873,  0.24068768,
        0.48717949,  1.        ,  0.5854251 ])

X_test_norm[0]array([ 0.72849462,  0.16386555,  0.47849462,  0.29896907,  0.52173913,
        0.53956835,  0.74311927,  0.13461538,  0.37974684,  0.4364852 ,
        0.32478632,  0.70695971,  0.60566802])

Post a Comment for "Python Valueerror: Non-broadcastable Output Operand With Shape (124,1) Doesn't Match The Broadcast Shape (124,13)"