フィルターのクリア

Why fitlm function is giving wierd results?

2 ビュー (過去 30 日間)
Devendra
Devendra 2024 年 4 月 13 日
コメント済み: Manikanta Aditya 2024 年 4 月 14 日
I am using following code
% PCA
[coeff, score, ~, ~, explained] = pca(X);
X_pca = score(:, 1:10);
% Split data
cv = cvpartition(size(X_pca, 1), 'HoldOut', 0.2);
idxTrain = training(cv);
idxTest = test(cv);
X_train = X_pca(idxTrain, :);
X_test = X_pca(idxTest, :);
Y_train = Y(idxTrain);
Y_test = Y(idxTest);
reg = fitlm(X_train, Y_train);
However, the rusults fitlm are coming wierd. Please suggest me how to get correct results.
Deva
  7 件のコメント
Devendra
Devendra 2024 年 4 月 14 日
Thanks a lot for your kind guidance. Certainly it has helped me to understand the basics of prediction of data.🙏🙏 Devendra
Manikanta Aditya
Manikanta Aditya 2024 年 4 月 14 日
Thank you! Good to know.

サインインしてコメントする。

採用された回答

Manikanta Aditya
Manikanta Aditya 2024 年 4 月 13 日
Hope you are doing great!
The error message you’re seeing is because the predict function is expecting an input with the same number of columns as the original data used to train the model. In your case, the model was trained with scoreTrain which has more than 3 columns, but you’re trying to predict with scoreTest which only has 3 columns (the principal components).
The issue arises from this line of code:
scoreTest = (X_test - mu)*coeff(:,1:idx)
Here, you’re reducing the dimensionality of your test set to 3 principal components, but your model was trained on the full set of principal components in scoreTrain.
To fix this, you should also limit the number of principal components in scoreTrain to 3. Here’s how you can do it:
scoreTrain = scoreTrain(:,1:idx);
reg = fitlm(scoreTrain, Y_train,'y ~ x1*x2*x3-x1:x2:x3');
Now, scoreTrain and scoreTest have the same number of columns, and you should be able to use the predict function without errors. Remember, the dimensions of the input for training and prediction must always match.
I hope this helps, let me know.
  4 件のコメント
Image Analyst
Image Analyst 2024 年 4 月 13 日
Because you're deleting your posts, you will probably have difficulty finding people to want to help you anymore.
Devendra
Devendra 2024 年 4 月 13 日
I am very sorry for my mistake. It will not happen again. Actually I deleted the post before realizing that already my friends have commented on it. My real intention was not to waste time of my friends on this issue since the problem is resolved. However it will not happen again in future. Devendra

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by