Skip to content
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
wants to merge 5 commits into
base: gearpump
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import org.apache.samoa.core.ContentEvent;
import org.apache.samoa.core.Processor;
import org.apache.samoa.learners.ModelContentEvent;
import org.apache.samoa.learners.ClassificationModelContentEvent;
import org.apache.samoa.learners.ResultContentEvent;
import org.apache.samoa.moa.core.Measurement;
import org.apache.samoa.moa.evaluation.LearningCurve;
Expand Down Expand Up @@ -74,7 +74,7 @@ private EvaluatorProcessor(Builder builder) {
@Override
public boolean process(ContentEvent event) {
// for serialize
if (event instanceof ModelContentEvent) {
if (event instanceof ClassificationModelContentEvent) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@
*/

import org.apache.samoa.core.ContentEvent;
import org.apache.samoa.learners.classifiers.ClassificationModel;

final public class ModelContentEvent implements ContentEvent {
final public class ClassificationModelContentEvent implements ContentEvent {
final private boolean isLast;
private Model model;
private ClassificationModel model;
private long modelIndex;
private long instanceIndex;
private int classifierIndex;
private int evaluationIndex;

public ModelContentEvent() {
public ClassificationModelContentEvent() {
this.isLast = false;
}

public ModelContentEvent(boolean isLast) {
public ClassificationModelContentEvent(boolean isLast) {
this.isLast = isLast;
}

public ModelContentEvent(boolean isLast, Model model, long modelIndex, long instanceIndex,
int classifierIndex, int evaluationIndex) {
public ClassificationModelContentEvent(boolean isLast, ClassificationModel model, long modelIndex, long instanceIndex,
int classifierIndex, int evaluationIndex) {
this.isLast = isLast;
this.model = model;
this.modelIndex = modelIndex;
Expand Down Expand Up @@ -71,11 +72,11 @@ public void setModelIndex(long modelIndex) {
this.modelIndex = modelIndex;
}

public Model getModel() {
public ClassificationModel getModel() {
return model;
}

public void setModel(Model model) {
public void setModel(ClassificationModel model) {
this.model = model;
}

Expand Down
142 changes: 142 additions & 0 deletions samoa-api/src/main/java/org/apache/samoa/learners/InstanceUtils.java
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);
}
}
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 {
Copy link
Member

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.

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;
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.samoa.core.ContentEvent;
import org.apache.samoa.instances.Instance;
import org.apache.samoa.learners.InstanceContentEvent;
import org.apache.samoa.learners.ModelContentEvent;
import org.apache.samoa.learners.ClassificationModelContentEvent;
import org.apache.samoa.learners.ResultContentEvent;
import org.apache.samoa.moa.core.DoubleVector;
import org.apache.samoa.moa.core.Utils;
Expand Down Expand Up @@ -59,8 +59,8 @@ public class BoostingPredictionCombinerProcessor extends PredictionCombinerProce
@Override
public boolean process(ContentEvent event) {
// for serialize
if (event instanceof ModelContentEvent) {
return this.processModel((ModelContentEvent) event);
if (event instanceof ClassificationModelContentEvent) {
return this.processModel((ClassificationModelContentEvent) event);
}

ResultContentEvent inEvent = (ResultContentEvent) event;
Expand Down
Loading