Skip to content

Commit 0828977

Browse files
authored
Create WekaBatteryPredictionExample.java
1 parent be73d5a commit 0828977

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package ml;
2+
3+
/*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
**/
17+
18+
import com.google.common.io.Resources;
19+
import weka.classifiers.Classifier;
20+
import weka.classifiers.functions.LinearRegression;
21+
import weka.core.Attribute;
22+
import weka.core.DenseInstance;
23+
import weka.core.Instances;
24+
25+
import java.io.*;
26+
import java.util.ArrayList;
27+
28+
public class WekaBatteryPredictionExample {
29+
30+
private static Classifier cls;
31+
32+
static {
33+
try {
34+
String trainingSetFile = Resources.getResource("training-set.txt").getFile();
35+
Instances trainingSet = loadDatasetFromTxt(trainingSetFile);
36+
37+
cls = new LinearRegression();
38+
cls.buildClassifier(trainingSet);
39+
} catch (Exception e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
44+
public static double predictBatteryLife(double timeCharged) throws Exception {
45+
return cls.classifyInstance(new DenseInstance(1.0, new double[]{timeCharged}));
46+
}
47+
48+
private static Instances loadDatasetFromTxt(String txtFile) throws IOException {
49+
ArrayList<Attribute> atts = new ArrayList<>(2);
50+
atts.add(new Attribute("time_charged", Attribute.NUMERIC));
51+
atts.add(new Attribute("battery_lasted_time", Attribute.NUMERIC));
52+
Instances data = new Instances("battery-prediction-training-set", atts, 0);
53+
data.setClassIndex(1);
54+
55+
File file = new File(txtFile);
56+
FileReader fr = new FileReader(file);
57+
BufferedReader br = new BufferedReader(fr);
58+
String line;
59+
while((line = br.readLine()) != null){
60+
String[] values = line.split(",");
61+
double[] newInst = new double[2];
62+
newInst[0] = Double.valueOf(values[0]);
63+
newInst[1] = Double.valueOf(values[1]);
64+
65+
data.add(new DenseInstance(1.0, newInst));
66+
}
67+
br.close();
68+
fr.close();
69+
70+
return data;
71+
}
72+
73+
public static void main(String... args) throws Exception {
74+
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
75+
String input;
76+
while ((input = in.readLine()) != null && input.length() != 0) {
77+
Double timeCharged = Double.valueOf(input);
78+
if (timeCharged != null) {
79+
System.out.println(predictBatteryLife(timeCharged));
80+
}
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)