question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid warning about feature names missing in KNeighborsRegressor

See original GitHub issue

Describe the bug

When I call fit() then predict() on a KNeighborsRegressor, passing a DataFrame with the same column names both times, I get a warning:

[...]/python3.8/site-packages/sklearn/base.py:441: UserWarning: X does not have valid feature names, but KNeighborsRegressor was fitted with feature names
  warnings.warn(

Steps/Code to Reproduce

import numpy as np
import pandas as pd
from sklearn.neighbors import KNeighborsRegressor

df = pd.DataFrame({"A": np.random.rand(10)})
k = KNeighborsRegressor().fit(df, np.random.rand(10))
y_pred = k.predict(df)

Expected Results

No warning: I’ve provided a DataFrame with the same columns both times.

Actual Results

A warning.

Versions

System:
    python: 3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:50:38)  [Clang 11.1.0 ]
executable: /Users/ageron/miniconda3/envs/homl3/bin/python
   machine: macOS-10.15.7-x86_64-i386-64bit

Python dependencies:
          pip: 21.3
   setuptools: 58.2.0
      sklearn: 1.0
        numpy: 1.19.5
        scipy: 1.7.1
       Cython: None
       pandas: 1.3.3
   matplotlib: 3.4.3
       joblib: 0.14.1
threadpoolctl: 3.0.0

Built with OpenMP: True

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
glemaitrecommented, Oct 28, 2021

I confirm that I don’t get a warning anymore.

0reactions
glemaitrecommented, Dec 27, 2021

Please open a new issue to know the scikit-learn version used. It should be solved in 1.0.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

SKLearn warning "valid feature names" in version 1.0
When I trimmed to remove the headers and sent only values then the error got disappeared. Sample code before and after changes. Code...
Read more >
sklearn.neighbors.KNeighborsRegressor
Names of features seen during fit. Defined only when X has feature names that are all strings. New in version 1.0. n_samples_fit_int.
Read more >
Guide to the K-Nearest Neighbors Algorithm in Python and ...
In this detailed definitive guide - learn how K-Nearest Neighbors works, and how to implement it for regression, classification and anomaly ...
Read more >
09_tabular code: UserWarning: X does not have valid feature ...
Here is the error that comes up /opt/conda/lib/python3.7/site-packages/sklearn/base.py:446: UserWarning: X does not have valid feature names ...
Read more >
scikit-learn 1.1 Released - Exxact Corporation
sklearn.impute. Enhancement impute.SimpleImputer now warns with feature names when features which are skipped due to the lack of any observed ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found