Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 64f1a90

Browse files
committed
Added sample code for toy challenge
1 parent e66c5d5 commit 64f1a90

File tree

15 files changed

+168
-14
lines changed

15 files changed

+168
-14
lines changed

‎code/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.classpath
2+
.project
3+
/bin/
4+
sources.txt

‎code/Dockerfile

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
FROM python
1+
FROM openjdk:8-jdk-alpine3.9
22

3-
COPY . .
3+
# Create a /work directory within the container, copy everything from the
4+
# build directory and switch there.
5+
RUN mkdir /work
6+
COPY . /work
7+
WORKDIR /work
48

5-
RUN pip3 install -r requirements.txt
9+
# Make sure your build script is executable. Generally, always check that
10+
# all your scripts are made executable, forgetting this is a common error.
11+
# Another frequent error is having Windows style line endings in .sh files.
12+
RUN chmod +x build.sh
13+
RUN ./build.sh
614

7-
ENTRYPOINT [ "python3", "helloworld.py" ]
15+
# Again, test and train scripts should be executable within the container.
16+
RUN chmod +x test.sh
17+
RUN chmod +x train.sh
18+
19+
# Don't add any CMD or ENTRYPOINT!

‎code/build.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rm -rf ./bin
2+
mkdir ./bin
3+
find -name "*.java" > sources.txt
4+
javac -d ./bin @sources.txt

‎code/data/testing.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name,height
2+
terry,2.50
3+
tom,1.80
4+
jerry,1.05

‎code/data/training.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name,height,weight
2+
joe,1.80,75
3+
jack,2.10,99
4+
jane,1.65,56
5+
bill,1.70,110
6+
grace,1.75,60

‎code/helloworld.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎code/model/dummy-model.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
58.39999999999997
2+
-25.119999999999948

‎code/requirements.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

‎code/src/sample/submission/Tester.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package sample.submission;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.LineNumberReader;
6+
import java.io.PrintWriter;
7+
8+
/*
9+
* A simple solution, works by doing linear regression with parameters
10+
* calculated previously by the Trainer.
11+
*/
12+
public class Tester {
13+
public static void main(String[] args) throws Exception {
14+
// arg0: test file path
15+
// arg1: output csv path
16+
String inPath = null;
17+
String outPath = null;
18+
String modelPath = null;
19+
if (args.length != 3) {
20+
System.out.println("Usage: java sample.submission.Tester <test-path> <output-path> <model-path>");
21+
System.exit(1);
22+
}
23+
else {
24+
inPath = args[0];
25+
outPath = args[1];
26+
modelPath = args[2];
27+
}
28+
29+
LineNumberReader lnr = new LineNumberReader(new FileReader(modelPath));
30+
double a = Double.parseDouble(lnr.readLine());
31+
double b = Double.parseDouble(lnr.readLine());
32+
lnr.close();
33+
34+
PrintWriter out = new PrintWriter(new File(outPath));
35+
lnr = new LineNumberReader(new FileReader(inPath));
36+
while (true) {
37+
String line = lnr.readLine();
38+
if (line == null) break;
39+
// name,height
40+
// joe,1.80
41+
line = line.trim();
42+
if (line.isEmpty() || line.contains("name")) continue;
43+
String[] parts = line.split(",");
44+
String name = parts[0];
45+
double h = Double.parseDouble(parts[1]);
46+
double w = a * h + b;
47+
out.println(name + "," + w);
48+
}
49+
lnr.close();
50+
out.close();
51+
}
52+
}

‎code/src/sample/submission/Trainer.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package sample.submission;
2+
3+
import java.io.File;
4+
import java.io.FileReader;
5+
import java.io.LineNumberReader;
6+
import java.io.PrintWriter;
7+
import java.util.List;
8+
import java.util.Vector;
9+
10+
/*
11+
* A simple trainer, calculates linear regression parameters from the
12+
* training data and saves it as 'model'.
13+
*/
14+
public class Trainer {
15+
public static void main(String[] args) throws Exception {
16+
// arg0: truth file
17+
// arg1: output 'model' file
18+
String trainPath = null;
19+
String outPath = null;
20+
if (args.length != 2) {
21+
System.out.println("Usage: java sample.submission.Trainer <truth-path> <model-path>");
22+
System.exit(1);
23+
}
24+
else {
25+
trainPath = args[0];
26+
outPath = args[1];
27+
}
28+
29+
List<Double> xs = new Vector<>();
30+
List<Double> ys = new Vector<>();
31+
32+
File truthCsv = new File(trainPath);
33+
LineNumberReader lnr = new LineNumberReader(new FileReader(truthCsv));
34+
while (true) {
35+
String line = lnr.readLine();
36+
if (line == null) break;
37+
// name,height,weight
38+
// joe,1.80,75
39+
line = line.trim();
40+
if (line.isEmpty() || line.contains("name")) continue;
41+
String[] parts = line.split(",");
42+
xs.add(Double.parseDouble(parts[1]));
43+
ys.add(Double.parseDouble(parts[2]));
44+
}
45+
lnr.close();
46+
47+
int n = xs.size();
48+
double sumX = 0.0, sumY = 0.0;
49+
for (int i = 0; i < n; i++) {
50+
double x = xs.get(i);
51+
double y = ys.get(i);
52+
sumX += x;
53+
sumY += y;
54+
}
55+
56+
double avgX = sumX / n;
57+
double avgY = sumY / n;
58+
59+
double xx = 0.0, xy = 0.0;
60+
for (int i = 0; i < n; i++) {
61+
double x = xs.get(i);
62+
double y = ys.get(i);
63+
xx += (x - avgX) * (x - avgX);
64+
xy += (x - avgX) * (y - avgY);
65+
}
66+
double a = xy / xx;
67+
double b = avgY - a * avgX;
68+
69+
PrintWriter out = new PrintWriter(new File(outPath));
70+
out.println("" + a);
71+
out.println("" + b);
72+
out.close();
73+
System.out.println("Training finished. Model written to " + outPath);
74+
}
75+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /