Reputation: 179
As explained in similar question, one can easily test the data when you have a set of samples. If the user has to predict the target of a single sample, then how to proceed. Please help.
Thanks.
Upvotes: 0
Views: 1688
Reputation: 5696
You can use the same MinMaxScaler()
object you've used while training to transform your single instance. Here's an example.
# training data
X_train = np.array([[1, 2], [3, 4], [5, 6]])
y_train = np.array([1, 0])
# scaler
scaler = MinMaxScaler().fit(X_train)
Scaling X_train
:
X_train_scaled = scaler.transform(X_train)
Train the model using X_train_scaled and y_train ...
Predicting on the new sample np.array([7, 8])
:
new_sample = np.array([7, 8]).reshape(1, -1) # because the scaler expects a 2D array
scaler.transform(new_sample) # pass this to model.predict()
Edit:
How Min-Max Normalization works:
The following transformation is applied to each feature (Wikipedia Link):
We will apply that to X_train
X_train = np.array([[1, 2], [3, 4], [5, 6]])
array([[1, 2],
[3, 4],
[5, 6]])
# min, max of each feature
mn = np.min(X_train, axis=0) # array([1, 2])
mx = np.max(X_train, axis=0) # array([5, 6])
Calculating the scaled version:
(X_train - mn) / (mx - mn)
array([[0. , 0. ],
[0.5, 0.5],
[1. , 1. ]])
The above matches with the result of:
scaler = MinMaxScaler().fit(X_train)
X_train_scaled = scaler.transform(X_train)
array([[0. , 0. ],
[0.5, 0.5],
[1. , 1. ]])
When you supply a new input vector, the same transformation should apply using the above mn
and mx
values
new_smaple = np.array([7, 8]).reshape(1, -1)
(new_sample - mn) / (mx - mn)
array([[1.5, 1.5]])
This matches the output of scaler.transform(new_sample)
Also, you can extract min, max from a fitted MinMaxScaler object using scaler.data_min_
and scaler.data_max_
which will match the above mn
and mx
.
Upvotes: 4