Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IGNITE-7451: Make Linear SVM for multi-classification
this closes #3484
- Loading branch information
Showing
15 changed files
with
512 additions
and
44 deletions.
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
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
89 changes: 89 additions & 0 deletions
89
...les/ml/src/main/java/org/apache/ignite/ml/svm/SVMLinearMultiClassClassificationModel.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,89 @@ | |||
/* | |||
* 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.HashMap; | |||
import java.util.Map; | |||
import java.util.Objects; | |||
import java.util.TreeMap; | |||
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 multi-classification model for set of SVM classifiers. */ | |||
public class SVMLinearMultiClassClassificationModel implements Model<Vector, Double>, Exportable<SVMLinearMultiClassClassificationModel>, Serializable { | |||
/** List of models associated with each class. */ | |||
private Map<Double, SVMLinearBinaryClassificationModel> models; | |||
|
|||
/** */ | |||
public SVMLinearMultiClassClassificationModel() { | |||
this.models = new HashMap<>(); | |||
} | |||
|
|||
/** {@inheritDoc} */ | |||
@Override public Double apply(Vector input) { | |||
TreeMap<Double, Double> maxMargins = new TreeMap<>(); | |||
|
|||
models.forEach((k, v) -> maxMargins.put(input.dot(v.weights()) + v.intercept(), k)); | |||
|
|||
return maxMargins.lastEntry().getValue(); | |||
} | |||
|
|||
/** {@inheritDoc} */ | |||
@Override public <P> void saveModel(Exporter<SVMLinearMultiClassClassificationModel, 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; | |||
SVMLinearMultiClassClassificationModel mdl = (SVMLinearMultiClassClassificationModel)o; | |||
return Objects.equals(models, mdl.models); | |||
} | |||
|
|||
/** {@inheritDoc} */ | |||
@Override public int hashCode() { | |||
return Objects.hash(models); | |||
} | |||
|
|||
/** {@inheritDoc} */ | |||
@Override public String toString() { | |||
StringBuilder wholeStr = new StringBuilder(); | |||
|
|||
models.forEach((clsLb, mdl) -> { | |||
wholeStr.append("The class with label " + clsLb + " has classifier: " + mdl.toString() + System.lineSeparator()); | |||
}); | |||
|
|||
return wholeStr.toString(); | |||
} | |||
|
|||
/** | |||
* Adds a specific SVM binary classifier to the bunch of same classifiers. | |||
* | |||
* @param clsLb The class label for the added model. | |||
* @param mdl The model. | |||
*/ | |||
public void add(double clsLb, SVMLinearBinaryClassificationModel mdl) { | |||
models.put(clsLb, mdl); | |||
} | |||
} |
Oops, something went wrong.