forked from apache/incubator-samoa
-
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
Open
gy910210
wants to merge
5
commits into
gearpump:gearpump
Choose a base branch
from
gy910210:serialize
base: gearpump
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
samoa-api/src/main/java/org/apache/samoa/learners/InstanceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
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.ClassificationDataInstance; | ||
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; | ||
import java.util.Arrays; | ||
|
||
public class InstanceUtils { | ||
|
||
private static InstancesHeader getClusterInstanceHeader(ClusterDataInstance dataInstance) { | ||
ArrayList<Attribute> attributes = new ArrayList<>(); | ||
|
||
for (int i = 0; i < dataInstance.getNumberFeatures(); 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; | ||
} | ||
|
||
private static InstancesHeader getClassificationInstanceHeader(ClassificationDataInstance dataInstance) { | ||
FastVector<Attribute> attributes = new FastVector<>(); | ||
|
||
for (int i = 0; i < dataInstance.getNumberNominalFeatures(); i++) { | ||
FastVector<String> nominalAttVals = new FastVector<>(); | ||
for (int j = 0; j < dataInstance.getNumberValsPerNominalFeature()[i]; j++) { | ||
nominalAttVals.addElement("value" + (j + 1)); | ||
} | ||
attributes.addElement(new Attribute("nominal" + (i + 1), | ||
nominalAttVals)); | ||
} | ||
|
||
for (int i = 0; i < dataInstance.getNumberNumericFeatures(); i++) { | ||
attributes.addElement(new Attribute("numeric" + (i + 1))); | ||
} | ||
|
||
FastVector<String> classLabels = new FastVector<>(); | ||
for (int i = 0; i < dataInstance.getNumberLabels(); 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; | ||
} | ||
|
||
/** | ||
* convert ClassificationDataInstance to SAMOA Instance | ||
*/ | ||
public static Instance convertClassificationDataInstance(ClassificationDataInstance dataInstance) { | ||
InstancesHeader header = InstanceUtils.getClassificationInstanceHeader(dataInstance); | ||
Instance inst = new DenseInstance(header.numAttributes()); | ||
|
||
int numNomFeatures = dataInstance.getNumberNominalFeatures(); | ||
int numNumFeatures = dataInstance.getNumberNumericFeatures(); | ||
|
||
for (int i = 0; i < numNomFeatures + numNumFeatures; i++) { | ||
if (i < numNomFeatures) { | ||
inst.setValue(i, dataInstance.getNominalData()[i]); | ||
} else { | ||
inst.setValue(i, dataInstance.getNumericData()[i - numNomFeatures]); | ||
} | ||
} | ||
|
||
inst.setDataset(header); | ||
inst.setClassValue(dataInstance.getTrueLabel()); | ||
|
||
return inst; | ||
} | ||
|
||
/** | ||
* convert SAMOA Instance to ClassificationDataInstance | ||
*/ | ||
public static ClassificationDataInstance reConvertClassificationDataInstance( | ||
Instance inst, int numberNominalFeatures, int numberNumericFeatures) { | ||
double[] nominalDataTmp = Arrays.copyOfRange(inst.toDoubleArray(), 0, numberNominalFeatures); | ||
int[] nominalData = new int[nominalDataTmp.length]; | ||
for (int j = 0; j < nominalData.length; j++) { | ||
nominalData[j] = (int) nominalDataTmp[j]; | ||
} | ||
|
||
double[] numericData = Arrays.copyOfRange( | ||
inst.toDoubleArray(), numberNominalFeatures, | ||
inst.toDoubleArray().length - 1); | ||
|
||
int[] numValsPerNominal = new int[nominalData.length]; | ||
Arrays.fill(numValsPerNominal, inst.attribute(0).numValues()); | ||
|
||
return new ClassificationDataInstance( | ||
numberNumericFeatures, numericData, numberNominalFeatures, | ||
numValsPerNominal, nominalData, inst.numClasses(), (int) inst.classValue()); | ||
} | ||
|
||
/** | ||
* convert ClusterDataInstance to SAMOA Instance | ||
*/ | ||
public static Instance convertClusterDataInstance(ClusterDataInstance dataInstance) { | ||
Instance inst = new DenseInstance(1.0, dataInstance.getData()); | ||
inst.setDataset(InstanceUtils.getClusterInstanceHeader(dataInstance)); | ||
return new DataPoint(inst, dataInstance.getTimeStamp()); | ||
} | ||
|
||
/** | ||
* convert SAMOA Instance to ClusterDataInstance | ||
*/ | ||
public static ClusterDataInstance reConvertClusterDataInstance(DataPoint point) { | ||
double[] data = point.toDoubleArray(); | ||
return new ClusterDataInstance(data.length, point.getTimestamp(), data); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...a-api/src/main/java/org/apache/samoa/learners/classifiers/ClassificationDataInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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 java.io.Serializable; | ||
|
||
/** | ||
* DataInstance for classification problem | ||
* There may be tow types of feature in feature vector: numeric feature and nominal feature | ||
*/ | ||
public class ClassificationDataInstance implements Serializable { | ||
private int numberNumericFeatures; | ||
private double[] numericData; | ||
|
||
private int numberNominalFeatures; | ||
private int[] numberValsPerNominalFeature; | ||
private int[] nominalData; | ||
|
||
private int numberLabels; | ||
private int trueLabel; | ||
|
||
public ClassificationDataInstance(int numberNumericFeatures, | ||
double[] numericData, int numberNominalFeatures, | ||
int[] numberValsPerNominalFeature, int[] nominalData, | ||
int numberLabels, int trueLabel) { | ||
this.numberNumericFeatures = numberNumericFeatures; | ||
this.numericData = numericData; | ||
this.numberNominalFeatures = numberNominalFeatures; | ||
this.numberValsPerNominalFeature = numberValsPerNominalFeature; | ||
this.nominalData = nominalData; | ||
this.numberLabels = numberLabels; | ||
this.trueLabel = trueLabel; | ||
} | ||
|
||
public int getNumberNumericFeatures() { | ||
return numberNumericFeatures; | ||
} | ||
|
||
public double[] getNumericData() { | ||
return numericData; | ||
} | ||
|
||
public int getNumberNominalFeatures() { | ||
return numberNominalFeatures; | ||
} | ||
|
||
public int[] getNumberValsPerNominalFeature() { | ||
return numberValsPerNominalFeature; | ||
} | ||
|
||
public int[] getNominalData() { | ||
return nominalData; | ||
} | ||
|
||
public int getNumberLabels() { | ||
return numberLabels; | ||
} | ||
|
||
public int getTrueLabel() { | ||
return trueLabel; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
samoa-api/src/main/java/org/apache/samoa/learners/classifiers/ClassificationModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
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 java.io.Serializable; | ||
|
||
/** | ||
* Model for classification problem | ||
*/ | ||
public interface ClassificationModel extends Serializable { | ||
double[] predict(ClassificationDataInstance dataInstance); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It doesn't make sense to define an interface like this.