How to apply PCA for my dataset

11 次查看(过去 30 天)
Hi,
I am trying to apply pca on my dataset but I am not able to get bioplot. I just need bioplot for my data. Please help me. I am very new to Matlab.
I have attached by dataset
Thanks

采纳的回答

Taylor
Taylor 2024-4-19
The Reduce Dimensionality Live Task is perfect for this. Select "show code" at the bottom of the task and from there you can modify the code to your needs.
  6 个评论
shagun
shagun 2024-4-19
thanks, but how should i put variable name on my graph.
Taylor
Taylor 2024-4-22
You can use the legend, xlabel, and ylabel functions to label your plot.

请先登录,再进行评论。

更多回答(1 个)

shagun
shagun 2024-4-22
Thank you, But i want my variable name on the graph like same as attached in screenshot.
Thanks
  3 个评论
shagun
shagun 2024-4-22
I have tried but I am getting this error
load (Datagroup)
% Extract numeric variables from table
numericVars = varfun(@isnumeric,Datagroup,"output","uniform");
numericPredictorTable = Datagroup(:, numericVars);
% Convert table to matrix
numericPredictorMatrix = table2array(varfun(@double, numericPredictorTable));
% Principal Component Analysis
[coeffs, transformedData, ~, ~, explained] = pca(numericPredictorMatrix);
% Compute number of components from explained variance
enoughExplained = cumsum(explained)/sum(explained) >= 95/100;
numberOfComponentsToKeep = find(enoughExplained, 1);
disp("The number of components needed to explain at least 95% of the variance is "+ num2str(numberOfComponentsToKeep))
% Display results
figure
colorMap = colormap("lines");
barPlot = bar(explained);
hold on;
plot(cumsum(explained),"*-","Color",colorMap(3,:));
hold off;
barPlot(1).FaceColor = "flat";
barPlot(1).CData(1:numberOfComponentsToKeep,:) = repmat( ...
colorMap(2,:),[numberOfComponentsToKeep 1]);
label = ["Selected components"; sprintf("Cumulative explained\nvariance")];
legend(label,"Location","best");
title("Scree Plot of Explained Variances")
xlabel("Principal component")
ylabel("Variance explained (%)")
clear barPlot colorMap label explained
figure
scatter(transformedData(:,1),transformedData(:,2), "Marker", ".");
title("Scatter Plot of Principal Components")
xlabel("Component 1")
ylabel("Component 2")
variableNames = {'TSSLV10', 'TSSLV20', 'TSSLV30', 'TSSLV40', 'TSSLV50', 'TSSLV60', 'TSSLV70', 'TSSLV80', 'TSSLV90', ...
'TPLV10', 'TPLV20', 'TPLV30', 'TPLV40', 'TPLV50', 'TPLV60', 'TPLV70', 'TPLV80', 'TPLV90', ...
'SRPLV10', 'SRPLV20', 'SRPLV30', 'SRPLV40', 'SRPLV50', 'SRPLV60', 'SRPLV70', 'SRPLV80', 'SRPLV90', ...
'RDep10', 'RDep20', 'RDep30', 'RDep40', 'RDep50', 'RDep60', 'RDep70', 'RDep80', 'RDep90', ...
'AI10', 'AI20', 'AI30', 'AI40', 'AI50', 'AI60', 'AI70', 'AI80', 'AI90', ...
'RD10', 'RD20', 'RD30', 'RD40', 'RD50', 'RD60', 'RD70', 'RD80', 'RD90', 'TRD', 'AI', 'RD', 'ADP (HR)'};
figure
biplot(coeffs(:,[1 2],'VarLabels', variableNames));
title("PCA Biplot")
xlabel("Component 1")
ylabel("Component 2")
clear coeffs enoughExplained explained numericPredictorMatrix numericPredictorTable varLabels
transformedData = transformedData(:,1:numberOfComponentsToKeep);
clear numberOfComponentsToKeep
transformedData = array2table(transformedData);
transformedData = [transformedData,Datagroup(:,~numericVars)];
clear numericVars
Unable to use a value of type cell as an index.
shagun
shagun 2024-4-22
I am getting this error of Unable to use a value of type cell as an indexUnable to use a value of type cell as an index

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dimensionality Reduction and Feature Extraction 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by