-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIrisDensityPlot.scala
More file actions
75 lines (63 loc) · 2.73 KB
/
IrisDensityPlot.scala
File metadata and controls
75 lines (63 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package examples
import java.io.File
import de.dreambeam.veusz.components._
import de.dreambeam.veusz.format._
import de.dreambeam.veusz.util.SizeUnits._
import kantan.csv._
import kantan.csv.ops._
import kantan.csv.generic._
import smile.stat.distribution._
object IrisDensityPlot extends App {
implicit def vectorToArray(vector: Vector[Double]) = vector.toArray
/**
*
*
* @param over the x-values for which a probability value shall be estimated
* @param xs the data points fed into Kernel Density Estimation
* @return the predicated values
*/
def kde(over: Vector[Double])(xs: Array[Double]) = {
val kd = new KernelDensity(xs) // use rule of thumb
over.map(kd.p)
}
// Read CSV
val reader = new File("data/iris/iris.data").asUnsafeCsvReader[IrisRow](rfc.withoutHeader).toVector
// Group by Sepal Length
val sepalLengthGrouped = reader.groupBy(_.name).mapValues(_.map(_.sepalLength))
// Compute minimum and maximum value for x-Axis
val xMargin = 0.3 // with some extra margin on each side
val minXValue = math.max(0.0, sepalLengthGrouped.values.flatten.min - xMargin)
val maxXValue = sepalLengthGrouped.values.flatten.max + xMargin
val granularity = .001 // the resolution of the model; it makes the curve look smooth
val xData = minXValue to maxXValue by granularity toVector
// partially apply kde with xData
def kdeOverX = kde(xData)(_)
/* Setosa */
val xyPlotSetosa = XY(xData, kdeOverX(sepalLengthGrouped("Iris-setosa")), keyText="Setosa")
// Config Setosa
xyPlotSetosa.config.main.markerType = MarkerType.None
xyPlotSetosa.config.fillBelow.fillTo = FillTo.bottom
xyPlotSetosa.config.fillBelow.hide = false
xyPlotSetosa.config.fillBelow.transparency = 60
xyPlotSetosa.config.fillBelow.color = "#ef8188"
/* Versicolor */
val xyPlotVersicolor = XY(xData, kdeOverX(sepalLengthGrouped("Iris-versicolor")), keyText="Versicolor")
// Config Versicolor
xyPlotVersicolor.config.main.markerType = MarkerType.None
xyPlotVersicolor.config.fillBelow.fillTo = FillTo.bottom
xyPlotVersicolor.config.fillBelow.hide = false
xyPlotVersicolor.config.fillBelow.transparency = 60
xyPlotVersicolor.config.fillBelow.color = "#91e0a4"
/* Virginica */
val xyPlotVirginica = XY(xData, kdeOverX(sepalLengthGrouped("Iris-virginica")), keyText="Virginica")
// Config Virginica
xyPlotVirginica.config.main.markerType = MarkerType.None
xyPlotVirginica.config.fillBelow.fillTo = FillTo.bottom
xyPlotVirginica.config.fillBelow.hide = false
xyPlotVirginica.config.fillBelow.transparency = 60
xyPlotVirginica.config.fillBelow.color = "13b7d8"
val graph = Graph(xyPlotSetosa, xyPlotVersicolor, xyPlotVirginica, Key())
val page = Page(graph)
page.config.height = 10 cm
page.show("IrisDensityPlot")
}