'scaling data to specific range in python

I would like to scale an array of size [192,4000] to a specific range. I would like each row (1:192) to be rescaled to a specific range e.g. (-840,840). I run a very simple code:

import numpy as np
from sklearn import preprocessing as sp
sample_mat = np.random.randint(-840,840, size=(192, 4000))
scaler = sp.MinMaxScaler(feature_range=(-840,840))
scaler = scaler.fit(sample_mat)
scaled_mat= scaler.transform(sample_mat)

This messes up my matrix range, even when max and min of my original matrix is exactly the same. I can't figure out what is wrong, any idea?



Solution 1:[1]

MinMaxScaler support feature_range argument on initialization that can produce the output in a certain range. scaler = MinMaxScaler(feature_range=(1, 2)) will yield output in the (1,2) range

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CloudBalancing