You are on page 1of 5

❖ Practical No 03) Practical of Principal Component Analysis.

➢ Example No 01):-
> iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
| | | | | |
| | | | | |
| | | | | |
148 6.5 3.0 5.2 2.0 virginica
149 6.2 3.4 5.4 2.3 virginica
150 5.9 3.0 5.1 1.8 virginica

>
> Data<-iris[1:4]
> Data
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
| | | | |
| | | | |
| | | | |
148 6.5 3.0 5.2 2.0
149 6.2 3.4 5.4 2.3
150 5.9 3.0 5.1 1.8
> Data1<-iris[,-5]
> Data1
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2

> PC<-princomp(Data,cor = TRUE,scores = TRUE)


> PC
Call:
princomp(x = Data, cor = TRUE, scores = TRUE)

Standard deviations:
Comp.1 Comp.2 Comp.3 Comp.4
1.7083611 0.9560494 0.3830886 0.1439265

4 variables and 150 observations.


> summary(PC)
Importance of components:
Comp.1 Comp.2 Comp.3 Comp.4
Standard deviation 1.7083611 0.9560494 0.38308860 0.143926497
Proportion of Variance 0.7296245 0.2285076 0.03668922 0.005178709
Cumulative Proportion 0.7296245 0.9581321 0.99482129 1.000000000
> plot(PC)

➢ Output:
> plot(PC)
> plot(PC,type="l")

>
➢ Output:

> biplot(PC)

>
➢ Output:
➢ Example No:02):-
> MyData<-read.csv("E:\\Satyavan\\TYCS Practical\\Sem VI\\Data Scienc
e\\Principal Component Analysis.csv")
> MyData
Exam.No X12th.Percentage X10th.Percentage Performance
1 1 48 62 Poor
2 2 55 67 Average
3 3 56 66 Average
4 4 60 70 Moderate
5 5 85 63 Excellent
6 6 75 68 Satisfactory
| | | | |
| | | | |
| | | | |
37 37 52 67 Average
38 38 76 66 Satisfactory
39 39 96 63 Outstanding

>
> MData<-MyData[2:3]
> MData
X12th.Percentage X10th.Percentage
1 48 62
2 55 67
3 56 66
| | |
| | |
| | |
37 52 67
38 76 66
39 96 63

>
> PC<-princomp(MData,cor=TRUE,scores = TRUE)
> PC
Call:
princomp(x = MData, cor = TRUE, scores = TRUE)

Standard deviations:
Comp.1 Comp.2
1.030286 0.968768

2 variables and 39 observations.


> summary(PC)
Importance of components:
Comp.1 Comp.2
Standard deviation 1.0302856 0.9687680
Proportion of Variance 0.5307442 0.4692558
Cumulative Proportion 0.5307442 1.0000000
> plot(PC)

➢ Output:

> plot(PC,type="l")
>
➢ Output:
> biplot(PC)

➢ Output:

You might also like