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

AttributeError: 'Ridge' object has no attribute 'feature_names_in_' #21696

Closed
21564669 opened this issue Nov 17, 2021 · 1 comment
Closed

AttributeError: 'Ridge' object has no attribute 'feature_names_in_' #21696

21564669 opened this issue Nov 17, 2021 · 1 comment

Comments

@21564669
Copy link

21564669 commented Nov 17, 2021

Describe the bug

the issue appears in the example of https://scikit-learn.org/stable/auto_examples/linear_model/plot_ridge_coeffs.html#sphx-glr-auto-examples-linear-model-plot-ridge-coeffs-py

in the following piece of code, if we add 'print(f"clf.feature_names_in:{clf.feature_names_in_}")' after the fit() function is called,
it will pop up an issue that 'AttributeError: 'Ridge' object has no attribute 'feature_names_in_''

# Train the model with different regularisation strengths
for a in alphas:
    clf.set_params(alpha=a)
    clf.fit(X, y)
    print(f"clf.feature_names_in:{clf.feature_names_in_}")
    coefs.append(clf.coef_)
    errors.append(mean_squared_error(clf.coef_, w))

sklearn version is 1.01

Steps/Code to Reproduce

import matplotlib.pyplot as plt
import numpy as np

from sklearn.datasets import make_regression
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error

clf = Ridge()

X, y, w = make_regression(
    n_samples=10, n_features=10, coef=True, random_state=2, bias=0.0
)
coefs = []
errors = []

alphas = np.logspace(-6, 6, 200)
for a in alphas:
    clf.set_params(alpha=a)
    clf.fit(X, y)
    print(f"clf.feature_names_in:{clf.feature_names_in_}")
    coefs.append(clf.coef_)
    errors.append(mean_squared_error(clf.coef_, w))

Expected Results

it is expected to print the attribute of feature_names_in_

Actual Results

but it raised an error.
AttributeError: 'Ridge' object has no attribute 'feature_names_in_'

Versions

System:
python: 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)]
executable: C:\Users\ezqiuya\AppData\Local\Programs\Python\Python38\python.exe
machine: Windows-10-10.0.18362-SP0

Python dependencies:
pip: 21.3.1
setuptools: 59.1.1
sklearn: 1.0.1
numpy: 1.21.4
scipy: 1.7.2
Cython: 0.29.24
pandas: 1.3.4
matplotlib: 3.5.0
joblib: 1.1.0
threadpoolctl: 3.0.0

Built with OpenMP: True

@glemaitre
Copy link
Member

glemaitre commented Nov 18, 2021

As documented in the attributes section of the Ridge documentation (and this rule apply to all estimator), feature_names_in_ is only available if the X as all string columns:

In [1]: import matplotlib.pyplot as plt
   ...: import numpy as np
   ...: import pandas as pd
   ...: from sklearn.datasets import make_regression
   ...: from sklearn.linear_model import Ridge
   ...: from sklearn.metrics import mean_squared_error
   ...: 
   ...: clf = Ridge()
   ...: 
   ...: X, y, w = make_regression(
   ...:     n_samples=10, n_features=10, coef=True, random_state=2, bias=0.0
   ...: )
   ...: X = pd.DataFrame(X, columns=[f"Features {i}" for i in range(X.shape[1])])
   ...: coefs = []
   ...: errors = []
   ...: 
   ...: alphas = np.logspace(-6, 6, 200)
   ...: for a in alphas:
   ...:     clf.set_params(alpha=a)
   ...:     clf.fit(X, y)
   ...:     print(f"clf.feature_names_in:{clf.feature_names_in_}")
   ...:     coefs.append(clf.coef_)
   ...:     errors.append(mean_squared_error(clf.coef_, w))
   ...: 

In your case, a NumPy array has no column names so you could generate the column name with range(X.shape[1]).

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