运行代码,出现TypeError: fit() missing 1 required positional argument: 'y',对其进行修改

from sklearn.datasets import load_boston
boston = load_boston()
print(boston.DESCR)

#导入model_selection进行数据分割
from sklearn.model_selection import train_test_split
import numpy as np
x = boston.data
y = boston.target

x_train, x_test, y_train, y_test = train_test_split(x, y , test_size=0.25, random_state=33)
print("The max target value is", np.max(boston.target))
print("The min target value is", np.min(boston.target))
print("The average target value is", np.mean(boston.target))

from sklearn.preprocessing import StandardScaler
ss_x = StandardScaler()
ss_y = StandardScaler()

x_train = ss_x.fit_transform(x_train)
x_test = ss_x.transform(x_test)
y_train = ss_y.fit_transform(y_train.reshape(-1, 1))
y_test = ss_y.transform(y_test.reshape(-1, 1))

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(x_train, y_train)
lr_y_pred = lr.predict(x_test)

from sklearn.linear_model import SGDRegressor
sgdr = SGDRegressor
sgdr.fit(x_train, y_train.ravel())
sgdr_y_pred = sgdr.predict(x_test)


print('The value of default measurement of LinearRegression is', lr.score(x_train, y_train))

from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error

print('The value of R-sqaured of LinearRegression is', r2_score(y_test, lr_y_pred))
print('The mean squared error of LinearRegression is', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(lr_y_pred)))
print('The mean absolute error of LinearRegression is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(lr_y_pred)))


print('The value of default of SDGRegressor is', sgdr.score(x_test, y_test))
print('The vlue of R-squared of SGDRegressor is', r2_score(y_test, sgdr_y_pred))
print('The mean squared error of SGDRegressor is', mean_squared_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(sgdr_y_pred)))
print('The mean absolute error of SGDRegressor is', mean_absolute_error(ss_y.inverse_transform(y_test), ss_y.inverse_transform(sgdr_y_pred)))

运行上面程序会出现以下问题

Traceback (most recent call last):
  File "D:\Python362\a_机器学习及实战\LinearRegression.py", line 32, in 
    sgdr.fit(x_train, y_train.ravel())
TypeError: fit() missing 1 required positional argument: 'y'
意思大概是fit的时候缺少参数y。仔细检查和上网百度了之后发现sgdr = SGDRegressor初始化的时候没有加(),后修改为sgdr = SGDRegressor()这一错误不再提醒



你可能感兴趣的:(python机器学习及实战)