import pandas as pd df = pd.read_csv("house_data.csv") # 查看前五行数据 class LinearRegressionByMyself(object): # 初始化学习率和循环迭代次数 def __init__(self, Learning_rate = 0.001, epoch = 20): self.Learning_rate = Learning_rate self.epoch = epoch # 定义训练fit函数,传入训练集(X),测试集(y) def fit(self, X, y): # 初始化权重(w) self.w = np.zeros(1 + X.shape[1]) self.cost_list = [] # 更新权重 for i in range(self.epoch): output = self.Regression_input(X) error = (y - output) self.w[1:] += self.Learning_rate * X.T.dot(error) self.w[0] += self.Learning_rate * error.sum() cost = (error ** 2).sum() / 2.0 self.cost_list.append(cost) return self # 定义线性回归的输入计算函数 def Regression_input(self, X): return np.dot(X, self.w[1:]) + self.w[0] # 定义线性回归的预测函数 def predict(self, X): return self.Regression_input(X) X = df[["RM"]].values y = df["MEDV"].values from sklearn.preprocessing import StandardScaler StandardScaler_X = StandardScaler() StandardScaler_y = StandardScaler() X_Standard = StandardScaler_X.fit_transform(X) y_Standard = StandardScaler_y.fit_transform(y.reshape(-1,1)) model = LinearRegressionByMyself() model.fit(X_Standard, y_Standard)
这是我全部的代码,到最后一行就报错了,请问怎么修改呢。之前按照老师给的代码,在y_Standard = StandardScaler_y.fit_transform(y)会报错