'LinearRegression' object has no attribute 'intercept_'
时间: 2023-09-21 14:10:38 浏览: 314
解决运行出现dict object has no attribute has_key问题
5星 · 资源好评率100%
This error message usually occurs when you try to access the 'intercept_' attribute of a LinearRegression object before calling the 'fit' method to train the model. The 'intercept_' attribute is only available after fitting the model to the data using the 'fit' method.
To solve this issue, make sure that you call the 'fit' method on your LinearRegression object before trying to access its 'intercept_' attribute. Here's an example code snippet:
```
from sklearn.linear_model import LinearRegression
import numpy as np
# create some sample data
X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([5, 10, 15])
# create a LinearRegression object
lr = LinearRegression()
# fit the model to the data
lr.fit(X, y)
# access the intercept_ attribute
print(lr.intercept_)
```
This should output the intercept value of the trained model.
阅读全文