@@ -9,64 +9,76 @@ import scalaglm.*
99
1010@ main def irisPca () =
1111
12- val url = " http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
13- val fileName = " iris.csv"
14- val imap = Map (
15- " Iris-setosa" -> 0 ,
16- " Iris-versicolor" -> 1 ,
17- " Iris-virginica" -> 2 )
12+ val url =
13+ " http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
14+ val fileName = " iris.csv"
15+ val imap =
16+ Map (" Iris-setosa" -> 0 , " Iris-versicolor" -> 1 , " Iris-virginica" -> 2 )
1817
19- // download the file to disk if it hasn't been already
20- val file = new java.io.File (fileName)
21- if ! file.exists then
22- val s = new java.io.PrintWriter (file)
23- val data = scala.io.Source .fromURL(url).getLines()
24- data.foreach(l => s.write(l.trim.split(',' ).
25- map(x => imap.getOrElse(x, x)).mkString(" " , " ," , " \n " )))
26- s.close
18+ // download the file to disk if it hasn't been already
19+ val file = new java.io.File (fileName)
20+ if ! file.exists then
21+ val s = new java.io.PrintWriter (file)
22+ val data = scala.io.Source .fromURL(url).getLines()
23+ data.foreach(l =>
24+ s.write(
25+ l.trim.split(',' ).map(x => imap.getOrElse(x, x)).mkString(" " , " ," , " \n " )
26+ )
27+ )
28+ s.close
2729
28- // read the file from disk
29- val mat = csvread(file)
30- println(" Mat Dim: " + mat.rows + " " + mat.cols)
31- val x = mat(:: , 0 to 3 )
32- println(" X Dim: " + x.rows + " " + x.cols)
33- val clas = mat(:: , 4 ).toDenseVector
30+ // read the file from disk
31+ val mat = csvread(file)
32+ println(" Mat Dim: " + mat.rows + " " + mat.cols)
33+ val x = mat(:: , 0 to 3 )
34+ println(" X Dim: " + x.rows + " " + x.cols)
35+ val clas = mat(:: , 4 ).toDenseVector
3436
35- println(" PCA with built-in Breeze version (like R princomp):" )
36- val pca = new PCA (x, covmat(x))
37- println(" Loadings:" )
38- println(pca.loadings)
39- println(" Stdev:" )
40- println(pca.sdev)
41- println(pca.scores(0 to 5 , :: ))
37+ println(" PCA with built-in Breeze version (like R princomp):" )
38+ val pca = new PCA (x, covmat(x))
39+ println(" Loadings:" )
40+ println(pca.loadings)
41+ println(" Stdev:" )
42+ println(pca.sdev)
43+ println(pca.scores(0 to 5 , :: ))
4244
43- println(" Now my version (like R prcomp):" )
44- val myPca = Pca (x, List (" S-L" ," S-W" ," P-L" ," P-W" ))
45- println(myPca.loadings) // loadings transposed
46- println(myPca.sdev)
47- myPca.summary
48- println(" Scores:" )
49- println(myPca.scores(0 to 5 , :: ))
50- myPca.plots.saveas(" IrisPcaDiag.png" )
45+ println(" Now my version (like R prcomp):" )
46+ val myPca = Pca (x, List (" S-L" , " S-W" , " P-L" , " P-W" ))
47+ println(myPca.loadings) // loadings transposed
48+ println(myPca.sdev)
49+ myPca.summary
50+ println(" Scores:" )
51+ println(myPca.scores(0 to 5 , :: ))
52+ myPca.plots.saveas(" IrisPcaDiag.png" )
5153
52- // scatter plot first 2 principal components
53- import breeze .plot .*
54- val fig = Figure (" PCA" )
55- val p = fig.subplot(0 )
56- val ind0 = (0 until x.rows) filter (i => clas(i) == 0 )
57- p += plot(myPca.scores(ind0, 0 ).toDenseVector,
58- myPca.scores(ind0, 1 ).toDenseVector, '.' , colorcode = " blue" )
59- val ind1 = (0 until x.rows) filter (i => clas(i) == 1 )
60- p += plot(myPca.scores(ind1, 0 ).toDenseVector,
61- myPca.scores(ind1, 1 ).toDenseVector, '.' , colorcode = " red" )
62- val ind2 = (0 until x.rows) filter (i => clas(i) == 2 )
63- p += plot(myPca.scores(ind2, 0 ).toDenseVector,
64- myPca.scores(ind2, 1 ).toDenseVector, '.' , colorcode = " green" )
65- fig.saveas(" IrisPca.png" )
66-
67- // Test without variable names
68- Pca (x).summary
54+ // scatter plot first 2 principal components
55+ import breeze .plot .*
56+ val fig = Figure (" PCA" )
57+ val p = fig.subplot(0 )
58+ val ind0 = (0 until x.rows) filter (i => clas(i) == 0 )
59+ p += plot(
60+ myPca.scores(ind0, 0 ).toDenseVector,
61+ myPca.scores(ind0, 1 ).toDenseVector,
62+ '.' ,
63+ colorcode = " blue"
64+ )
65+ val ind1 = (0 until x.rows) filter (i => clas(i) == 1 )
66+ p += plot(
67+ myPca.scores(ind1, 0 ).toDenseVector,
68+ myPca.scores(ind1, 1 ).toDenseVector,
69+ '.' ,
70+ colorcode = " red"
71+ )
72+ val ind2 = (0 until x.rows) filter (i => clas(i) == 2 )
73+ p += plot(
74+ myPca.scores(ind2, 0 ).toDenseVector,
75+ myPca.scores(ind2, 1 ).toDenseVector,
76+ '.' ,
77+ colorcode = " green"
78+ )
79+ fig.saveas(" IrisPca.png" )
6980
81+ // Test without variable names
82+ Pca (x).summary
7083
7184// eof
72-
0 commit comments