Skip to content
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

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

Closed
smpawlowski opened this issue May 16, 2024 · 5 comments
Closed

Comments

@smpawlowski
Copy link

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

  • Sample code throws in ver 3.1.0, but works fine in 2.6.0
@haifengl
Copy link
Owner

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

@haifengl
Copy link
Owner

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

@smpawlowski
Copy link
Author

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
Copy link
Owner

v3.1.1 is released.

@smpawlowski
Copy link
Author

3.1.1 fixes the issue! Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants