-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnivariate_linearRegression.java
More file actions
174 lines (151 loc) · 5.62 KB
/
Univariate_linearRegression.java
File metadata and controls
174 lines (151 loc) · 5.62 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package univariate_linearregression;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Driver;
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
/**
*
* @author admin
*/
public class Univariate_linearRegression extends Application {
static double[][][] TRAINING_DATA =
// SEQUENTIALLY CLUSTERED DATASET
{{{1.0,400},{800}},
{{1.0,450},{820}},
{{1.0,500},{980}},
{{1.0,550},{990}},
{{1.0,600},{920}},
{{1.0,650},{930}},
{{1.0,700},{1250}},
{{1.0,750},{1280}},
{{1.0,800},{1400}},
{{1.0,850},{1350}},
{{1.0,900},{1430}},
{{1.0,950},{1400}},
{{1.0,1000},{1500}},
{{1.0,1050},{1550}},
{{1.0,1100},{1600}},
{{1.0,1150},{1700}},
{{1.0,1200},{1800}},
{{1.0,1250},{1900}},
{{1.0,1300},{2100}}};
/* RANDOMIZED CLUSTERED DATASET
{{{1.0,400},{800}},
{{1.0,450},{820}},
{{1.0,500},{980}},
{{1.0,1150},{1700}},
{{1.0,1200},{1800}},
{{1.0,1250},{1900}},
{{1.0,1300},{2100}},
{{1.0,550},{990}},
{{1.0,850},{1350}},
{{1.0,900},{1430}},
{{1.0,950},{1400}},
{{1.0,1000},{1500}},
{{1.0,1050},{1550}},
{{1.0,600},{920}},
{{1.0,650},{930}},
{{1.0,700},{1250}},
{{1.0,750},{1280}},
{{1.0,800},{1400}},
{{1.0,1100},{1600}}};
*/
/* IRREGULAR SCATTERED DATASET
{{{1.0,400},{800}},
{{1.0,450},{820}},
{{1.0,500},{980}},
{{1.0,1050},{3700}},
{{1.0,1200},{800}},
{{1.0,1250},{1900}},
{{1.0,1300},{100}},
{{1.0,550},{90}},
{{1.0,9850},{350}},
{{1.0,900},{430}},
{{1.0,950},{1030}},
{{1.0,1000},{500}},
{{1.0,1050},{1350}},
{{1.0,600},{920}},
{{1.0,650},{9303}},
{{1.0,700},{1250}},
{{1.0,150},{180}},
{{1.0,820},{1100}},
{{1.0,1300},{600}}};
*/
static logicClass lc;
public static void main(String[] args) throws Exception {
double[][] xArray = new double[TRAINING_DATA.length][TRAINING_DATA[0][0].length];
double[][] yArray = new double[TRAINING_DATA.length][1];
IntStream.range(0, TRAINING_DATA.length).forEach(i ->{
IntStream.range(0, TRAINING_DATA[0][0].length).forEach(j-> xArray[i][j]= TRAINING_DATA[i][0][j]);
yArray[i][0]=TRAINING_DATA[i][1][0];
});
lc = new logicClass(xArray, yArray);
launch();
}
static void handleCommandLine() throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("> To estimate rent, enter apt size (in square feet), or exit:");
try{
String entry = bufferedReader.readLine();
if(!entry.equals("exit"))
System.out.print("Estimated rent: $"+ lc.estimateRent(entry));
else System.exit(0);
}catch (Exception e)
{
System.out.println("Invalid input");
}
}
}
public void start(Stage stage) throws Exception{
Platform.setImplicitExit(false);
XYChart.Series<Number,Number> series1 = new XYChart.Series<Number, Number>();
XYChart.Series<Number,Number> series2 = new XYChart.Series<Number, Number>();
IntStream.range(0, Univariate_linearRegression.TRAINING_DATA.length).forEach( i->
series1.getData().add(
new XYChart.Data<Number,Number>(
Univariate_linearRegression.TRAINING_DATA[i][0][1],Univariate_linearRegression.TRAINING_DATA[i][1][0])));
IntStream.range(0, Univariate_linearRegression.TRAINING_DATA.length).forEach( i->
series2.getData().add(
new XYChart.Data<Number,Number>(
Univariate_linearRegression.TRAINING_DATA[i][0][1], lc.getEstimate().getEntry(i, 0))));
NumberAxis xAxis = new NumberAxis(0,1500,400);
xAxis.setLabel("Size(in square feet)");
NumberAxis yAxis = new NumberAxis(0,2200,700);
yAxis.setLabel("Rent(in USD)");
ScatterChart<Number,Number> scatterChart = new ScatterChart<Number,Number>(xAxis,yAxis);
scatterChart.getData().add(series1);
LineChart<Number,Number> lineChart = new LineChart<Number,Number>(xAxis,yAxis);
lineChart.getData().add(series2);
lineChart.setOpacity(0.4);
Pane pane = new Pane();
pane.getChildren().addAll(scatterChart,lineChart);
stage.setScene(new Scene(pane,580,370));
stage.setOnHidden(e -> { try{
handleCommandLine();
}
catch(Exception e1)
{
e1.printStackTrace();
}});
System.out.println("Close display window to proceed");
stage.setTitle("Univariate linear regression");
stage.show();
}
}