Standardization fitlm, weird results?

4 vues (au cours des 30 derniers jours)
JoE
JoE le 7 Mai 2021
Modifié(e) : Asvin Kumar le 10 Mai 2021
Hi guys,
im new to machine learning and Im trying my best to get used to it. Right now Im trying to find a way to standardize my obeservations and use it with fitlm()
The problem: Im not in the range of the correct scale of the predictions after standardization. How do I fix this problem? Whats wrong with my code or with my intenion?
Example code:
load carsmall
X = [Weight,Horsepower,Acceleration];
Fulldata = [X,MPG];
Fulldata = rmmissing(Fulldata);
Testsample = [3504,130,12];
VarNames = {'Weight','Horsepower','Acceleration','MPG'};
%No standardization - dataset
Fulldataclean=array2table(Fulldata,"VariableNames",VarNames);
% Standardization - dataset
Fulldatastd = zscore(Fulldata);
Fulldatastd = array2table(Fulldatastd,"VariableNames",VarNames);
% Train model
lm = fitlm(Fulldataclean)
lm2 = fitlm(Fulldatastd)
%Predictions
test = predict(lm,Testsample)
test2 = predict(lm2,Testsample)
Output:
test = 19.3335
test2 = -2.3181e+03

Réponses (1)

Asvin Kumar
Asvin Kumar le 10 Mai 2021
Modifié(e) : Asvin Kumar le 10 Mai 2021
The reason your outputs aren't even in the same range is because you're comparing apples to oranges -- in two different places.
  1. Your training data (Fulldatastd) which is passed to fitlm normalizes the response 'MPG' too. So, test2 will always be in a different range, i.e, the normalized range.
  2. The training data (Fulldatastd) which is passed to fitlm is normalized but your test data isn't. So, you're predicting on an input that is out of the range of the training data.
Here's a modified version of your attached code. You can see that the outputs are now equal. Here are the changes that I've made:
  1. Normalized only the predictors - X
  2. Normalized the test point for testing
  3. Converted the calls to fitglm from fitglm(tbl) form to fitglm(x,y) form for clarity
load carsmall
X = [Weight,Horsepower,Acceleration];
Fulldata = [X,MPG];
Fulldata = rmmissing(Fulldata);
Xclean = Fulldata(:,1:3);
MPGclean = Fulldata(:,4);
Testsample = [3504,130,12];
Testsamplestd = (Testsample-mean(Xclean,1))./std(Xclean);
VarNames = {'Weight','Horsepower','Acceleration','MPG'};
% Standardization - dataset
Xstd = zscore(Xclean);
% Train model
lm = fitlm(Xclean,MPGclean);
lm2 = fitlm(Xstd, MPGclean);
%Predictions
test = predict(lm,Testsample)
test = 19.3335
test2 = predict(lm2,Testsamplestd)
test2 = 19.3335

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by