How to apply PCA for my dataset

조회 수: 11 (최근 30일)
shagun
shagun 2024년 4월 19일
댓글: shagun 2024년 4월 22일
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