sklearn StandardScaler returns all zeros

前端 未结 1 474
盖世英雄少女心
盖世英雄少女心 2021-01-12 04:24

I have a sklearn StandardScaler saved from a previous model and am trying to apply it to new data

scaler = myOldStandardScaler
print(\"ORIG:\",          


        
相关标签:
1条回答
  • 2021-01-12 04:57

    When you're trying to apply fit_transform method of StandardScaler object to array of size (1, n) you obviously get all zeros, because for each number of array you subtract from it mean of this number, which equal to number and divide to std of this number. If you want to get correct scaling of your array, you should convert it to array with size (n, 1). You can do it this way:

    import numpy as np
    
    X = np.array([1, -4, 5, 6, -8, 5]) # here should be your X in np.array format
    X_transformed = scaler.fit_transform(X[:, np.newaxis])
    

    In this case you get Standard scaling for one object by its features, that's not you're looking for.
    If you want to get scaling by one feature of 3 objects, you should pass to fit_transform method array of size (3, 1) with values of certain feature corresponding to each object.

    X = np.array([0.00000000e+00, 9.49627142e-04, 3.19029839e-04])
    X_transformed = scaler.fit_transform(X[:, np.newaxis]) # you should get
    # array([[-1.07174217], [1.33494964], [-0.26320747]]) you're looking for
    

    And if you want to work with already fitted StandardScaler object, you shouldn't use fit_transform method, beacuse it refit object with new data. StandardScaler has transform method, which work with single observation:

    X = np.array([1, -4, 5, 6, -8, 5]) # here should be your X in np.array format
    X_transformed = scaler.transform(X.reshape(1, -1))
    
    0 讨论(0)
提交回复
热议问题