0
$\begingroup$

I'm trying to make a sales prediction using the column X = item_amount and y = item_price_total, I'm confused whether it's okay to normalize data on the dependent variable using minmaxscalar?

With the data that I have more or less like this:

|    date     | item_amount | item_price_total |   
|  :--------: |   :--------  |     :--------     |
|  2018-01-07 |       148    |      13750000     |
|  2018-01-14 |       749    |      93921000     |
|  2018-01-21 |      3175    |     439218700     |
|  2018-01-28 |        23    |       3029700     |
|  2018-02-04 |       203    |      41661000     |

item_amount = [148, 749, 3175, 23, 203]
item_price_total = [13750000, 93921000, 439218700, 3029700, 41661000]

And the code i will use is like this:

import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import mean_squared_error, r2_score

# Determine the X and y variables
X = df['item_amount'].values.reshape(-1, 1)
y = df['item_price_total'].values.reshape(-1, 1)

# Normalize data using MinMaxScaler
scalar = MinMaxScaler()
X_scaled = scalar.fit_transform(X)
y_scaled = scalar.fit_transform(y)

# Dividing the dataset into training data and test data
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled, test_size=0.3, shuffle=False)

# Create a linear regression model
model_linreg = LinearRegression()
# Define parameters
param_lr = {
    "positive": [True, False],
    "fit_intercept": [True, False],
    "copy_X": [True, False],
    "n_jobs": [4, 3, 6, 5, 9, 12]
}

# Create a support vector regression model
model_svr = SVR()
# Define parameters
param_svr = {
    "C": [0.5, 1, 10, 100, 1000],
    "gamma": [0.001, 0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 2, 5],
    "epsilon": [0.001, 0.01, 0.1, 1, 2, 4],
    "kernel": ["rbf", "poly", "sigmoid", "linear"]
    }

# Train models
grid_search_linreg = GridSearchCV(model_linreg, param_lr)
grid_search_linreg.fit(X_train, y_train)
grid_search_svr = GridSearchCV(model_svr, param_svr, n_jobs=-1, verbose=2)
grid_search_svr.fit(X_train, y_train.ravel())

# Predict the y-value
y_pred = grid_search_linreg.predict(X_test)
y_prediction = grid_search_svr.predict(X_test)

# Calculate R2
r2_linreg = r2_score(y_test, y_pred)
print("R2 Score: ", r2_linreg)
r2_svr = r2_score(y_test, y_prediction)
print("R2 Score: ", r2_svr)

# Calculate RMSE
rmse_linreg = np.sqrt(mean_squared_error(y_test, y_pred))
print("RMSE: ", rmse_linreg)
rmse_svr = np.sqrt(mean_squared_error(y_test, y_prediction))
print("RMSE: ", rmse_svr)

In the way I did, I got pretty good R2 and RMSE values. But if I normalize only the independent variable (X), the R2 and RMSE values will be bad.

# Determine the X and y variables
X = df['item_amount '].values.reshape(-1, 1)
y = df['item_price_total '].values.reshape(-1, 1)

# Dividing the dataset into training data and test data
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled, test_size=0.3, shuffle=False)

# Normalize data using MinMaxScaler
scalar = MinMaxScaler()
X_train = scalar.fit_transform(X_train)
X_test = scalar.transform(X_test)

Is the value of y (dependent variable) based on the data I have, normalization is required using minmaxscalar? Is there a journal/paper reference that I can read? Thank You!

$\endgroup$

1 Answer 1

1
$\begingroup$

You can scale your output parameter as long as your are inverse transforming again after generating the prediction, In many cases finding a model that fits the scaled data can be found more easily than unscaled data. One important thing to consider in this scenario is the loss (rmse or mse) looks very less on scaled data and data is scaled. But at a high level its no harm in scaling.

$\endgroup$
2
  • $\begingroup$ Thank you for your answer, but I want to ask from your answer what does "But at a high level its no harm in scaling" mean? And do you know of any reference journals/papers that have tried scaling the output parameters that I can read? Thank You @SaandeepSreerambatla $\endgroup$
    – Fatur
    Commented Jun 18, 2023 at 7:39
  • $\begingroup$ What I meant by No-harm is -> You need to consider the error on scaled data it looks very less. $\endgroup$ Commented Jul 5, 2023 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.