Message Boards Message Boards


Function for principal component analysis (PCA)?

Posted 3 months ago
4 Replies
3 Total Likes

I downloaded the Boston housing price data collection from the Internet for practice. Related blogs discuss principal component analysis (PCA). But I did not search for relevant examples, commands, or functions on the Wolfram website. Are there any readily commands or functions for PCA in Mathematica?

{key, data} = {First@#, 0.0 + ToExpression@Rest@#} &@
BostonHousing.csv", {"CSV", "RawData"}];
{test, train} = 
  TakeDrop[#, 100] &@((#[[1 ;; -2]] -> #[[-1]]) & /@ data);
p = Predict[train];
m = PredictorMeasurements[p1, test];
4 Replies

You have two little programs about PCR and PCC in the book " Geographical Models with Mathematica" (pp 43-45)

I have borrowed this book from the library~~I am currently studying! Thanks for your suggestions!

Posted 3 months ago

Hi Ming-Chou,

WL has a PrincipalComponents function that may work for you.

A suggestion on your code. Evaluating ToExpression on data from an external source can pose a security risk. Here is an alternative

dataset = 
    HeaderLines -> 1]; 

dataPredict = dataset[All, Most@Values@# -> Last@Values@# &] // Normal

It is important to randomize the data before analyzing it to remove any bias from the way the data is sorted. The ResourceFunction TrainTestSplit is useful for this.

{train, test} = ResourceFunction["TrainTestSplit"][dataPredict];
p = Predict[train];

There is a typo in the code, p should be passed to PredictorMeasurements, not p1.

Thanks for your suggestions. Let me learn new skills.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract