Skip to content

Commit

Permalink
Restructure model data and algorithm handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nok committed Oct 14, 2017
1 parent 635da46 commit ff82bb8
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 99 deletions.
75 changes: 45 additions & 30 deletions examples/estimator/classifier/BernoulliNB/java/basics.ipynb
Expand Up @@ -22,14 +22,14 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(150, 4) (150,)\n"
"((150, 4), (150,))\n"
]
}
],
Expand All @@ -39,7 +39,7 @@
"iris_data = load_iris()\n",
"X, y = iris_data.data, iris_data.target\n",
"\n",
"print X.shape, y.shape"
"print(X.shape, y.shape)"
]
},
{
Expand All @@ -51,7 +51,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -60,7 +60,7 @@
"BernoulliNB(alpha=1.0, binarize=0.0, class_prior=None, fit_prior=True)"
]
},
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -81,7 +81,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {
"scrolled": false
},
Expand All @@ -92,35 +92,38 @@
"text": [
"class Brain {\n",
"\n",
" public static int predict(double[] atts) {\n",
" if (atts.length != 4) {\n",
" return -1;\n",
" }\n",
" int i, j;\n",
" \n",
" double[] priors = {-1.0986122886681096, -1.0986122886681096, -1.0986122886681096};\n",
" double[][] negProbs = {{-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}, {-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}, {-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}};\n",
" double[][] delProbs = {{3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}};\n",
" private double[] priors;\n",
" private double[][] negProbs;\n",
" private double[][] delProbs;\n",
"\n",
" public Brain(double[] priors, double[][] negProbs, double[][] delProbs) {\n",
" this.priors = priors;\n",
" this.negProbs = negProbs;\n",
" this.delProbs = delProbs;\n",
" }\n",
"\n",
" public int predict(double[] features) {\n",
" if (features.length != 4) return -1;\n",
" \n",
" double[] jll = new double[3];\n",
" for (i = 0; i < 3; i++) {\n",
" for (int i = 0; i < 3; i++) {\n",
" double sum = 0.;\n",
" for (j = 0; j < 4; j++) {\n",
" sum += atts[i] * delProbs[j][i];\n",
" for (int j = 0; j < 4; j++) {\n",
" sum += features[i] * this.delProbs[j][i];\n",
" }\n",
" jll[i] = sum;\n",
" }\n",
" for (i = 0; i < 3; i++) {\n",
" for (int i = 0; i < 3; i++) {\n",
" double sum = 0.;\n",
" for (j = 0; j < 4; j++) {\n",
" sum += negProbs[i][j];\n",
" for (int j = 0; j < 4; j++) {\n",
" sum += this.negProbs[i][j];\n",
" }\n",
" jll[i] += priors[i] + sum;\n",
" jll[i] += this.priors[i] + sum;\n",
" }\n",
" \n",
" double highestLikeli = Double.NEGATIVE_INFINITY;\n",
" int classIndex = -1;\n",
" for (i = 0; i < 3; i++) {\n",
" for (int i = 0; i < 3; i++) {\n",
" if (jll[i] > highestLikeli) {\n",
" highestLikeli = jll[i];\n",
" classIndex = i;\n",
Expand All @@ -131,11 +134,23 @@
"\n",
" public static void main(String[] args) {\n",
" if (args.length == 4) {\n",
" double[] atts = new double[args.length];\n",
"\n",
" // Features:\n",
" double[] features = new double[args.length];\n",
" for (int i = 0, l = args.length; i < l; i++) {\n",
" atts[i] = Double.parseDouble(args[i]);\n",
" features[i] = Double.parseDouble(args[i]);\n",
" }\n",
" System.out.println(Brain.predict(atts));\n",
"\n",
" // Parameters:\n",
" final double[] priors = {-1.0986122886681096, -1.0986122886681096, -1.0986122886681096};\n",
" final double[][] negProbs = {{-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}, {-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}, {-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}};\n",
" final double[][] delProbs = {{3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}};\n",
"\n",
" // Prediction:\n",
" Brain brain = new Brain(priors, negProbs, delProbs);\n",
" int estimation = brain.predict(features);\n",
" System.out.println(estimation);\n",
"\n",
" }\n",
" }\n",
"}\n"
Expand All @@ -145,9 +160,9 @@
"source": [
"from sklearn_porter import Porter\n",
"\n",
"output = Porter(clf).export()\n",
"\n",
"print output"
"porter = Porter(clf)\n",
"output = porter.export()\n",
"print(output)"
]
}
],
Expand All @@ -167,7 +182,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"version": "2.7.13"
}
},
"nbformat": 4,
Expand Down
59 changes: 37 additions & 22 deletions examples/estimator/classifier/BernoulliNB/java/basics.py
Expand Up @@ -11,42 +11,45 @@
clf = BernoulliNB()
clf.fit(X, y)

output = Porter(clf).export()
# output = Porter(clf, language='java').export()
porter = Porter(clf)
output = porter.export()
print(output)

"""
class Brain {
public static int predict(double[] atts) {
if (atts.length != 4) {
return -1;
}
int i, j;
private double[] priors;
private double[][] negProbs;
private double[][] delProbs;
double[] priors = {-1.0986122886681096, -1.0986122886681096, -1.0986122886681096};
double[][] negProbs = {{-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}, {-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}, {-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}};
double[][] delProbs = {{3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}};
public Brain(double[] priors, double[][] negProbs, double[][] delProbs) {
this.priors = priors;
this.negProbs = negProbs;
this.delProbs = delProbs;
}
public int predict(double[] features) {
if (features.length != 4) return -1;
double[] jll = new double[3];
for (i = 0; i < 3; i++) {
for (int i = 0; i < 3; i++) {
double sum = 0.;
for (j = 0; j < 4; j++) {
sum += atts[i] * delProbs[j][i];
for (int j = 0; j < 4; j++) {
sum += features[i] * this.delProbs[j][i];
}
jll[i] = sum;
}
for (i = 0; i < 3; i++) {
for (int i = 0; i < 3; i++) {
double sum = 0.;
for (j = 0; j < 4; j++) {
sum += negProbs[i][j];
for (int j = 0; j < 4; j++) {
sum += this.negProbs[i][j];
}
jll[i] += priors[i] + sum;
jll[i] += this.priors[i] + sum;
}
double highestLikeli = Double.NEGATIVE_INFINITY;
int classIndex = -1;
for (i = 0; i < 3; i++) {
for (int i = 0; i < 3; i++) {
if (jll[i] > highestLikeli) {
highestLikeli = jll[i];
classIndex = i;
Expand All @@ -57,11 +60,23 @@ class Brain {
public static void main(String[] args) {
if (args.length == 4) {
double[] atts = new double[args.length];
// Features:
double[] features = new double[args.length];
for (int i = 0, l = args.length; i < l; i++) {
atts[i] = Double.parseDouble(args[i]);
features[i] = Double.parseDouble(args[i]);
}
System.out.println(Brain.predict(atts));
// Parameters:
final double[] priors = {-1.0986122886681096, -1.0986122886681096, -1.0986122886681096};
final double[][] negProbs = {{-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}, {-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}, {-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138}};
final double[][] delProbs = {{3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}, {3.931825632724312, 3.931825632724312, 3.931825632724312}};
// Prediction:
Brain brain = new Brain(priors, negProbs, delProbs);
int estimation = brain.predict(features);
System.out.println(estimation);
}
}
}
Expand Down

0 comments on commit ff82bb8

Please sign in to comment.