-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support data instance class for user usage, and add test cases for model serialization #6
base: gearpump
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.apache.samoa.learners; | ||
|
||
/* | ||
* #%L | ||
* SAMOA | ||
* %% | ||
* Copyright (C) 2014 - 2016 Apache Software Foundation | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import java.io.Serializable; | ||
|
||
public interface DataInstance extends Serializable { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package org.apache.samoa.learners; | ||
|
||
/* | ||
* #%L | ||
* SAMOA | ||
* %% | ||
* Copyright (C) 2014 - 2016 Apache Software Foundation | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import org.apache.samoa.instances.*; | ||
import org.apache.samoa.learners.classifiers.NominalDataInstance; | ||
import org.apache.samoa.learners.classifiers.NumericDataInstance; | ||
import org.apache.samoa.learners.clusterers.ClusterDataInstance; | ||
import org.apache.samoa.moa.core.DataPoint; | ||
import org.apache.samoa.moa.core.FastVector; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class InstanceUtils { | ||
static private InstancesHeader getNumericInstanceHeader(NumericDataInstance numericDataInstance) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. private static is more common used/ |
||
FastVector<Attribute> attributes = new FastVector<>(); | ||
|
||
for (int i = 0; i < numericDataInstance.getNumNumerics(); i++) { | ||
attributes.addElement(new Attribute("numeric" + (i + 1))); | ||
} | ||
|
||
FastVector<String> classLabels = new FastVector<>(); | ||
for (int i = 0; i < numericDataInstance.getNumClasses(); i++) { | ||
classLabels.addElement("class" + (i + 1)); | ||
} | ||
attributes.addElement(new Attribute("class", classLabels)); | ||
|
||
InstancesHeader instancesHeader = new InstancesHeader( | ||
new Instances(null, attributes, 0)); | ||
instancesHeader.setClassIndex(instancesHeader.numAttributes() - 1); | ||
|
||
return instancesHeader; | ||
} | ||
|
||
static private Instance convertNumericInstance(NumericDataInstance numericDataInstance) { | ||
InstancesHeader header = InstanceUtils.getNumericInstanceHeader(numericDataInstance); | ||
Instance inst = new DenseInstance(header.numAttributes()); | ||
|
||
for (int i = 0; i < numericDataInstance.getNumNumerics(); i++) { | ||
inst.setValue(i, numericDataInstance.getData()[i]); | ||
} | ||
|
||
inst.setDataset(header); | ||
inst.setClassValue(numericDataInstance.getTrueClass()); | ||
|
||
return inst; | ||
} | ||
|
||
static private InstancesHeader getNominalInstanceHeader(NominalDataInstance nominalDataInstance) { | ||
FastVector<Attribute> attributes = new FastVector<>(); | ||
|
||
for (int i = 0; i < nominalDataInstance.getNumNominals(); i++) { | ||
FastVector<String> nominalAttVals = new FastVector<>(); | ||
for (int j = 0; j < nominalDataInstance.getNumValsPerNominal()[i]; j++) { | ||
nominalAttVals.addElement("value" + (j + 1)); | ||
} | ||
attributes.addElement(new Attribute("nominal" + (i + 1), | ||
nominalAttVals)); | ||
} | ||
|
||
FastVector<String> classLabels = new FastVector<>(); | ||
for (int i = 0; i < nominalDataInstance.getNumClasses(); i++) { | ||
classLabels.addElement("class" + (i + 1)); | ||
} | ||
attributes.addElement(new Attribute("class", classLabels)); | ||
|
||
InstancesHeader instancesHeader = new InstancesHeader( | ||
new Instances(null, attributes, 0)); | ||
instancesHeader.setClassIndex(instancesHeader.numAttributes() - 1); | ||
|
||
return instancesHeader; | ||
} | ||
|
||
static private Instance convertNominalInstance(NominalDataInstance nominalDataInstance) { | ||
InstancesHeader header = InstanceUtils.getNominalInstanceHeader(nominalDataInstance); | ||
Instance inst = new DenseInstance(header.numAttributes()); | ||
|
||
for (int i = 0; i < nominalDataInstance.getNumNominals(); i++) { | ||
inst.setValue(i, nominalDataInstance.getData()[i]); | ||
} | ||
|
||
inst.setDataset(header); | ||
inst.setClassValue(nominalDataInstance.getTrueClass()); | ||
|
||
return inst; | ||
} | ||
|
||
static private InstancesHeader getClusterInstanceHeader(ClusterDataInstance clusterDataInstance) { | ||
ArrayList<Attribute> attributes = new ArrayList<>(); | ||
|
||
for (int i = 0; i < clusterDataInstance.getNumAtts(); i++) { | ||
attributes.add(new Attribute("att" + (i + 1))); | ||
} | ||
|
||
// attributes.add(new Attribute("class", null)); | ||
|
||
InstancesHeader instancesHeader = new InstancesHeader( | ||
new Instances(null, attributes, 0)); | ||
instancesHeader.setClassIndex(instancesHeader.numAttributes() - 1); | ||
|
||
return instancesHeader; | ||
} | ||
|
||
static private Instance convertClusterInstance(ClusterDataInstance clusterDataInstance) { | ||
Instance inst = new DenseInstance(1.0, clusterDataInstance.getData()); | ||
inst.setDataset(InstanceUtils.getClusterInstanceHeader(clusterDataInstance)); | ||
return new DataPoint(inst, clusterDataInstance.getTimeStamp()); | ||
} | ||
|
||
static public Instance convertToSamoaInstance(DataInstance dataInstance) { | ||
if (dataInstance instanceof NumericDataInstance) { | ||
return InstanceUtils.convertNumericInstance((NumericDataInstance) dataInstance); | ||
} else if (dataInstance instanceof NominalDataInstance) { | ||
return InstanceUtils.convertNominalInstance((NominalDataInstance) dataInstance); | ||
} else if (dataInstance instanceof ClusterDataInstance) { | ||
return InstanceUtils.convertClusterInstance((ClusterDataInstance) dataInstance); | ||
} else { | ||
throw new Error("Invalid input class!"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,8 @@ | |
*/ | ||
|
||
|
||
import org.apache.samoa.instances.Instance; | ||
|
||
import java.io.Serializable; | ||
|
||
public interface Model extends Serializable { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is important to add enough doc for public interface. |
||
double[] predict(Instance inst); | ||
double[] predict(DataInstance dataInstance); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.apache.samoa.learners.classifiers; | ||
|
||
/* | ||
* #%L | ||
* SAMOA | ||
* %% | ||
* Copyright (C) 2014 - 2016 Apache Software Foundation | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import org.apache.samoa.learners.DataInstance; | ||
|
||
public class NominalDataInstance implements DataInstance { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add document about what it is. |
||
|
||
private int numNominals; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to document what is numNominals? |
||
private int numClasses; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to document what is numNominals? |
||
private double trueClass; // index started from 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to document what is trueclass? |
||
private int[] numValsPerNominal; | ||
private double[] data; // index started from 0 | ||
|
||
public NominalDataInstance(int numNominals, int numClasses, double trueClass, | ||
int[] numValsPerNominal, double[] data) { | ||
this.numNominals = numNominals; | ||
this.numClasses = numClasses; | ||
this.trueClass = trueClass; | ||
this.numValsPerNominal = numValsPerNominal; | ||
this.data = data; | ||
} | ||
|
||
public int getNumNominals() { | ||
return numNominals; | ||
} | ||
|
||
public void setNumNominals(int numNominals) { | ||
this.numNominals = numNominals; | ||
} | ||
|
||
public int getNumClasses() { | ||
return numClasses; | ||
} | ||
|
||
public void setNumClasses(int numClasses) { | ||
this.numClasses = numClasses; | ||
} | ||
|
||
public double getTrueClass() { | ||
return trueClass; | ||
} | ||
|
||
public void setTrueClass(double trueClass) { | ||
this.trueClass = trueClass; | ||
} | ||
|
||
public int[] getNumValsPerNominal() { | ||
return numValsPerNominal; | ||
} | ||
|
||
public void setNumValsPerNominal(int[] numValsPerNominal) { | ||
this.numValsPerNominal = numValsPerNominal; | ||
} | ||
|
||
public double[] getData() { | ||
return data; | ||
} | ||
|
||
public void setData(double[] data) { | ||
this.data = data; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.apache.samoa.learners.classifiers; | ||
|
||
/* | ||
* #%L | ||
* SAMOA | ||
* %% | ||
* Copyright (C) 2014 - 2016 Apache Software Foundation | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import org.apache.samoa.learners.DataInstance; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add document about what it is. |
||
public class NumericDataInstance implements DataInstance { | ||
private int numNumerics; | ||
private int numClasses; | ||
private double trueClass; // index started from 0 | ||
private double[] data; | ||
|
||
public NumericDataInstance(int numNumerics, int numClasses, double trueClass, double[] data) { | ||
this.numNumerics = numNumerics; | ||
this.numClasses = numClasses; | ||
this.trueClass = trueClass; | ||
this.data = data; | ||
} | ||
|
||
public int getNumNumerics() { | ||
return numNumerics; | ||
} | ||
|
||
public void setNumNumerics(int numNumerics) { | ||
this.numNumerics = numNumerics; | ||
} | ||
|
||
public int getNumClasses() { | ||
return numClasses; | ||
} | ||
|
||
public void setNumClasses(int numClasses) { | ||
this.numClasses = numClasses; | ||
} | ||
|
||
public double getTrueClass() { | ||
return trueClass; | ||
} | ||
|
||
public void setTrueClass(double trueClass) { | ||
this.trueClass = trueClass; | ||
} | ||
|
||
public double[] getData() { | ||
return data; | ||
} | ||
|
||
public void setData(double[] data) { | ||
this.data = data; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe some comments about why this interface is empty?