Python代码实现 首先建立linear_regression.py文件,用于实现线性回归的类文件,包含了线性回归内部的核心函数: # -*- coding: utf-8 -*- import numpy as np class LinerRegression(object): def __init__(self, learning_rate=0.01, max_iter=100, seed=None): np.random.seed(seed) self.lr = learning_rate self.max_iter = max_iter self.w = np.random.normal(1, 0.1) self.b = np.random.normal(1, 0.1) self.loss_arr = [] def fit(self, x, y): self.x = x self.y = y for i in range(self.max_iter): self._train_step() self.loss_arr.append(self.loss()) # print('loss:\t{:.3}'.format(self.loss())) # print('w: \t{:.3}'.format(self.w)) # print('b: \t{:.3}'.format(self.b)) def _f(self, x, w, b): return x * w + b def predict(self, x=None): if x is None: x = self.x y_pred = self._f(x, self.w, self.b) return y_pred def loss(self, y_true=None, y_pred=None): if y_true is None or y_pred is None: y_true = self.y y_pred = self.predict(self.x) return np.mean((y_true - y_pred)**2) def _calc_gradient(self): d_w = np.mean((self.x * self.w + self.b - self.y) * self.x) d_b = np.mean(self.x * self.w + self.b - self.y) return d_w, d_b def _train_step(self): d_w, d_b = self._calc_gradient() self.w = self.w - self.lr * d_w self.b = self.b - self.lr * d_b return self.w, self.b 建立 train.py 文件,用于生成模拟数据,并调用 liner_regression.py 中的类,完成线性回归任务 # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt def show_data(x, y, w=None, b=None): plt.scatter(x, y, marker='.') plt.text(2,200,r'Least Square Method',fontsize=13) if w is not None and b is not None: plt.plot(x, w*x+b, c='red') plt.show() # data generation np.random.seed(272) data_size = 100 x = np.random.uniform(low=1.0, high=10.0,size=data_size) y = x * 20 + 10 + np.random.normal(loc=0.0,scale=10.0, size=data_size) plt.scatter(x, y, marker='.') plt.text(2,200,r'Data Set',fontsize=13) plt.show() # train / test split shuffled_index =np.random.permutation(data_size) x = x[shuffled_index] y = y[shuffled_index] split_index = int(data_size * 0.7) x_train = x[:split_index] y_train = y[:split_index] x_test = x[split_index:] y_test = y[split_index:] # visualize data plt.scatter(x_train, y_train, marker='.') plt.text(2,200,r'Train Data Set(0.7)',fontsize=13) plt.show() plt.scatter(x_test, y_test, marker='.') plt.text(2,200,r'Test Data Set(0.3)',fontsize=13) plt.show() # train the liner regression model regr = LinerRegression(learning_rate=0.01,max_iter=10, seed=314) regr.fit(x_train, y_train) print('cost: \t{:.3}'.format(regr.loss())) print('w: \t{:.3}'.format(regr.w)) print('b: \t{:.3}'.format(regr.b)) show_data(x, y, regr.w, regr.b) # plot the evolution of cost plt.scatter(np.arange(len(regr.loss_arr)),regr.loss_arr, marker='o', c='green') plt.text(6,5000,r'Loss Function',fontsize=13) plt.show()
|