import numpy as np if __name__ == "__main__": srcX, y = regression.loadDataSet('temperature.txt') m, n = srcX.shape srcX = np.concatenate((srcX[:, 0], np.power(srcX[:, 0], 2)), axis=1) # 特征缩放 X = regression.standardize(srcX.copy()) X = np.concatenate((np.ones((m, 1)), X), axis=1) rate = 0.1 maxLoop = 1000 epsilon = 0.01 result, timeConsumed = regression.bgd(rate, maxLoop, epsilon, X, y) theta, errors, thetas = result # 打印特征点 fittingFig = plt.figure() title = 'polynomial with bgd: rate=%.2f, maxLoop=%d, epsilon=%.3f \n time: %ds' % ( rate, maxLoop, epsilon, timeConsumed) ax = fittingFig.add_subplot(111, title=title) trainingSet = ax.scatter(srcX[:, 1].flatten().A[0], y[:, 0].flatten().A[0]) print(theta) # 打印拟合曲线 xx = np.linspace(50, 100, 50) xx2 = np.power(xx, 2) yHat = []
# Author: Shibo Tang import regression as re import numpy as np # Linear regression print('Regression:') testData = re.read_csv("regression/test.csv") trainData = re.read_csv("regression/train.csv") # Batch gradient descent algorithm BGD1 = re.bgd(0.25, trainData['x'], trainData['y']) costTest = re.cost(BGD1['weight'], testData['x'], testData['y']) print('Batch gradient descent algorithm:') print('Final cost function value of the training data:', BGD1['cost'][-1]) print('Final cost function value of the test data:', costTest) print('r:', BGD1['r']) print('Final weight:', BGD1['weight']) # Stochastic gradient descent (SGD) algorithm SGD1 = re.sgd(0.25, trainData['x'], trainData['y']) costTest = re.cost(SGD1['weight'], testData['x'], testData['y']) print('Stochastic gradient descent algorithm:') print('Final cost function value of the training data:', SGD1['cost'][-1]) print('Final cost function value of the test data:', costTest) print('r:', SGD1['r']) print('Final weight:', SGD1['weight']) # optimal weight vector with analytical form w = re.analytical(trainData['x'], trainData['y']) cost_train = re.cost(w, trainData['x'], trainData['y']) cost_test = re.cost(w, testData['x'], testData['y'])