Transformations: Fix bug where having NaN in the input to regression analysis transformation causes all predictions to be NaN (#80079)

Make regression analysis transformation handle NaN
This commit is contained in:
Oscar Kilhed 2024-01-05 15:50:20 +01:00 committed by GitHub
parent 2563b7b330
commit f5a221e93f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -152,6 +152,34 @@ describe('Regression transformation', () => {
expect(result[1].fields[0].values[8]).toBeCloseTo(3.55, 1);
expect(result[1].fields[0].values[9]).toBe(4);
});
it('should filter NaNs', () => {
const source = [
toDataFrame({
name: 'data',
refId: 'A',
fields: [
{ name: 'y', type: FieldType.number, values: [0, 1, 2, 3, NaN] },
{ name: 'x', type: FieldType.number, values: [0, 1, 2, 3, 4] },
],
}),
];
const config: RegressionTransformerOptions = {
modelType: ModelType.linear,
predictionCount: 5,
xFieldName: 'x',
yFieldName: 'y',
};
const result = RegressionTransformer.transformer(config, {} as DataTransformContext)(source);
expect(result[1].fields[1].values[0]).toBe(0);
expect(result[1].fields[1].values[1]).toBe(1);
expect(result[1].fields[1].values[2]).toBe(2);
expect(result[1].fields[1].values[3]).toBe(3);
expect(result[1].fields[1].values[4]).toBe(4);
});
});
function toEquableDataFrame(source: DataFrame): DataFrame {

View File

@ -84,7 +84,7 @@ export const RegressionTransformer: SynchronousDataTransformerInfo<RegressionTra
const xValues = [];
for (let i = 0; i < xField.values.length; i++) {
if (yField.values[i] !== null) {
if (yField.values[i] !== null && !isNaN(yField.values[i])) {
xValues.push(xField.values[i] - normalizationSubtrahend);
yValues.push(yField.values[i]);
}