Skip to content

Commit

Permalink
[ML] Add type FeatureImportClassName
Browse files Browse the repository at this point in the history
[ML] Make check for isClassification and isRegression FI better
[ML] Remove redundant baseline check after above change
[ML] Refactor computeMultiClassImportanceDenominator to only loop once inside
[ML] Refactor buildClassificationDecisionPathData
[ML] Make test cases smaller
  • Loading branch information
qn895 committed Oct 18, 2020
1 parent 55ab819 commit 10eae2d
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 255 deletions.
22 changes: 14 additions & 8 deletions x-pack/plugins/ml/common/types/feature_importance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

export type FeatureImportClassName = string | number | boolean;

export interface ClassFeatureImportance {
class_name: string | number | boolean;
class_name: FeatureImportClassName;
importance: number;
}
export interface FeatureImportance {
Expand All @@ -15,15 +17,15 @@ export interface FeatureImportance {
}

export interface TopClass {
class_name: string;
class_name: FeatureImportClassName;
class_probability: number;
class_score: number;
}

export type TopClasses = TopClass[];

export interface ClassFeatureImportanceSummary {
class_name: string;
class_name: FeatureImportClassName;
importance: {
max: number;
min: number;
Expand All @@ -50,7 +52,7 @@ export type TotalFeatureImportance =
| RegressionTotalFeatureImportance;

export interface FeatureImportanceClassBaseline {
class_name: string | number | boolean;
class_name: FeatureImportClassName;
baseline: number;
}
export interface ClassificationFeatureImportanceBaseline {
Expand Down Expand Up @@ -78,13 +80,17 @@ export function isRegressionTotalFeatureImportance(
}

export function isClassificationFeatureImportanceBaseline(
baselineData: ClassificationFeatureImportanceBaseline | RegressionFeatureImportanceBaseline
baselineData: any
): baselineData is ClassificationFeatureImportanceBaseline {
return (baselineData as ClassificationFeatureImportanceBaseline).classes !== undefined;
return (
typeof baselineData === 'object' &&
baselineData.hasOwnProperty('classes') &&
Array.isArray(baselineData.classes)
);
}

export function isRegressionFeatureImportanceBaseline(
baselineData: ClassificationFeatureImportanceBaseline | RegressionFeatureImportanceBaseline
baselineData: any
): baselineData is RegressionFeatureImportanceBaseline {
return (baselineData as RegressionFeatureImportanceBaseline).baseline !== undefined;
return typeof baselineData === 'object' && baselineData.hasOwnProperty('baseline');
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ANALYSIS_CONFIG_TYPE } from '../../../data_frame_analytics/common';
import { ClassificationDecisionPath } from './decision_path_classification';
import { useMlKibana } from '../../../contexts/kibana';
import { DataFrameAnalysisConfigType } from '../../../../../common/types/data_frame_analytics';
import { getStringBasedClassName } from './use_classification_path_data';

interface DecisionPathPopoverProps {
featureImportance: FeatureImportance[];
Expand All @@ -36,7 +37,7 @@ enum DECISION_PATH_TABS {
}

export interface ExtendedFeatureImportance extends FeatureImportance {
absImportance?: number;
absImportance: number;
}

export const DecisionPathPopover: FC<DecisionPathPopoverProps> = ({
Expand Down Expand Up @@ -116,23 +117,24 @@ export const DecisionPathPopover: FC<DecisionPathPopoverProps> = ({
/>
</EuiText>
{analysisType === ANALYSIS_CONFIG_TYPE.CLASSIFICATION &&
baseline &&
isClassificationFeatureImportanceBaseline(baseline) && (
<ClassificationDecisionPath
featureImportance={featureImportance}
topClasses={topClasses as TopClasses}
predictedValue={predictedValue as string}
predictedValue={getStringBasedClassName(predictedValue)}
predictionFieldName={predictionFieldName}
baseline={baseline}
/>
)}
{analysisType === ANALYSIS_CONFIG_TYPE.REGRESSION &&
baseline &&
isRegressionFeatureImportanceBaseline(baseline) && (
isRegressionFeatureImportanceBaseline(baseline) &&
predictedValue !== undefined && (
<RegressionDecisionPath
featureImportance={featureImportance}
baseline={baseline}
predictedValue={predictedValue as number}
predictedValue={
typeof predictedValue === 'string' ? parseFloat(predictedValue) : predictedValue
}
predictionFieldName={predictionFieldName}
/>
)}
Expand Down
Loading

0 comments on commit 10eae2d

Please sign in to comment.