# coding: utf-8 # linear_regression/test_lwr.py import regression import matplotlib.pyplot as plt import matplotlib.ticker as mtick import numpy as np if __name__ == "__main__": srcX, y = regression.loadDataSet('data/lwr.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 predicateX = regression.standardize(np.matrix([[8, 64]])) predicateX = np.concatenate((np.ones((1, 1)), predicateX), axis=1) result, t = regression.lwr(rate, maxLoop, epsilon, X, y, predicateX, 1) theta, errors, thetas = result result2, t = regression.lwr(rate, maxLoop, epsilon, X, y, predicateX, 0.1) theta2, errors2, thetas2 = result2 # 打印特征点
# coding: utf-8 # linear_regression/test_temperature_polynomial.py import regression import matplotlib.pyplot as plt import matplotlib.ticker as mtick 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])
# linear_regression/test_temperature_normal.py import regression as re from matplotlib import cm from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import matplotlib.ticker as mtick import numpy as np if __name__ == '__main__': srcX, y = re.loadDataSet('data/temperature.txt'); m, n = srcX.shape srcX = np.concatenate((srcX[:, 0], np.power(srcX[:, 0], 2)), axis=1) # 特征缩放 X = re.standardize(srcX.copy()) X = np.concatenate((np.ones((m, 1)), X), axis=1) rate = 0.1 maxLoop = 1000 epsilon = 0.01 result, timeConsumed = re.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[:, 0].flatten().A[0], y[:, 0].flatten().A[0])