You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ch...@apache.org on 2018/01/25 09:38:57 UTC
ignite git commit: IGNITE-7316: Make Linear SVM for binary
classification
Repository: ignite
Updated Branches:
refs/heads/master 340569b8f -> 63aad1aed
IGNITE-7316: Make Linear SVM for binary classification
this closes #3399
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/63aad1ae
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/63aad1ae
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/63aad1ae
Branch: refs/heads/master
Commit: 63aad1aed7708fc1311e65c0eab7e50ce419b01a
Parents: 340569b
Author: zaleslaw <za...@gmail.com>
Authored: Thu Jan 25 12:38:46 2018 +0300
Committer: YuriBabak <y....@gmail.com>
Committed: Thu Jan 25 12:38:46 2018 +0300
----------------------------------------------------------------------
.../apache/ignite/ml/structures/Dataset.java | 21 +-
.../apache/ignite/ml/structures/DatasetRow.java | 1 -
.../ignite/ml/structures/LabeledDataset.java | 17 +-
.../structures/LabeledDatasetTestTrainPair.java | 4 +-
.../preprocessing/LabeledDatasetLoader.java | 31 +--
.../preprocessing/LabellingMachine.java | 10 +-
.../ml/structures/preprocessing/Normalizer.java | 2 +
.../SVMLinearBinaryClassificationTrainer.java | 245 +++++++++++++++++++
.../ml/svm/SVMLinearClassificationModel.java | 176 +++++++++++++
.../org/apache/ignite/ml/svm/package-info.java | 22 ++
.../org/apache/ignite/ml/IgniteMLTestSuite.java | 2 +
.../org/apache/ignite/ml/LocalModelsTest.java | 18 ++
.../ignite/ml/knn/LabeledDatasetTest.java | 1 +
.../apache/ignite/ml/math/ExternalizeTest.java | 1 +
.../org/apache/ignite/ml/svm/BaseSVMTest.java | 58 +++++
...dLinearSVMClassificationSCDATrainerTest.java | 34 +++
.../ml/svm/GenericLinearSVMTrainerTest.java | 139 +++++++++++
...lLinearSVMClassificationSCDATrainerTest.java | 37 +++
.../org/apache/ignite/ml/svm/SVMModelTest.java | 112 +++++++++
.../org/apache/ignite/ml/svm/SVMTestSuite.java | 34 +++
.../ml/src/test/resources/datasets/README.md | 5 +-
21 files changed, 936 insertions(+), 34 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/main/java/org/apache/ignite/ml/structures/Dataset.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/structures/Dataset.java b/modules/ml/src/main/java/org/apache/ignite/ml/structures/Dataset.java
index cb50516..89ff1b4 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/structures/Dataset.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/structures/Dataset.java
@@ -41,6 +41,9 @@ public class Dataset<Row extends DatasetRow> implements Serializable, Externaliz
/** Amount of attributes in each vector. */
protected int colSize;
+ /** */
+ protected boolean isDistributed;
+
/**
* Default constructor (required by Externalizable).
*/
@@ -65,7 +68,7 @@ public class Dataset<Row extends DatasetRow> implements Serializable, Externaliz
* @param colSize Amount of observed attributes in each vector.
*/
public Dataset(Row[] data, String[] featureNames, int colSize) {
- this(data.length, colSize, featureNames);
+ this(data.length, colSize, featureNames, false);
assert data != null;
@@ -89,7 +92,7 @@ public class Dataset<Row extends DatasetRow> implements Serializable, Externaliz
* @param colSize Amount of attributes. Should be > 0
* @param featureNames Column names.
*/
- public Dataset(int rowSize, int colSize, String[] featureNames) {
+ public Dataset(int rowSize, int colSize, String[] featureNames, boolean isDistributed) {
assert rowSize > 0;
assert colSize > 0;
@@ -102,6 +105,7 @@ public class Dataset<Row extends DatasetRow> implements Serializable, Externaliz
this.rowSize = rowSize;
this.colSize = colSize;
+ this.isDistributed = isDistributed;
}
/** */
@@ -179,6 +183,16 @@ public class Dataset<Row extends DatasetRow> implements Serializable, Externaliz
return data[idx];
}
+ /** */
+ public boolean isDistributed() {
+ return isDistributed;
+ }
+
+ /** */
+ public void setDistributed(boolean distributed) {
+ isDistributed = distributed;
+ }
+
/**
* Get the features.
*
@@ -211,6 +225,7 @@ public class Dataset<Row extends DatasetRow> implements Serializable, Externaliz
res = 31 * res + Arrays.hashCode(meta);
res = 31 * res + rowSize;
res = 31 * res + colSize;
+ res = 31 * res + (isDistributed ? 1 : 0);
return res;
}
@@ -220,6 +235,7 @@ public class Dataset<Row extends DatasetRow> implements Serializable, Externaliz
out.writeObject(meta);
out.writeInt(rowSize);
out.writeInt(colSize);
+ out.writeBoolean(isDistributed);
}
/** {@inheritDoc} */
@@ -228,5 +244,6 @@ public class Dataset<Row extends DatasetRow> implements Serializable, Externaliz
meta = (FeatureMetadata[]) in.readObject();
rowSize = in.readInt();
colSize = in.readInt();
+ isDistributed = in.readBoolean();
}
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/main/java/org/apache/ignite/ml/structures/DatasetRow.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/structures/DatasetRow.java b/modules/ml/src/main/java/org/apache/ignite/ml/structures/DatasetRow.java
index 3ba0cf7..f70f7ca 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/structures/DatasetRow.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/structures/DatasetRow.java
@@ -29,7 +29,6 @@ public class DatasetRow<V extends Vector> implements Serializable, Externalizabl
/** Vector. */
protected V vector;
-
/**
* Default constructor (required by Externalizable).
*/
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java b/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java
index c5581cb..15e8c40 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java
@@ -22,8 +22,7 @@ import org.apache.ignite.ml.math.exceptions.CardinalityException;
import org.apache.ignite.ml.math.exceptions.NoDataException;
import org.apache.ignite.ml.math.exceptions.knn.NoLabelVectorException;
import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
-import org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector;
-import org.jetbrains.annotations.NotNull;
+import org.apache.ignite.ml.math.impls.vector.SparseDistributedVector;
/**
* Class for set of labeled vectors.
@@ -66,13 +65,13 @@ public class LabeledDataset<L, Row extends LabeledVector> extends Dataset<Row> {
* @param isDistributed Use distributed data structures to keep data.
*/
public LabeledDataset(int rowSize, int colSize, String[] featureNames, boolean isDistributed){
- super(rowSize, colSize, featureNames);
+ super(rowSize, colSize, featureNames, isDistributed);
- initializeDataWithLabeledVectors(rowSize, colSize, isDistributed);
+ initializeDataWithLabeledVectors();
}
/** */
- private void initializeDataWithLabeledVectors(int rowSize, int colSize, boolean isDistributed) {
+ private void initializeDataWithLabeledVectors() {
data = (Row[])new LabeledVector[rowSize];
for (int i = 0; i < rowSize; i++)
data[i] = (Row)new LabeledVector(emptyVector(colSize, isDistributed), null);
@@ -191,9 +190,11 @@ public class LabeledDataset<L, Row extends LabeledVector> extends Dataset<Row> {
}
/** */
- @NotNull public static Vector emptyVector(int size, boolean isDistributed) {
+ public static Vector emptyVector(int size, boolean isDistributed) {
- if(isDistributed) return new SparseBlockDistributedVector(size);
- else return new DenseLocalOnHeapVector(size);
+ if(isDistributed)
+ return new SparseDistributedVector(size);
+ else
+ return new DenseLocalOnHeapVector(size);
}
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDatasetTestTrainPair.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDatasetTestTrainPair.java b/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDatasetTestTrainPair.java
index baf72d8..f362fbc 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDatasetTestTrainPair.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDatasetTestTrainPair.java
@@ -78,8 +78,8 @@ public class LabeledDatasetTestTrainPair implements Serializable {
}
}
- test = new LabeledDataset(testVectors, testSize);
- train = new LabeledDataset(trainVectors, trainSize);
+ test = new LabeledDataset(testVectors, dataset.colSize());
+ train = new LabeledDataset(trainVectors, dataset.colSize());
}
/** This method generates "random double, integer" pairs, sort them, gets first "testSize" elements and returns appropriate indices */
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/LabeledDatasetLoader.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/LabeledDatasetLoader.java b/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/LabeledDatasetLoader.java
index 0faa416..b9d56ae 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/LabeledDatasetLoader.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/LabeledDatasetLoader.java
@@ -95,24 +95,25 @@ public class LabeledDatasetLoader {
int colSize, int rowIdx, String[] rowData) {
final Vector vec = LabeledDataset.emptyVector(colSize, isDistributed);
- for (int j = 0; j < colSize; j++) {
+ if (isFallOnBadData && rowData.length != colSize + 1)
+ throw new CardinalityException(colSize + 1, rowData.length);
- if (rowData.length == colSize + 1) {
- double val = fillMissedData();
+ double missedData = fillMissedData();
- try {
- val = Double.parseDouble(rowData[j + 1]);
- vec.set(j, val);
- }
- catch (NumberFormatException e) {
- if (isFallOnBadData)
- throw new FileParsingException(rowData[j + 1], rowIdx, pathToFile);
- else
- vec.set(j, val);
- }
+ for (int j = 0; j < colSize; j++) {
+ try {
+ double feature = Double.parseDouble(rowData[j + 1]);
+ vec.set(j, feature);
+ }
+ catch (NumberFormatException e) {
+ if (isFallOnBadData)
+ throw new FileParsingException(rowData[j + 1], rowIdx, pathToFile);
+ else
+ vec.set(j, missedData);
+ }
+ catch (ArrayIndexOutOfBoundsException e){
+ vec.set(j, missedData);
}
- else
- throw new CardinalityException(colSize + 1, rowData.length);
}
return vec;
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/LabellingMachine.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/LabellingMachine.java b/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/LabellingMachine.java
index 44719cf..e8d88ad 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/LabellingMachine.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/LabellingMachine.java
@@ -17,23 +17,23 @@
package org.apache.ignite.ml.structures.preprocessing;
-import org.apache.ignite.ml.knn.models.KNNModel;
+import org.apache.ignite.ml.Model;
import org.apache.ignite.ml.structures.LabeledDataset;
/** Data pre-processing step which assigns labels to all observations according model. */
public class LabellingMachine {
/**
- * Set labels to each observation according passed Model.
+ * Set labels to each observation according passed model.
* <p>
* NOTE: In-place operation.
* </p>
* @param ds The given labeled dataset.
- * @param knnMdl The given kNN Model.
+ * @param mdl The given model.
* @return Dataset with predicted labels.
*/
- public static LabeledDataset assignLabels(LabeledDataset ds, KNNModel knnMdl) {
+ public static LabeledDataset assignLabels(LabeledDataset ds, Model mdl) {
for (int i = 0; i < ds.rowSize(); i++) {
- double predictedCls = knnMdl.apply(ds.getRow(i).features());
+ double predictedCls = (double) mdl.apply(ds.getRow(i).features());
ds.setLabel(i, predictedCls);
}
return ds;
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/Normalizer.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/Normalizer.java b/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/Normalizer.java
index 26d8bf9..161ec35 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/Normalizer.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/structures/preprocessing/Normalizer.java
@@ -54,6 +54,8 @@ public class Normalizer {
for (int j = 0; j < colSize; j++) {
double div = maxs[j] - mins[j];
+ if(div == 0)
+ continue;
for (int i = 0; i < rowSize; i++) {
double oldVal = data[i].features().get(j);
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearBinaryClassificationTrainer.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearBinaryClassificationTrainer.java b/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearBinaryClassificationTrainer.java
new file mode 100644
index 0000000..a14694b
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearBinaryClassificationTrainer.java
@@ -0,0 +1,245 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.ignite.ml.svm;
+
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.ignite.ml.Trainer;
+import org.apache.ignite.ml.math.Vector;
+import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
+import org.apache.ignite.ml.math.impls.vector.SparseDistributedVector;
+import org.apache.ignite.ml.structures.LabeledDataset;
+import org.apache.ignite.ml.structures.LabeledVector;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Base class for a soft-margin SVM linear classification trainer based on the communication-efficient distributed dual
+ * coordinate ascent algorithm (CoCoA) with hinge-loss function. <p> This trainer takes input as Labeled Dataset with -1
+ * and +1 labels for two classes and makes binary classification. </p> The paper about this algorithm could be found
+ * here https://arxiv.org/abs/1409.1458.
+ */
+public class SVMLinearBinaryClassificationTrainer implements Trainer<SVMLinearClassificationModel, LabeledDataset> {
+ /** Amount of outer SDCA algorithm iterations. */
+ private int amountOfIterations = 20;
+
+ /** Amount of local SDCA algorithm iterations. */
+ private int amountOfLocIterations = 50;
+
+ /** Regularization parameter. */
+ private double lambda = 0.2;
+
+ /** This flag enables distributed mode for this algorithm. */
+ private boolean isDistributed;
+
+ /**
+ * Returns model based on data
+ *
+ * @param data data to build model
+ * @return model
+ */
+ @Override public SVMLinearClassificationModel train(LabeledDataset data) {
+ isDistributed = data.isDistributed();
+
+ final int weightVectorSizeWithIntercept = data.colSize() + 1;
+ Vector weights = initializeWeightsWithZeros(weightVectorSizeWithIntercept);
+
+ for (int i = 0; i < this.getAmountOfIterations(); i++) {
+ Vector deltaWeights = calculateUpdates(data, weights);
+ weights = weights.plus(deltaWeights); // creates new vector
+ }
+
+ return new SVMLinearClassificationModel(weights.viewPart(1, weights.size() - 1), weights.get(0));
+ }
+
+ /** */
+ @NotNull private Vector initializeWeightsWithZeros(int vectorSize) {
+ if (isDistributed)
+ return new SparseDistributedVector(vectorSize);
+ else
+ return new DenseLocalOnHeapVector(vectorSize);
+ }
+
+ /** */
+ private Vector calculateUpdates(LabeledDataset data, Vector weights) {
+ Vector copiedWeights = weights.copy();
+ Vector deltaWeights = initializeWeightsWithZeros(weights.size());
+
+ final int amountOfObservation = data.rowSize();
+
+ Vector tmpAlphas = initializeWeightsWithZeros(amountOfObservation);
+ Vector deltaAlphas = initializeWeightsWithZeros(amountOfObservation);
+
+ for (int i = 0; i < this.getAmountOfLocIterations(); i++) {
+ int randomIdx = ThreadLocalRandom.current().nextInt(amountOfObservation);
+
+ Deltas deltas = getDeltas(data, copiedWeights, amountOfObservation, tmpAlphas, randomIdx);
+
+ copiedWeights = copiedWeights.plus(deltas.deltaWeights); // creates new vector
+ deltaWeights = deltaWeights.plus(deltas.deltaWeights); // creates new vector
+
+ tmpAlphas.set(randomIdx, tmpAlphas.get(randomIdx) + deltas.deltaAlpha);
+ deltaAlphas.set(randomIdx, deltaAlphas.get(randomIdx) + deltas.deltaAlpha);
+ }
+ return deltaWeights;
+ }
+
+ /** */
+ private Deltas getDeltas(LabeledDataset data, Vector copiedWeights, int amountOfObservation, Vector tmpAlphas,
+ int randomIdx) {
+ LabeledVector row = (LabeledVector)data.getRow(randomIdx);
+ Double lb = (Double)row.label();
+ Vector v = makeVectorWithInterceptElement(row);
+
+ double alpha = tmpAlphas.get(randomIdx);
+
+ return maximize(lb, v, alpha, copiedWeights, amountOfObservation);
+ }
+
+ /** */
+ private Vector makeVectorWithInterceptElement(LabeledVector row) {
+ Vector vec = row.features().like(row.features().size() + 1);
+
+ vec.set(0, 1); // set intercept element
+
+ for (int j = 0; j < row.features().size(); j++)
+ vec.set(j + 1, row.features().get(j));
+
+ return vec;
+ }
+
+ /** */
+ private Deltas maximize(double lb, Vector v, double alpha, Vector weights, int amountOfObservation) {
+ double gradient = calcGradient(lb, v, weights, amountOfObservation);
+ double prjGrad = calculateProjectionGradient(alpha, gradient);
+
+ return calcDeltas(lb, v, alpha, prjGrad, weights.size(), amountOfObservation);
+ }
+
+ /** */
+ private Deltas calcDeltas(double lb, Vector v, double alpha, double gradient, int vectorSize,
+ int amountOfObservation) {
+ if (gradient != 0.0) {
+
+ double qii = v.dot(v);
+ double newAlpha = calcNewAlpha(alpha, gradient, qii);
+
+ Vector deltaWeights = v.times(lb * (newAlpha - alpha) / (this.lambda() * amountOfObservation));
+
+ return new Deltas(newAlpha - alpha, deltaWeights);
+ }
+ else
+ return new Deltas(0.0, initializeWeightsWithZeros(vectorSize));
+ }
+
+ /** */
+ private double calcNewAlpha(double alpha, double gradient, double qii) {
+ if (qii != 0.0)
+ return Math.min(Math.max(alpha - (gradient / qii), 0.0), 1.0);
+ else
+ return 1.0;
+ }
+
+ /** */
+ private double calcGradient(double lb, Vector v, Vector weights, int amountOfObservation) {
+ double dotProduct = v.dot(weights);
+ return (lb * dotProduct - 1.0) * (this.lambda() * amountOfObservation);
+ }
+
+ /** */
+ private double calculateProjectionGradient(double alpha, double gradient) {
+ if (alpha <= 0.0)
+ return Math.min(gradient, 0.0);
+
+ else if (alpha >= 1.0)
+ return Math.max(gradient, 0.0);
+
+ else
+ return gradient;
+ }
+
+ /**
+ * Set up the regularization parameter.
+ * @param lambda The regularization parameter. Should be more than 0.0.
+ * @return Trainer with new lambda parameter value.
+ */
+ public SVMLinearBinaryClassificationTrainer withLambda(double lambda) {
+ assert lambda > 0.0;
+ this.lambda = lambda;
+ return this;
+ }
+
+ /**
+ * Gets the regularization lambda.
+ * @return The parameter value.
+ */
+ public double lambda() {
+ return lambda;
+ }
+
+ /**
+ * Gets the amount of outer iterations of SCDA algorithm.
+ * @return The parameter value.
+ */
+ public int getAmountOfIterations() {
+ return amountOfIterations;
+ }
+
+ /**
+ * Set up the amount of outer iterations of SCDA algorithm.
+ * @param amountOfIterations The parameter value.
+ * @return Trainer with new amountOfIterations parameter value.
+ */
+ public SVMLinearBinaryClassificationTrainer withAmountOfIterations(int amountOfIterations) {
+ this.amountOfIterations = amountOfIterations;
+ return this;
+ }
+
+ /**
+ * Gets the amount of local iterations of SCDA algorithm.
+ * @return The parameter value.
+ */
+ public int getAmountOfLocIterations() {
+ return amountOfLocIterations;
+ }
+
+ /**
+ * Set up the amount of local iterations of SCDA algorithm.
+ * @param amountOfLocIterations The parameter value.
+ * @return Trainer with new amountOfLocIterations parameter value.
+ */
+ public SVMLinearBinaryClassificationTrainer withAmountOfLocIterations(int amountOfLocIterations) {
+ this.amountOfLocIterations = amountOfLocIterations;
+ return this;
+ }
+}
+
+/** This is a helper class to handle pair results which are returned from the calculation method. */
+class Deltas {
+ /** */
+ public double deltaAlpha;
+
+ /** */
+ public Vector deltaWeights;
+
+ /** */
+ public Deltas(double deltaAlpha, Vector deltaWeights) {
+ this.deltaAlpha = deltaAlpha;
+ this.deltaWeights = deltaWeights;
+ }
+}
+
+
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearClassificationModel.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearClassificationModel.java b/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearClassificationModel.java
new file mode 100644
index 0000000..e807128
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearClassificationModel.java
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.ignite.ml.svm;
+
+import java.io.Serializable;
+import java.util.Objects;
+import org.apache.ignite.ml.Exportable;
+import org.apache.ignite.ml.Exporter;
+import org.apache.ignite.ml.Model;
+import org.apache.ignite.ml.math.Vector;
+
+/**
+ * Base class for SVM linear classification model.
+ */
+public class SVMLinearClassificationModel implements Model<Vector, Double>, Exportable<SVMLinearClassificationModel>, Serializable {
+ /** Output label format. -1 and +1 for false value and raw distances from the separating hyperplane otherwise. */
+ private boolean isKeepingRawLabels = false;
+
+ /** Threshold to assign +1 label to the observation if raw value more than this threshold. */
+ private double threshold = 0.0;
+
+ /** Multiplier of the objects's vector required to make prediction. */
+ private Vector weights;
+
+ /** Intercept of the linear regression model. */
+ private double intercept;
+
+ /** */
+ public SVMLinearClassificationModel(Vector weights, double intercept) {
+ this.weights = weights;
+ this.intercept = intercept;
+ }
+
+ /**
+ * Set up the output label format.
+ * @param isKeepingRawLabels The parameter value.
+ * @return Model with new isKeepingRawLabels parameter value.
+ */
+ public SVMLinearClassificationModel withRawLabels(boolean isKeepingRawLabels) {
+ this.isKeepingRawLabels = isKeepingRawLabels;
+ return this;
+ }
+
+ /**
+ * Set up the threshold.
+ * @param threshold The parameter value.
+ * @return Model with new threshold parameter value.
+ */
+ public SVMLinearClassificationModel withThreshold(double threshold) {
+ this.threshold = threshold;
+ return this;
+ }
+
+ /**
+ * Set up the weights.
+ * @param weights The parameter value.
+ * @return Model with new weights parameter value.
+ */
+ public SVMLinearClassificationModel withWeights(Vector weights) {
+ this.weights = weights;
+ return this;
+ }
+
+ /**
+ * Set up the intercept.
+ * @param intercept The parameter value.
+ * @return Model with new intercept parameter value.
+ */
+ public SVMLinearClassificationModel withIntercept(double intercept) {
+ this.intercept = intercept;
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Double apply(Vector input) {
+ final double res = input.dot(weights) + intercept;
+ if (isKeepingRawLabels)
+ return res;
+ else
+ return res - threshold > 0 ? 1.0 : -1.0;
+ }
+
+ /**
+ * Gets the output label format mode.
+ * @return The parameter value.
+ */
+ public boolean isKeepingRawLabels() {
+ return isKeepingRawLabels;
+ }
+
+ /**
+ * Gets the threshold.
+ * @return The parameter value.
+ */
+ public double threshold() {
+ return threshold;
+ }
+
+ /**
+ * Gets the weights.
+ * @return The parameter value.
+ */
+ public Vector weights() {
+ return weights;
+ }
+
+ /**
+ * Gets the intercept.
+ * @return The parameter value.
+ */
+ public double intercept() {
+ return intercept;
+ }
+
+ /** {@inheritDoc} */
+ @Override public <P> void saveModel(Exporter<SVMLinearClassificationModel, P> exporter, P path) {
+ exporter.save(this, path);
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object o) {
+ if (this == o)
+ return true;
+ if (o == null || getClass() != o.getClass())
+ return false;
+ SVMLinearClassificationModel mdl = (SVMLinearClassificationModel)o;
+ return Double.compare(mdl.intercept, intercept) == 0
+ && Double.compare(mdl.threshold, threshold) == 0
+ && Boolean.compare(mdl.isKeepingRawLabels, isKeepingRawLabels) == 0
+ && Objects.equals(weights, mdl.weights);
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ return Objects.hash(weights, intercept, isKeepingRawLabels, threshold);
+ }
+
+ /** {@inheritDoc} */
+ @Override public String toString() {
+ if (weights.size() < 10) {
+ StringBuilder builder = new StringBuilder();
+
+ for (int i = 0; i < weights.size(); i++) {
+ double nextItem = i == weights.size() - 1 ? intercept : weights.get(i + 1);
+
+ builder.append(String.format("%.4f", Math.abs(weights.get(i))))
+ .append("*x")
+ .append(i)
+ .append(nextItem > 0 ? " + " : " - ");
+ }
+
+ builder.append(String.format("%.4f", Math.abs(intercept)));
+ return builder.toString();
+ }
+
+ return "LinearRegressionModel{" +
+ "weights=" + weights +
+ ", intercept=" + intercept +
+ '}';
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/main/java/org/apache/ignite/ml/svm/package-info.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/svm/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/svm/package-info.java
new file mode 100644
index 0000000..1593f3d
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/svm/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/**
+ * <!-- Package description. -->
+ * Contains main APIs for SVM(support vector machines) algorithms.
+ */
+package org.apache.ignite.ml.svm;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/java/org/apache/ignite/ml/IgniteMLTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/IgniteMLTestSuite.java b/modules/ml/src/test/java/org/apache/ignite/ml/IgniteMLTestSuite.java
index d5d6d94..c42efc5 100644
--- a/modules/ml/src/test/java/org/apache/ignite/ml/IgniteMLTestSuite.java
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/IgniteMLTestSuite.java
@@ -23,6 +23,7 @@ import org.apache.ignite.ml.math.MathImplMainTestSuite;
import org.apache.ignite.ml.nn.MLPTestSuite;
import org.apache.ignite.ml.optimization.OptimizationTestSuite;
import org.apache.ignite.ml.regressions.RegressionsTestSuite;
+import org.apache.ignite.ml.svm.SVMTestSuite;
import org.apache.ignite.ml.trainers.group.TrainersGroupTestSuite;
import org.apache.ignite.ml.trees.DecisionTreesTestSuite;
import org.junit.runner.RunWith;
@@ -35,6 +36,7 @@ import org.junit.runners.Suite;
@Suite.SuiteClasses({
MathImplMainTestSuite.class,
RegressionsTestSuite.class,
+ SVMTestSuite.class,
ClusteringTestSuite.class,
DecisionTreesTestSuite.class,
KNNTestSuite.class,
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/java/org/apache/ignite/ml/LocalModelsTest.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/LocalModelsTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/LocalModelsTest.java
index 862a9c1..bcbc1fc 100644
--- a/modules/ml/src/test/java/org/apache/ignite/ml/LocalModelsTest.java
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/LocalModelsTest.java
@@ -31,6 +31,7 @@ import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix;
import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
import org.apache.ignite.ml.regressions.linear.LinearRegressionModel;
import org.apache.ignite.ml.structures.LabeledDataset;
+import org.apache.ignite.ml.svm.SVMLinearClassificationModel;
import org.junit.Assert;
import org.junit.Test;
@@ -78,6 +79,23 @@ public class LocalModelsTest {
}
/** */
+ @Test
+ public void importExportSVMClassificationModelTest() throws IOException {
+ executeModelTest(mdlFilePath -> {
+ SVMLinearClassificationModel mdl = new SVMLinearClassificationModel(new DenseLocalOnHeapVector(new double[]{1, 2}), 3);
+ Exporter<SVMLinearClassificationModel, String> exporter = new FileExporter<>();
+ mdl.saveModel(exporter, mdlFilePath);
+
+ SVMLinearClassificationModel load = exporter.load(mdlFilePath);
+
+ Assert.assertNotNull(load);
+ Assert.assertEquals("", mdl, load);
+
+ return null;
+ });
+ }
+
+ /** */
private void executeModelTest(Function<String, Void> code) throws IOException {
Path mdlPath = Files.createTempFile(null, null);
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/java/org/apache/ignite/ml/knn/LabeledDatasetTest.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/knn/LabeledDatasetTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/knn/LabeledDatasetTest.java
index c4ae70f..079ae55 100644
--- a/modules/ml/src/test/java/org/apache/ignite/ml/knn/LabeledDatasetTest.java
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/knn/LabeledDatasetTest.java
@@ -266,6 +266,7 @@ public class LabeledDatasetTest extends BaseKNNTest implements ExternalizableTes
assertEquals(lbs[i], labels[i]);
}
+ /** */
@Override public void testExternalization() {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/java/org/apache/ignite/ml/math/ExternalizeTest.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/ExternalizeTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/ExternalizeTest.java
index b99d85e..5dd798f 100644
--- a/modules/ml/src/test/java/org/apache/ignite/ml/math/ExternalizeTest.java
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/ExternalizeTest.java
@@ -31,6 +31,7 @@ import static org.junit.Assert.fail;
/**
* Common test for externalization.
+ * TODO: IGNITE-7325 remove this class from all test and change on ExternalizableTest
*/
public abstract class ExternalizeTest<T extends Externalizable & Destroyable> {
/** */
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/java/org/apache/ignite/ml/svm/BaseSVMTest.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/svm/BaseSVMTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/svm/BaseSVMTest.java
new file mode 100644
index 0000000..424118d
--- /dev/null
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/svm/BaseSVMTest.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.ignite.ml.svm;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+
+/**
+ * Base class for SVM tests.
+ */
+public class BaseSVMTest extends GridCommonAbstractTest {
+ /** Count of nodes. */
+ private static final int NODE_COUNT = 4;
+
+ /** Grid instance. */
+ protected Ignite ignite;
+
+ /**
+ * Default constructor.
+ */
+ public BaseSVMTest() {
+ super(false);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override protected void beforeTest() throws Exception {
+ ignite = grid(NODE_COUNT);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void beforeTestsStarted() throws Exception {
+ for (int i = 1; i <= NODE_COUNT; i++)
+ startGrid(i);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void afterTestsStopped() throws Exception {
+ stopAllGrids();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/java/org/apache/ignite/ml/svm/DistributedLinearSVMClassificationSCDATrainerTest.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/svm/DistributedLinearSVMClassificationSCDATrainerTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/svm/DistributedLinearSVMClassificationSCDATrainerTest.java
new file mode 100644
index 0000000..a4a91a4
--- /dev/null
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/svm/DistributedLinearSVMClassificationSCDATrainerTest.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.ignite.ml.svm;
+
+import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix;
+import org.apache.ignite.ml.regressions.linear.LinearRegressionSGDTrainer;
+
+/**
+ * Tests for {@link LinearRegressionSGDTrainer} on {@link DenseLocalOnHeapMatrix}.
+ */
+public class DistributedLinearSVMClassificationSCDATrainerTest extends GenericLinearSVMTrainerTest {
+ /** */
+ public DistributedLinearSVMClassificationSCDATrainerTest() {
+ super(
+ new SVMLinearBinaryClassificationTrainer(),
+ true,
+ 1e-2);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/java/org/apache/ignite/ml/svm/GenericLinearSVMTrainerTest.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/svm/GenericLinearSVMTrainerTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/svm/GenericLinearSVMTrainerTest.java
new file mode 100644
index 0000000..1cd2a50
--- /dev/null
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/svm/GenericLinearSVMTrainerTest.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.ignite.ml.svm;
+
+import java.util.concurrent.ThreadLocalRandom;
+import org.apache.ignite.internal.util.IgniteUtils;
+import org.apache.ignite.ml.TestUtils;
+import org.apache.ignite.ml.Trainer;
+import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
+import org.apache.ignite.ml.structures.LabeledDataset;
+import org.apache.ignite.ml.structures.LabeledVector;
+import org.junit.Test;
+
+/**
+ * Base class for all linear regression trainers.
+ */
+public class GenericLinearSVMTrainerTest extends BaseSVMTest {
+ /** Fixed size of Dataset. */
+ private static final int AMOUNT_OF_OBSERVATIONS = 100;
+
+ /** Fixed size of columns in Dataset. */
+ private static final int AMOUNT_OF_FEATURES = 2;
+
+ /** */
+ private final Trainer<SVMLinearClassificationModel, LabeledDataset> trainer;
+
+ /** */
+ private boolean isDistributed;
+
+ /** */
+ private final double precision;
+
+ /** */
+ GenericLinearSVMTrainerTest(
+ Trainer<SVMLinearClassificationModel, LabeledDataset> trainer,
+ boolean isDistributed,
+ double precision) {
+ super();
+ this.trainer = trainer;
+ this.precision = precision;
+ this.isDistributed = isDistributed;
+ }
+
+ /**
+ * Test trainer on classification model y = x.
+ */
+ @Test
+ public void testTrainWithTheLinearlySeparableCase() {
+ if (isDistributed)
+ IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
+
+ LabeledDataset dataset = new LabeledDataset<Double, LabeledVector>(AMOUNT_OF_OBSERVATIONS, AMOUNT_OF_FEATURES, isDistributed);
+
+ ThreadLocalRandom rndX = ThreadLocalRandom.current();
+ ThreadLocalRandom rndY = ThreadLocalRandom.current();
+ for (int i = 0; i < AMOUNT_OF_OBSERVATIONS; i++) {
+ double x = rndX.nextDouble(-1000, 1000);
+ double y = rndY.nextDouble(-1000, 1000);
+ dataset.features(i).set(0, x);
+ dataset.features(i).set(1, y);
+ double lb = y - x > 0 ? 1 : -1;
+ dataset.setLabel(i, lb);
+ }
+
+ SVMLinearClassificationModel mdl = trainer.train(dataset);
+
+ TestUtils.assertEquals(-1, mdl.apply(new DenseLocalOnHeapVector(new double[] {100, 10})), precision);
+ TestUtils.assertEquals(1, mdl.apply(new DenseLocalOnHeapVector(new double[] {10, 100})), precision);
+ }
+
+ /**
+ * Test trainer on classification model y = x. Amount of generated points is increased 10 times.
+ */
+ @Test
+ public void testTrainWithTheLinearlySeparableCase10() {
+ if (isDistributed)
+ IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
+
+ LabeledDataset dataset = new LabeledDataset<Double, LabeledVector>(AMOUNT_OF_OBSERVATIONS * 10, AMOUNT_OF_FEATURES, isDistributed);
+
+ ThreadLocalRandom rndX = ThreadLocalRandom.current();
+ ThreadLocalRandom rndY = ThreadLocalRandom.current();
+ for (int i = 0; i < AMOUNT_OF_OBSERVATIONS * 10; i++) {
+ double x = rndX.nextDouble(-1000, 1000);
+ double y = rndY.nextDouble(-1000, 1000);
+ dataset.features(i).set(0, x);
+ dataset.features(i).set(1, y);
+ double lb = y - x > 0 ? 1 : -1;
+ dataset.setLabel(i, lb);
+ }
+
+ SVMLinearClassificationModel mdl = trainer.train(dataset);
+
+ TestUtils.assertEquals(-1, mdl.apply(new DenseLocalOnHeapVector(new double[] {100, 10})), precision);
+ TestUtils.assertEquals(1, mdl.apply(new DenseLocalOnHeapVector(new double[] {10, 100})), precision);
+ }
+
+ /**
+ * Test trainer on classification model y = x. Amount of generated points is increased 100 times.
+ */
+ @Test
+ public void testTrainWithTheLinearlySeparableCase100() {
+ if (isDistributed)
+ IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
+
+ LabeledDataset dataset = new LabeledDataset<Double, LabeledVector>(AMOUNT_OF_OBSERVATIONS * 100, AMOUNT_OF_FEATURES, isDistributed);
+
+ ThreadLocalRandom rndX = ThreadLocalRandom.current();
+ ThreadLocalRandom rndY = ThreadLocalRandom.current();
+ for (int i = 0; i < AMOUNT_OF_OBSERVATIONS * 100; i++) {
+ double x = rndX.nextDouble(-1000, 1000);
+ double y = rndY.nextDouble(-1000, 1000);
+ dataset.features(i).set(0, x);
+ dataset.features(i).set(1, y);
+ double lb = y - x > 0 ? 1 : -1;
+ dataset.setLabel(i, lb);
+ }
+
+ SVMLinearClassificationModel mdl = trainer.train(dataset);
+
+ TestUtils.assertEquals(-1, mdl.apply(new DenseLocalOnHeapVector(new double[] {100, 10})), precision);
+ TestUtils.assertEquals(1, mdl.apply(new DenseLocalOnHeapVector(new double[] {10, 100})), precision);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/java/org/apache/ignite/ml/svm/LocalLinearSVMClassificationSCDATrainerTest.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/svm/LocalLinearSVMClassificationSCDATrainerTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/svm/LocalLinearSVMClassificationSCDATrainerTest.java
new file mode 100644
index 0000000..68860d2
--- /dev/null
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/svm/LocalLinearSVMClassificationSCDATrainerTest.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.ignite.ml.svm;
+
+import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix;
+import org.apache.ignite.ml.regressions.linear.LinearRegressionSGDTrainer;
+
+/**
+ * Tests for {@link LinearRegressionSGDTrainer} on {@link DenseLocalOnHeapMatrix}.
+ */
+public class LocalLinearSVMClassificationSCDATrainerTest extends GenericLinearSVMTrainerTest {
+ /** */
+ public LocalLinearSVMClassificationSCDATrainerTest() {
+ super(
+ new SVMLinearBinaryClassificationTrainer()
+ .withLambda(0.2)
+ .withAmountOfIterations(10)
+ .withAmountOfLocIterations(20),
+ false,
+ 1e-2);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/java/org/apache/ignite/ml/svm/SVMModelTest.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/svm/SVMModelTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/svm/SVMModelTest.java
new file mode 100644
index 0000000..1742593
--- /dev/null
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/svm/SVMModelTest.java
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.ignite.ml.svm;
+
+import org.apache.ignite.ml.TestUtils;
+import org.apache.ignite.ml.math.Vector;
+import org.apache.ignite.ml.math.exceptions.CardinalityException;
+import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
+import org.apache.ignite.ml.regressions.linear.LinearRegressionModel;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for {@link LinearRegressionModel}.
+ */
+public class SVMModelTest {
+ /** */
+ private static final double PRECISION = 1e-6;
+
+ /** */
+ @Test
+ public void testPredictWithRawLabels() {
+ Vector weights = new DenseLocalOnHeapVector(new double[]{2.0, 3.0});
+ SVMLinearClassificationModel mdl = new SVMLinearClassificationModel(weights, 1.0).withRawLabels(true);
+
+ Vector observation = new DenseLocalOnHeapVector(new double[]{1.0, 1.0});
+ TestUtils.assertEquals(1.0 + 2.0 * 1.0 + 3.0 * 1.0, mdl.apply(observation), PRECISION);
+
+ observation = new DenseLocalOnHeapVector(new double[]{2.0, 1.0});
+ TestUtils.assertEquals(1.0 + 2.0 * 2.0 + 3.0 * 1.0, mdl.apply(observation), PRECISION);
+
+ observation = new DenseLocalOnHeapVector(new double[]{1.0, 2.0});
+ TestUtils.assertEquals(1.0 + 2.0 * 1.0 + 3.0 * 2.0, mdl.apply(observation), PRECISION);
+
+ observation = new DenseLocalOnHeapVector(new double[]{-2.0, 1.0});
+ TestUtils.assertEquals(1.0 - 2.0 * 2.0 + 3.0 * 1.0, mdl.apply(observation), PRECISION);
+
+ observation = new DenseLocalOnHeapVector(new double[]{1.0, -2.0});
+ TestUtils.assertEquals(1.0 + 2.0 * 1.0 - 3.0 * 2.0, mdl.apply(observation), PRECISION);
+
+ Assert.assertEquals(true, mdl.isKeepingRawLabels());
+ }
+
+ /** */
+ @Test
+ public void testPredictWithErasedLabels() {
+ Vector weights = new DenseLocalOnHeapVector(new double[]{1.0, 1.0});
+ SVMLinearClassificationModel mdl = new SVMLinearClassificationModel(weights, 1.0);
+
+ Vector observation = new DenseLocalOnHeapVector(new double[]{1.0, 1.0});
+ TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
+
+ observation = new DenseLocalOnHeapVector(new double[]{3.0, 4.0});
+ TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
+
+ observation = new DenseLocalOnHeapVector(new double[]{-1.0, -1.0});
+ TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
+
+ observation = new DenseLocalOnHeapVector(new double[]{-2.0, 1.0});
+ TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
+
+ observation = new DenseLocalOnHeapVector(new double[]{-1.0, -2.0});
+ TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
+
+ mdl.withIntercept(-2.0).withWeights(new DenseLocalOnHeapVector(new double[]{-2.0, -2.0}));
+ observation = new DenseLocalOnHeapVector(new double[]{-1.0, -2.0});
+ TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
+ TestUtils.assertEquals(-2.0, mdl.intercept(), PRECISION);
+ }
+
+ /** */
+ @Test
+ public void testPredictWithErasedLabelsAndChangedThreshold() {
+ Vector weights = new DenseLocalOnHeapVector(new double[]{1.0, 1.0});
+ SVMLinearClassificationModel mdl = new SVMLinearClassificationModel(weights, 1.0).withThreshold(5);
+
+ Vector observation = new DenseLocalOnHeapVector(new double[]{1.0, 1.0});
+ TestUtils.assertEquals(-1.0, mdl.apply(observation), PRECISION);
+
+ observation = new DenseLocalOnHeapVector(new double[]{3.0, 4.0});
+ TestUtils.assertEquals(1.0, mdl.apply(observation), PRECISION);
+
+ TestUtils.assertEquals(5, mdl.threshold(), PRECISION);
+ }
+
+ /** */
+ @Test(expected = CardinalityException.class)
+ public void testPredictOnAnObservationWithWrongCardinality() {
+ Vector weights = new DenseLocalOnHeapVector(new double[]{2.0, 3.0});
+
+ SVMLinearClassificationModel mdl = new SVMLinearClassificationModel(weights, 1.0);
+
+ Vector observation = new DenseLocalOnHeapVector(new double[]{1.0});
+
+ mdl.apply(observation);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/java/org/apache/ignite/ml/svm/SVMTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/svm/SVMTestSuite.java b/modules/ml/src/test/java/org/apache/ignite/ml/svm/SVMTestSuite.java
new file mode 100644
index 0000000..bfc341c
--- /dev/null
+++ b/modules/ml/src/test/java/org/apache/ignite/ml/svm/SVMTestSuite.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.ignite.ml.svm;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+
+/**
+ * Test suite for all tests located in org.apache.ignite.ml.regressions.* package.
+ */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+ LocalLinearSVMClassificationSCDATrainerTest.class,
+ DistributedLinearSVMClassificationSCDATrainerTest.class,
+ SVMModelTest.class
+})
+public class SVMTestSuite {
+ // No-op.
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/63aad1ae/modules/ml/src/test/resources/datasets/README.md
----------------------------------------------------------------------
diff --git a/modules/ml/src/test/resources/datasets/README.md b/modules/ml/src/test/resources/datasets/README.md
index 2f9c5ec..128626e 100644
--- a/modules/ml/src/test/resources/datasets/README.md
+++ b/modules/ml/src/test/resources/datasets/README.md
@@ -1,2 +1,5 @@
iris.txt and cleared_machines are from Lichman, M. (2013). UCI Machine Learning Repository [http://archive.ics.uci.edu/ml]. Irvine, CA: University of California, School of Information and Computer Science.
-Read more about machine dataset http://archive.ics.uci.edu/ml/machine-learning-databases/cpu-performance/machine.names
\ No newline at end of file
+Read more about machine dataset http://archive.ics.uci.edu/ml/machine-learning-databases/cpu-performance/machine.names
+
+
+titanic dataset has next columns survived pclass sex age sibsp parch fare