Skip to content
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.

Commit

Permalink
0737e23 replay starts
Browse files Browse the repository at this point in the history
  • Loading branch information
DrRacket committed Dec 6, 2017
1 parent 95c0d39 commit 6c1c33f
Show file tree
Hide file tree
Showing 14 changed files with 1,468 additions and 40 deletions.
66 changes: 66 additions & 0 deletions core/src/main/java/hivemall/smile/classification/DecisionTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@
import hivemall.smile.utils.SmileExtUtils;
import hivemall.utils.collections.lists.IntArrayList;
import hivemall.utils.lang.ObjectUtils;
import hivemall.utils.lang.StringUtils;
import hivemall.utils.lang.mutable.MutableInt;
import hivemall.utils.sampling.IntReservoirSampler;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -426,6 +429,58 @@ public void exportGraphviz(@Nonnull final StringBuilder builder,
}
}

@Deprecated
public int opCodegen(@Nonnull final List<String> scripts, int depth) {
int selfDepth = 0;
final StringBuilder buf = new StringBuilder();
if (trueChild == null && falseChild == null) {
buf.append("push ").append(output);
scripts.add(buf.toString());
buf.setLength(0);
buf.append("goto last");
scripts.add(buf.toString());
selfDepth += 2;
} else {
if (splitFeatureType == AttributeType.NOMINAL) {
buf.append("push ").append("x[").append(splitFeature).append("]");
scripts.add(buf.toString());
buf.setLength(0);
buf.append("push ").append(splitValue);
scripts.add(buf.toString());
buf.setLength(0);
buf.append("ifeq ");
scripts.add(buf.toString());
depth += 3;
selfDepth += 3;
int trueDepth = trueChild.opCodegen(scripts, depth);
selfDepth += trueDepth;
scripts.set(depth - 1, "ifeq " + String.valueOf(depth + trueDepth));
int falseDepth = falseChild.opCodegen(scripts, depth + trueDepth);
selfDepth += falseDepth;
} else if (splitFeatureType == AttributeType.NUMERIC) {
buf.append("push ").append("x[").append(splitFeature).append("]");
scripts.add(buf.toString());
buf.setLength(0);
buf.append("push ").append(splitValue);
scripts.add(buf.toString());
buf.setLength(0);
buf.append("ifle ");
scripts.add(buf.toString());
depth += 3;
selfDepth += 3;
int trueDepth = trueChild.opCodegen(scripts, depth);
selfDepth += trueDepth;
scripts.set(depth - 1, "ifle " + String.valueOf(depth + trueDepth));
int falseDepth = falseChild.opCodegen(scripts, depth + trueDepth);
selfDepth += falseDepth;
} else {
throw new IllegalStateException("Unsupported attribute type: "
+ splitFeatureType);
}
}
return selfDepth;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(splitFeature);
Expand Down Expand Up @@ -1069,13 +1124,24 @@ public int predict(Vector x, double[] posteriori) {
throw new UnsupportedOperationException("Not supported.");
}

@Nonnull
public String predictJsCodegen(@Nonnull final String[] featureNames,
@Nonnull final String[] classNames) {
StringBuilder buf = new StringBuilder(1024);
_root.exportJavascript(buf, featureNames, classNames, 0);
return buf.toString();
}

@Deprecated
@Nonnull
public String predictOpCodegen(@Nonnull final String sep) {
List<String> opslist = new ArrayList<String>();
_root.opCodegen(opslist, 0);
opslist.add("call end");
String scripts = StringUtils.concat(opslist, sep);
return scripts;
}

@Nonnull
public byte[] serialize(boolean compress) throws HiveException {
try {
Expand Down
65 changes: 65 additions & 0 deletions core/src/main/java/hivemall/smile/regression/RegressionTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@
import hivemall.utils.collections.sets.IntArraySet;
import hivemall.utils.collections.sets.IntSet;
import hivemall.utils.lang.ObjectUtils;
import hivemall.utils.lang.StringUtils;
import hivemall.utils.lang.mutable.MutableInt;
import hivemall.utils.math.MathUtils;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -373,6 +376,57 @@ public void exportGraphviz(@Nonnull final StringBuilder builder,
}
}

@Deprecated
public int opCodegen(@Nonnull final List<String> scripts, int depth) {
int selfDepth = 0;
final StringBuilder buf = new StringBuilder();
if (trueChild == null && falseChild == null) {
buf.append("push ").append(output);
scripts.add(buf.toString());
buf.setLength(0);
buf.append("goto last");
scripts.add(buf.toString());
selfDepth += 2;
} else {
if (splitFeatureType == AttributeType.NOMINAL) {
buf.append("push ").append("x[").append(splitFeature).append("]");
scripts.add(buf.toString());
buf.setLength(0);
buf.append("push ").append(splitValue);
scripts.add(buf.toString());
buf.setLength(0);
buf.append("ifeq ");
scripts.add(buf.toString());
depth += 3;
selfDepth += 3;
int trueDepth = trueChild.opCodegen(scripts, depth);
selfDepth += trueDepth;
scripts.set(depth - 1, "ifeq " + String.valueOf(depth + trueDepth));
int falseDepth = falseChild.opCodegen(scripts, depth + trueDepth);
selfDepth += falseDepth;
} else if (splitFeatureType == AttributeType.NUMERIC) {
buf.append("push ").append("x[").append(splitFeature).append("]");
scripts.add(buf.toString());
buf.setLength(0);
buf.append("push ").append(splitValue);
scripts.add(buf.toString());
buf.setLength(0);
buf.append("ifle ");
scripts.add(buf.toString());
depth += 3;
selfDepth += 3;
int trueDepth = trueChild.opCodegen(scripts, depth);
selfDepth += trueDepth;
scripts.set(depth - 1, "ifle " + String.valueOf(depth + trueDepth));
int falseDepth = falseChild.opCodegen(scripts, depth + trueDepth);
selfDepth += falseDepth;
} else {
throw new IllegalStateException("Unsupported attribute type: "
+ splitFeatureType);
}
}
return selfDepth;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
Expand Down Expand Up @@ -932,12 +986,23 @@ public double predict(@Nonnull final Vector x) {
return _root.predict(x);
}

@Nonnull
public String predictJsCodegen(@Nonnull final String[] featureNames) {
StringBuilder buf = new StringBuilder(1024);
_root.exportJavascript(buf, featureNames, 0);
return buf.toString();
}

@Deprecated
@Nonnull
public String predictOpCodegen(@Nonnull String sep) {
List<String> opslist = new ArrayList<String>();
_root.opCodegen(opslist, 0);
opslist.add("call end");
String scripts = StringUtils.concat(opslist, sep);
return scripts;
}

@Nonnull
public byte[] serialize(boolean compress) throws HiveException {
try {
Expand Down
Loading

0 comments on commit 6c1c33f

Please sign in to comment.