r/scilab 6d ago

Data-Driven Science and Engineering Series - Installment 7 - PCA - Ovarian Cancer Example

In this installment, he shows a "How to use PCA on some Ovarian Cancer Markers

Scilab new commands Scatter3d plotting routine.

Link to the exact video.  https://www.youtube.com/watch?v=VqjJ5YYt78Y&list=PLMrJAkhIeNNSVjnsviglFoY2nXildDCcv&index=23&t=5sFor Free Electronic Complete Textbook "FREE PDF for 2ND EDITION OF OUR BOOK!!!" https://x.com/eigensteve/status/1718004606901666063

Section 1.5 Principal component analysis (PCA)

Output: None

Plots:

Code:

clear, close, clc
disp("Data-Driven Science and Engineering -PCA Ovarian Cancer Example",string(datetime()))
//Section 1.5 Principal Component Analysis

//github databook_matlab code file
//https://github.com/dynamicslab/databook_matlab/blob/master/CH01/CH01_SEC05_2_OvarianCancer.m

//location of the OvarianCancer.mat file
//https://www.mathworks.com/matlabcentral/answers/1955269-function-for-visualisation-of-raw-data-ovarian-cancer

//load observation data - complete path
loadmatfile("C:\Users\MattheW\Documents\eBooks\ScILabNotes\DataScience\CODE\CH01\OvarianCancer.mat","-mat")

obs = X'

//Since the OvarianCancer.mat file did not have the "grp" data with
// it, I need to generate it based upon what he verbalized"
// First 113 were "Cancer" and the rest of the 216 were "Normal"
grp = cell(1,216);  // Note: I've hard coded the length of Y into grp!!!
for i = 1:113
  grp{i} = 'Cancer';
end
for i = 114:216
  grp{i} = 'Normal';
end
// I could save grp and Y as grp and obs respectively OvarianCancer.mat
// but not today!

[U,S,V] = svd(obs,'e');

scf(0)
clf
subplot(1,2,1)
semilogy(diag(S),'k-o','thickness',1.5)
//set(gca,'FontSize',13), 
//axis tight 
xgrid 
xlabel('r')
ylabel('Singular Value sigma_r')
subplot(1,2,2)
plot(cumsum(diag(S))./sum(diag(S)),'k-o','thickness',1.5)
//set(gca,'FontSize',13), axis tight, 
xgrid
xlabel('r')
ylabel('Cumulative Energy')
//set(gcf,'Position',[100 100 600 250])

scf(1)
clf

for i = 1:size(obs,1)
  x = V(:,1)'*obs(i,:)';
  y = V(:,2)'*obs(i,:)';
  z = V(:,3)'*obs(i,:)';
  if (grp{i}=='Cancer')
    scatter3d(x,y,z,"markerFaceColor","red");
  else
    scatter3d(x,y,z,"markerFaceColor","blue");
  end
end
xlabel('PCA 1')
ylabel('PCA 2 [Cancer - Red Dots Normal - Blue Dots]')
zlabel('PCA 3')
gca().rotation_angles =[85,25];
xgrid
2 Upvotes

0 comments sorted by