我无法让Sklearn Ridge回归器正常工作

时间:2020-04-28 19:31:18

标签: python

我正在构建一些机器学习模型,但无法使sklearn.linear_model Ridge运行。 Mo代码发布在下面:

%matplotlib inline

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from sklearn.preprocessing import scale 
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Ridge, RidgeCV, Lasso, LassoCV
from sklearn.metrics import mean_squared_error

url = "https://gist.githubusercontent.com/keeganhines/59974f1ebef97bbaa44fb19143f90bad/raw/d9bcf657f97201394a59fffd801c44347eb7e28d/Hitters.csv"
df = pd.read_csv('Hitters.csv').dropna()
print(df.info(), df.head())

dummies = pd.get_dummies(df[['League', 'Division', 'NewLeague']])
y = df.Salary

# Drop the column with the independent variable (Salary), and columns for which we created dummy variables
X_ = df.drop(['Salary', 'League', 'Division', 'NewLeague'], axis = 1).astype('float64')

# Define the feature set X.
X = pd.concat([X_, dummies[['League_N', 'Division_W', 'NewLeague_N']]], axis = 1)

X.info()

alphas = 10**np.linspace(10,-2,100)*0.5

ridge = Ridge(normalize = True)
coefs = []

for a in alphas:
    ridge.set_params(alpha = a)
    ridge.fit(X, y)
    coefs.append(ridge.coef_)

np.shape(coefs)

然后,当我运行代码时,它因以下错误而失败。有什么我可以解决的。搜索互联网没有提供任何线索。

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
 in ()
      4 for a in alphas:
      5     ridge.set_params(alpha = a)
----> 6     ridge.fit(X, y)
      7     coefs.append(ridge.coef_)
      8 
RuntimeError: Null pointer in input arguments

0 个答案:

没有答案
相关问题