Skip to content

Commit 95cfc8b

Browse files
committed
Made Housing example code more readable
1 parent e9042f7 commit 95cfc8b

File tree

2 files changed

+49
-83
lines changed

2 files changed

+49
-83
lines changed

dynaml-examples/src/main/scala-2.11/io/github/mandar2812/dynaml/examples/TestGPHousing.scala

Lines changed: 46 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -24,107 +24,75 @@ import io.github.mandar2812.dynaml.analysis.VectorField
2424
import io.github.mandar2812.dynaml.evaluation.RegressionMetrics
2525
import io.github.mandar2812.dynaml.kernels._
2626
import io.github.mandar2812.dynaml.models.GPRegressionPipe
27-
import io.github.mandar2812.dynaml.models.gp.GPRegression
27+
import io.github.mandar2812.dynaml.models.gp.{AbstractGPRegressionModel, GPRegression}
2828
import io.github.mandar2812.dynaml.pipes.{BifurcationPipe, DataPipe}
2929

3030
/**
3131
* Created by mandar on 15/12/15.
3232
*/
3333
object TestGPHousing {
34-
35-
def apply(kernel: LocalScalarKernel[DenseVector[Double]],
34+
35+
36+
type Features = DenseVector[Double]
37+
type Output = Double
38+
type Pattern = (Features, Output)
39+
type Data = Stream[Pattern]
40+
type DataAlt = Seq[Pattern]
41+
type TTData = (Data, Data)
42+
type Kernel = LocalScalarKernel[Features]
43+
type Scales = (Features, Features)
44+
type GP = AbstractGPRegressionModel[Data, Features]
45+
type GPAlt = AbstractGPRegressionModel[DataAlt, Features]
46+
type PredictionsAndErrBars = Seq[(Features, Output, Output, Output, Output)]
47+
type PredictionsAndOutputs = List[(Output, Output)]
48+
49+
50+
def apply(kernel: Kernel,
3651
bandwidth: Double = 0.5,
37-
noise: LocalScalarKernel[DenseVector[Double]],
52+
noise: Kernel,
3853
trainFraction: Double = 0.75,
3954
columns: List[Int] = List(13,0,1,2,3,4,5,6,7,8,9,10,11,12),
4055
grid: Int = 5, step: Double = 0.2, globalOpt: String = "ML",
41-
stepSize: Double = 0.01, maxIt: Int = 300): Unit =
56+
stepSize: Double = 0.01, maxIt: Int = 300, policy: String = "GS"): Unit =
4257
runExperiment(kernel, bandwidth,
4358
noise, (506*trainFraction).toInt, columns,
44-
grid, step, globalOpt,
59+
grid, step, globalOpt, policy,
4560
Map("tolerance" -> "0.0001",
4661
"step" -> stepSize.toString,
4762
"maxIterations" -> maxIt.toString
4863
)
4964
)
5065

51-
def apply(kern: String,
52-
bandwidth: Double,
53-
noise: LocalScalarKernel[DenseVector[Double]],
54-
trainFraction: Double,
55-
columns: List[Int],
56-
grid: Int, step: Double, globalOpt: String,
57-
stepSize: Double, maxIt: Int): Unit = {
58-
59-
implicit val field = VectorField(columns.length - 1)
60-
61-
val kernel: LocalScalarKernel[DenseVector[Double]] =
62-
kern match {
63-
case "RBF" =>
64-
new RBFKernel(bandwidth)
65-
case "Cauchy" =>
66-
new CauchyKernel(bandwidth)
67-
case "Laplacian" =>
68-
new LaplacianKernel(bandwidth)
69-
case "RationalQuadratic" =>
70-
new RationalQuadraticKernel(bandwidth)
71-
case "FBM" => new FBMKernel(bandwidth)
72-
case "Student" => new TStudentKernel(bandwidth)
73-
case "Periodic" => new PeriodicKernel(bandwidth, bandwidth)
74-
}
75-
76-
val num_training = 506*trainFraction
77-
78-
runExperiment(kernel, bandwidth,
79-
noise, num_training.toInt, columns,
80-
grid, step, globalOpt,
81-
Map("tolerance" -> "0.0001",
82-
"step" -> stepSize.toString,
83-
"maxIterations" -> maxIt.toString
84-
)
85-
)
86-
87-
}
88-
89-
def runExperiment(kernel: LocalScalarKernel[DenseVector[Double]],
66+
def runExperiment(kernel: Kernel,
9067
bandwidth: Double = 0.5,
91-
noise: LocalScalarKernel[DenseVector[Double]],
68+
noise: Kernel,
9269
num_training: Int = 200, columns: List[Int] = List(40,16,21,23,24,22,25),
9370
grid: Int = 5, step: Double = 0.2,
94-
globalOpt: String = "ML", opt: Map[String, String]): Unit = {
95-
96-
97-
val startConf = kernel.state ++ noise.state
98-
99-
val modelpipe = new GPRegressionPipe[
100-
GPRegression, (
101-
(Stream[(DenseVector[Double], Double)], Stream[(DenseVector[Double], Double)]),
102-
(DenseVector[Double], DenseVector[Double])),
103-
DenseVector[Double]](
104-
(tt: (
105-
(Stream[(DenseVector[Double], Double)], Stream[(DenseVector[Double], Double)]),
106-
(DenseVector[Double], DenseVector[Double]))) => tt._1._1,
107-
kernel, noise) >
108-
modelTuning(startConf, globalOpt, grid, step) >
109-
DataPipe((modelCouple: (GPRegression, Map[String, Double])) => {
110-
modelCouple._1.setState(modelCouple._2)
71+
globalOpt: String = "ML", pol: String = "GS",
72+
opt: Map[String, String]): Unit = {
73+
74+
75+
val startConf = kernel.effective_state ++ noise.effective_state
76+
77+
val modelpipe =
78+
GPRegressionPipe[(TTData, Scales), Features]((tt: (TTData, Scales)) => tt._1._1, kernel, noise) >
79+
gpTuning[DataAlt, Features](startConf, globalOpt, grid, step, opt("maxIterations").toInt, pol) >
80+
DataPipe((modelCouple: (GPAlt, Map[String, Double])) => {
11181
modelCouple._1
11282
})
11383

114-
val testPipe = DataPipe((testSample: (GPRegression,
115-
(Stream[(DenseVector[Double], Double)],
116-
(DenseVector[Double], DenseVector[Double]))
117-
)) => (testSample._1.test(testSample._2._1), testSample._2._2)) >
118-
DataPipe((res: (Seq[(DenseVector[Double], Double, Double, Double, Double)],
119-
(DenseVector[Double], DenseVector[Double]))) =>
120-
res._1.map(i => (i._3, i._2)).toList.map{l => (l._1*res._2._2(-1) + res._2._1(-1),
121-
l._2*res._2._2(-1) + res._2._1(-1))}
122-
) > DataPipe((scoresAndLabels: List[(Double, Double)]) => {
123-
val metrics = new RegressionMetrics(scoresAndLabels,
124-
scoresAndLabels.length)
84+
val testPipe = DataPipe((testSample: (GPAlt, (Data, Scales))) => {
85+
val (model, (data, scales)) = testSample
86+
(model.test(data), scales) }) >
87+
DataPipe((res: (PredictionsAndErrBars, Scales)) =>
88+
res._1
89+
.map(i => (i._3, i._2)).toList
90+
.map(l => (l._1*res._2._2(-1) + res._2._1(-1), l._2*res._2._2(-1) + res._2._1(-1)))) >
91+
DataPipe((scoresAndLabels: PredictionsAndOutputs) => {
12592

93+
val metrics = new RegressionMetrics(scoresAndLabels, scoresAndLabels.length)
12694
metrics.setName("MEDV")
127-
95+
//Print the evaluation results
12896
metrics.print()
12997
metrics.generatePlots()
13098
})
@@ -147,11 +115,9 @@ object TestGPHousing {
147115
trainTestGaussianStandardization >
148116
BifurcationPipe(
149117
modelpipe,
150-
DataPipe((tt: (
151-
(Stream[(DenseVector[Double], Double)], Stream[(DenseVector[Double], Double)]),
152-
(DenseVector[Double], DenseVector[Double]))) => (tt._1._2, tt._2)
153-
)
154-
) > testPipe
118+
DataPipe((tt: (TTData, Scales)) => (tt._1._2, tt._2))
119+
) >
120+
testPipe
155121

156122
trainTestPipe.run(("data/housing.data", "data/housing.data"))
157123

scripts/kernelModels.sc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ val other_kernel1 = new CauchyKernel(1.0)
1212
val matern = new GenericMaternKernel[DenseVector[Double]](1.0, 2)
1313

1414
val otherSumK = kernel + other_kernel
15-
val noise = new DiracKernel(1.5)
15+
val noiseKernel = new DiracKernel(1.5)
1616

17-
//noise.block_all_hyper_parameters
17+
noiseKernel.block_all_hyper_parameters
1818

19-
AbottPowerPlant(other_kernel, noise,
19+
AbottPowerPlant(other_kernel, noiseKernel,
2020
opt = Map("globalOpt" -> "GPC", "grid" -> "2",
2121
"step" -> "0.5", "tolerance" -> "0.0001",
2222
"maxIterations" -> "10", "policy" -> "CSA"), num_training = 1025,

0 commit comments

Comments
 (0)