Python自编线性回归模型估计程序(梯度下降)
import numpy as npclass reg(object): def __init__(self, eta=0.001, n_iter=100): self.eta = eta self.n_iter = n_iter def fit(self, X, y): self.w_ = np.zeros(1 + X.shape[1]) self.cost_ = [] for i in range(self.n_iter): output = self.net_input(X) errors = (y - output) self.w_[1:] += self.eta * X.T.dot(errors) self.w_[0] += self.eta * errors.sum() cost = (errors**2).sum() / 2.0 self.cost_.append(cost) return self def net_input(self, X): return np.dot(X, self.w_[1:]) + self.w_[0] def predict(self, X): return self.net_input(X)
调用自编reg程序估计模型:
import numpy as npimport pandas as pdimport osos.chdir('C:/python')df = pd.read_csv('./housing.data.txt', header=None, sep='\s+')df.columns = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV']print(df.head(3))# 数据预处理X = df[['RM']].valuesy = df['MEDV'].valuesfrom sklearn.preprocessing import StandardScalersc_x = StandardScaler()sc_y = StandardScaler()X_std = sc_x.fit_transform(X)y_std = sc_y.fit_transform(y[:, np.newaxis]).flatten()from reg import * #导入自编程序lr = reg()lr.fit(X_std, y_std)print("Estimation Results:")print(lr.w_[1]) #访问参数,self.w_print(lr.w_[0])print(lr.w_)num_rooms_std = sc_x.transform(np.array([[5.0]]))print(lr.predict(num_rooms_std)) #调用预测函数print(sc_y.inverse_transform(lr.predict(num_rooms_std)))
结果: