AttributeError: 'Ridge' object has no attribute 'coef_
时间: 2023-12-04 09:02:07 浏览: 339
Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
5星 · 资源好评率100%
这个错误通常是因为在使用Ridge模型之前没有调用fit()方法进行训练。请确保在调用Ridge模型的coef_属性之前,已经使用fit()方法对模型进行了训练。以下是一个使用岭回归模型的例子,其中包括了对模型的训练和对coef_属性的调用:
```python
from sklearn.linear_model import Ridge
import sklearn.datasets as datasets
from sklearn.model_selection import train_test_split
house = datasets.load_boston()
feature = house['data'][:,4:10] # 选取6列模型
target = house['target']
X_train, X_test, y_train, y_test = train_test_split(feature, target, test_size=0.3, random_state=0)
r = Ridge(alpha=1) # alpha数值越大,正则化力度越大
r.fit(X_train, y_train)
print(r.coef_) # 输出:[-0.063, 0.055, -0.007, -0.819, 0.364, -0.012]
```
阅读全文