haifengl / smile

Statistical Machine Intelligence & Learning Engine
https://haifengl.github.io
Other
5.97k stars 1.13k forks source link

Predict requires DataFrame that contains the predicted variable in 3.1.0 #773

Closed smpawlowski closed 1 month ago

smpawlowski commented 1 month ago

Describe the bug LinearModel.predict requires DataFrame that contains the predicted variable. This likely affects other models as well.

Expected behavior Predict should work with explanatory variables only.

Actual behavior Predict throws when predicted variable is not included in the DataFrame.

Code snippet

@Test
public void test_formula_OLS() {
    double[] x = {1, 2, 3};
    double[] y = {1, 2, 3};
    DataFrame df = DataFrame.of(DoubleVector.of("x", x),
                                DoubleVector.of("y", y));
    LinearModel regr = OLS.fit(Formula.lhs("y"), df);

    double[] x_pred = {4, 5, 6};
    double[] y_pred = regr.predict(DataFrame.of( DoubleVector.of("x", x_pred)));
    for(int i=0; i<x_pred.length; i++) {
        Assert.assertEquals(x_pred[i], y_pred[i], 1e-9);
    }
}

Exception

java.lang.IllegalArgumentException: Field y doesn't exist

at smile.data.type.StructType.indexOf(StructType.java:103)
at smile.data.formula.Variable$1.<init>(Variable.java:80)
at smile.data.formula.Variable.bind(Variable.java:78)
at smile.data.formula.Formula.bind(Formula.java:360)
at smile.data.formula.Formula.x(Formula.java:497)
at smile.data.formula.Formula.matrix(Formula.java:546)
at smile.regression.LinearModel.predict(LinearModel.java:358)
at models.TestSmileRegression.test_formula_OLS(TestSmileRegression.java:22)

Input data In the snippet ^

Additional context

haifengl commented 1 month ago

Thanks for reporting. The fix is in the master branch now.

haifengl commented 1 month ago

Please let's know if it address your issues. If so, we will make a new release. Thanks!

smpawlowski commented 1 month ago

Hi! The code and test update look good. Unfortunately I can't fully test at the moment because of firewall/source code restrictions at my company. I can easily test if you make a release. Thanks for looking into it so quickly!

haifengl commented 1 month ago

v3.1.1 is released.

smpawlowski commented 1 month ago

3.1.1 fixes the issue! Thanks again!