Message Boards Message Boards

Function for principal component analysis (PCA)?

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?

Remove["Global`*"]
{key, data} = {First@#, 0.0 + ToExpression@Rest@#} &@
   Import["https://raw.githubusercontent.com/selva86/datasets/master/\
BostonHousing.csv", {"CSV", "RawData"}];
{test, train} = 
  TakeDrop[#, 100] &@((#[[1 ;; -2]] -> #[[-1]]) & /@ data);
p = Predict[train];
m = PredictorMeasurements[p1, test];
m["Properties"]
POSTED BY: Tsai Ming-Chou
4 Replies

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

POSTED BY: Tsai Ming-Chou

Thanks for your suggestions. Let me learn new skills.

POSTED BY: Tsai Ming-Chou
Posted 3 years 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 = 
  Import["https://raw.githubusercontent.com/selva86/datasets/master/BostonHousing.csv",
    "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.

POSTED BY: Rohit Namjoshi

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

POSTED BY: André Dauphiné
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract