Python, ValueError, BroadCast error using SKLearn Preproccesing

I try to run the standard SKLearn Preprocessing scaling function and I get the following error:

from sklearn import preprocessing as pre
scaler = pre.StandardScaler().fit(t_train)
t_train_scale = scaler.transform(t_train)
t_test_scale = scaler.transform(t_test)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-149-c0133b7e399b> in <module>()
      4 scaler = pre.StandardScaler().fit(t_train)
      5 t_train_scale = scaler.transform(t_train)

----> 6 t_test_scale = scaler.transform(t_test)

C:\Users\****\Anaconda\lib\site-packages\sklearn\preprocessing\data.pyc in transform(self, X, y, copy)
    356         else:
    357             if self.with_mean:
--> 358                 X -= self.mean_
    359             if self.with_std:
    360                 X /= self.std_

ValueError: operands could not be broadcast together with shapes (40000,59) (119,) (40000,59) 

I understand that the forms do not match. The train and test data have different lengths, so how could I convert the data?

+5
source share
4 answers

print the output with t_train.shape[1]andt_test.shape[1]

StandardScaler , . , ( , ..) . , t_train, t_test.

, :

ValueError: operands could not be broadcast together with shapes (40000,59) (119,) (40000,59)

, , t_train.shape[1] - 59 t_test.shape[1] - 119. , 59 119 .

StandardScaler?

+4

" "? ?

, , , , . , (PCA, SVD ..) - . , , .

0

. :

t_train.transpose()
t_test.transpose()

, 2x , "" - ...

0

t_train form (x, 119), while t_test form (40000.59). If you want to use the same scaling object for conversion, then your data should always have the same number of columns. Since you approach the scaler on t_train, that’s the reason you get the question when you try to turn t_test.

0
source

Source: https://habr.com/ru/post/1613752/


All Articles