-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
[ML] adds multi-class feature importance support #53803
Changes from all commits
15ae0c6
5fd61c2
2632205
2adfbaf
47b4225
fcf853a
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,97 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
package org.elasticsearch.xpack.core.ml.inference.results; | ||
|
||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class FeatureImportance implements Writeable { | ||
|
||
private final Map<String, Double> classImportance; | ||
private final double importance; | ||
private final String featureName; | ||
private static final String IMPORTANCE = "importance"; | ||
private static final String FEATURE_NAME = "feature_name"; | ||
|
||
public static FeatureImportance forRegression(String featureName, double importance) { | ||
return new FeatureImportance(featureName, importance, null); | ||
} | ||
|
||
public static FeatureImportance forClassification(String featureName, Map<String, Double> classImportance) { | ||
return new FeatureImportance(featureName, classImportance.values().stream().mapToDouble(Math::abs).sum(), classImportance); | ||
} | ||
|
||
private FeatureImportance(String featureName, double importance, Map<String, Double> classImportance) { | ||
this.featureName = Objects.requireNonNull(featureName); | ||
this.importance = importance; | ||
this.classImportance = classImportance == null ? null : Collections.unmodifiableMap(classImportance); | ||
} | ||
|
||
public FeatureImportance(StreamInput in) throws IOException { | ||
this.featureName = in.readString(); | ||
this.importance = in.readDouble(); | ||
if (in.readBoolean()) { | ||
this.classImportance = in.readMap(StreamInput::readString, StreamInput::readDouble); | ||
} else { | ||
this.classImportance = null; | ||
} | ||
} | ||
|
||
public Map<String, Double> getClassImportance() { | ||
return classImportance; | ||
} | ||
|
||
public double getImportance() { | ||
return importance; | ||
} | ||
|
||
public String getFeatureName() { | ||
return featureName; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(this.featureName); | ||
out.writeDouble(this.importance); | ||
out.writeBoolean(this.classImportance != null); | ||
if (this.classImportance != null) { | ||
out.writeMap(this.classImportance, StreamOutput::writeString, StreamOutput::writeDouble); | ||
} | ||
} | ||
|
||
public Map<String, Object> toMap() { | ||
Map<String, Object> map = new LinkedHashMap<>(); | ||
map.put(FEATURE_NAME, featureName); | ||
map.put(IMPORTANCE, importance); | ||
if (classImportance != null) { | ||
classImportance.forEach(map::put); | ||
} | ||
return map; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (object == this) { return true; } | ||
if (object == null || getClass() != object.getClass()) { return false; } | ||
FeatureImportance that = (FeatureImportance) object; | ||
return Objects.equals(featureName, that.featureName) | ||
&& Objects.equals(importance, that.importance) | ||
&& Objects.equals(classImportance, that.classImportance); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(featureName, importance, classImportance); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,45 +8,46 @@ | |
import org.elasticsearch.Version; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public abstract class SingleValueInferenceResults implements InferenceResults { | ||
|
||
private final double value; | ||
private final Map<String, Double> featureImportance; | ||
private final List<FeatureImportance> featureImportance; | ||
|
||
static Map<String, Double> takeTopFeatureImportances(Map<String, Double> unsortedFeatureImportances, int numTopFeatures) { | ||
return unsortedFeatureImportances.entrySet() | ||
.stream() | ||
.sorted((l, r)-> Double.compare(Math.abs(r.getValue()), Math.abs(l.getValue()))) | ||
static List<FeatureImportance> takeTopFeatureImportances(List<FeatureImportance> unsortedFeatureImportances, int numTopFeatures) { | ||
if (unsortedFeatureImportances == null || unsortedFeatureImportances.isEmpty()) { | ||
return unsortedFeatureImportances; | ||
} | ||
return unsortedFeatureImportances.stream() | ||
.sorted((l, r)-> Double.compare(Math.abs(r.getImportance()), Math.abs(l.getImportance()))) | ||
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. Is the abs necessary when the score is a norm? If the score can be -ve why is it wrong to use the -ve value? 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. Score is not absolutely the norm. Additionally, we want to have the MOST influential values, regardless of direction. We could have feature importances like this:
If we want the top two influential features, we want A and C. The |
||
.limit(numTopFeatures) | ||
.collect(LinkedHashMap::new, (h, e) -> h.put(e.getKey(), e.getValue()) , LinkedHashMap::putAll); | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
SingleValueInferenceResults(StreamInput in) throws IOException { | ||
value = in.readDouble(); | ||
if (in.getVersion().onOrAfter(Version.V_7_7_0)) { | ||
this.featureImportance = in.readMap(StreamInput::readString, StreamInput::readDouble); | ||
this.featureImportance = in.readList(FeatureImportance::new); | ||
} else { | ||
this.featureImportance = Collections.emptyMap(); | ||
this.featureImportance = Collections.emptyList(); | ||
} | ||
} | ||
|
||
SingleValueInferenceResults(double value, Map<String, Double> featureImportance) { | ||
SingleValueInferenceResults(double value, List<FeatureImportance> featureImportance) { | ||
this.value = value; | ||
this.featureImportance = ExceptionsHelper.requireNonNull(featureImportance, "featureImportance"); | ||
this.featureImportance = featureImportance == null ? Collections.emptyList() : featureImportance; | ||
} | ||
|
||
public Double value() { | ||
return value; | ||
} | ||
|
||
public Map<String, Double> getFeatureImportance() { | ||
public List<FeatureImportance> getFeatureImportance() { | ||
return featureImportance; | ||
} | ||
|
||
|
@@ -58,7 +59,7 @@ public String valueAsString() { | |
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeDouble(value); | ||
if (out.getVersion().onOrAfter(Version.V_7_7_0)) { | ||
out.writeMap(this.featureImportance, StreamOutput::writeString, StreamOutput::writeDouble); | ||
out.writeList(this.featureImportance); | ||
} | ||
} | ||
|
||
|
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.
I am not 100% convinced this should be
abs
.We don't write the feature importance value on the native side by looking at the
norm
of the vector.Do we want to make this the norm too? Or do we thing
abs
is good enough?@tveasey @valeriy42
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.
Can you please provide more context. What are you calculating here?
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.
@valeriy42 @tveasey this is calculating the "overall importance" of all the classes combined for a given feature. This is so we can measure "most important feature" independent of the classes.
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.
norm
would make it an L2 norm,abs
makes it an L1 norm. Either way is suitable. I think,abs
is better, sincenorm
over-treats larger importances and ignores smaller once.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.
+1 abs