Issue
I try to use MinMaxScaler
to normalize a sparse matrix, and I set the range to [-1, 1], but the result only gives me -1 and 1 int values. I don't know what I should change the setting of the scaler.
My output is like this :
list_1: [[11.41822609 -1.88521764 3.23520926]
[10.21012252 -1.05147544 6.925866 ]]
scaled: [[ 1. -1. -1.]
[-1. 1. 1.]]
transfer: [[11.41822609 -1.88521764 3.23520926]
[10.21012252 -1.05147544 6.925866 ]]
my code :
file = h5py.File('HDF5s\\xy_train&std_data.hdf5', 'r')
dataset = numpy.array(file['s1']['V1']['cnn1']['n_train'])
file.close()
list_1 = numpy.array(dataset[:2, :3], dtype='float64')
scaler_1 = MinMaxScaler(feature_range=(-1, 1))
s_list_1 = scaler_1.fit_transform(list_1)
print('list_1: ', list_1, '\n')
print('scaled: ', s_list_1, '\n')
print('transfer: ', scaler_1.inverse_transform(s_list_1), '\n')
Solution
This is because you've fit your MinMaxScaler()
to a numpy array with shape (2,3)
. MinMaxScaler()
will scale each feature (i.e. each column) separately. In the case of the array you supplied, there are only 2 values (i.e rows) for each feature, so the higher value becomes the upper bound for that feature (1), and the lower value becomes the lower bound (-1), hence all values are either -1 or 1.
If you fit the MinMaxScaler to a larger sample (i.e. more rows) then you will see the scaled values are no longer just -1 and 1.
Answered By - mdgrogan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.