# Function for principal component analysis (PCA)?

**URL:** <https://community.wolfram.com/t/function-for-principal-component-analysis-pca/16870>\
**Category:** Questions\
**Tags:** data-science, statistics-and-probability, machine-learning\
**Created:** [October 14, 2020, 7:30am UTC](https://community.wolfram.com/t/function-for-principal-component-analysis-pca/16870 "2020-10-14T07:30:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![predecessor77](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/predecessor77/32/6262_2.png) [@predecessor77](https://community.wolfram.com/u/predecessor77)\
**Post date:** [October 14, 2020, 7:30am UTC](https://community.wolfram.com/t/function-for-principal-component-analysis-pca/16870/1 "2020-10-14T07:30:09Z")

</div>

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"]

```

---

<div class="post-metadata">

**Author:** ![dauphinester](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dauphinester/32/9518_2.png) [@dauphinester](https://community.wolfram.com/u/dauphinester)\
**Post date:** [October 14, 2020, 8:10am UTC](https://community.wolfram.com/t/function-for-principal-component-analysis-pca/16870/2 "2020-10-14T08:10:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [October 14, 2020, 2:18pm UTC](https://community.wolfram.com/t/function-for-principal-component-analysis-pca/16870/3 "2020-10-14T14:18:20Z")

</div>

Hi Ming-Chou,

WL has a [`PrincipalComponents`](http://reference.wolfram.com/language/ref/PrincipalComponents.html) 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`](https://resources.wolframcloud.com/FunctionRepository/resources/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`.

---

<div class="post-metadata">

**Author:** ![predecessor77](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/predecessor77/32/6262_2.png) [@predecessor77](https://community.wolfram.com/u/predecessor77)\
**Post date:** [October 15, 2020, 1:24am UTC](https://community.wolfram.com/t/function-for-principal-component-analysis-pca/16870/4 "2020-10-15T01:24:02Z")

</div>

Thanks for your suggestions. Let me learn new skills.

---

<div class="post-metadata">

**Author:** ![predecessor77](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/predecessor77/32/6262_2.png) [@predecessor77](https://community.wolfram.com/u/predecessor77)\
**Post date:** [October 15, 2020, 1:25am UTC](https://community.wolfram.com/t/function-for-principal-component-analysis-pca/16870/5 "2020-10-15T01:25:19Z")

</div>

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