def test_gradient_vs_octave(self): """ compare gradient results to those given by octave to 10 decimal places try combos of long decimal places to seek out rounding differences with numpy try different amounts of regularisation to ensure that works """ Data = np.loadtxt('./test/fixtures/data/sample.data') y = np.matrix(Data[:, 0]).T m = y.shape[0] X = np.matrix(Data[:, 1]).T X = gd.add_y_intercept(X) theta = np.matrix('1; 1') self.assertEqual( np.round(gd.gradient(X, y, theta, 0), 10).tolist()[0], [-15.3030156742, 598.1674108394]) self.assertEqual( np.round(gd.gradient(X, y, theta, 1), 10).tolist()[0], [-15.3030156742, 598.2507441727]) theta = np.matrix('2.123123;2.123123') self.assertEqual( np.round(gd.gradient(X, y, theta, .3), 10).tolist()[0], [-19.8914519714, 1545.9334731365]) theta = np.matrix('2.123123;0.123456789') self.assertEqual( np.round(gd.gradient(X, y, theta, .9), 10).tolist()[0], [-9.7222967331, -151.6366579476])
def update_window(): for i in range(len(currency_data)): data_points = currency_data[i][2] update_data_points(data_points, currency_data[i][0] + '/EUR') exchange = None if len(data_points) > 0: exchange = data_points[-1][1] grad, error = None, None if len(data_points) >= 2: grad = gradient(data_points) error_exp = square_error(data_points, grad=grad) / len(data_points) if error_exp != 0: error = log10(error_exp) labels = currency_data[i][3] if len(data_points) > 0: labels[0].config(text=str(round(exchange, 3))+' €') if grad != None: labels[1].config( text=str(round(grad * 1000 * 60, 4)) + ' €/min', fg='#%02x%02x%02x' % gradient_color(grad, exchange)) if error != None: labels[2].config(text=str(round(error, 2))) window.after(1000, update_window)
def test_linreg_multi_compare(): data = pd.read_csv('data/Advertising.csv', index_col=0) x1 = data.values[:,0] x2 = data.values[:,1] x3 = data.values[:,2] y = data.values[:,3] y = y.reshape(y.size,1) x1 = x1.reshape(x1.size,1) x1u, x1s, x1 = util.normalize(x1) x2 = x2.reshape(x2.size,1) x2u, x2s, x2 = util.normalize(x2) x3 = x3.reshape(x3.size,1) x3u, x3s, x3 = util.normalize(x3) x0 = np.ones((x1.size,1)) x = np.hstack([x0,x1,x2,x3]) theta = np.zeros((4,1)) iterations = 1500 alpha = 0.01 theta, j_history = linreg.gradient(theta, x, y, alpha, iterations) print theta #plt.plot(np.arange(iterations), J_history) #plt.xlabel('Iterations') #plt.ylabel('Cost Function') #plt.show() print '$100k/$25k/$25k TV/Radio/Paper = %f widgets' % (np.array([1,(100-x1u)/x1s,(25-x2u)/x2s,(25-x3u)/x3s]).dot(theta).flatten()*1000)
def test_linreg_multi_var(): data = pd.read_csv('data/ex1data2.txt', header=None) n = data[0].size x0 = np.ones((n, 1)) x1 = data[0] x1 = x1.reshape(n, 1) x1u, x1s, x1 = util.normalize(x1) x2 = data[1] x2 = x2.reshape(n, 1) x2u, x2s, x2 = util.normalize(x2) x = np.hstack([x0, x1, x2]) y = data[2] y = y.reshape(n, 1) theta = np.zeros((3, 1)) iterations = 10000 alpha = 0.001 theta, j_history = linreg.gradient(theta, x, y, alpha, iterations) print theta ''' plt.plot(np.arange(iterations), j_history) plt.xlabel('Iterations') plt.ylabel('Cost Function') plt.show()''' print '2000sqft + 3bedrooms = %f' % np.array([1,(2000-x1u)/x1s,(3-x2u)/x2s]).dot(theta).flatten() print '4000sqft + 5bedrooms = %f' % np.array([1,(4000-x1u)/x1s,(5-x2u)/x2s]).dot(theta).flatten() print '4000sqft + 6bedrooms = %f' % np.array([1,(4000-x1u)/x1s,(6-x2u)/x2s]).dot(theta).flatten() print '2000sqft + 6bedrooms = %f' % np.array([1,(2000-x1u)/x1s,(6-x2u)/x2s]).dot(theta).flatten()
def diagnoseLR(): """线性回归诊断 """ initTheta = np.mat(np.ones((X.shape[1], 1))) result, timeConsumed = linear_regression.gradient( X, y, rate=0.001, maxLoop=5000, epsilon=0.1, initTheta=initTheta) theta, errors = result # 绘制拟合成果 Xmin = X[:, 1].min() Xmax = X[:, 1].max() ymax = y[:, 0].max() ymin = y[:, 0].min() fitX = np.mat(np.linspace(Xmin, Xmax, 20).reshape(-1, 1)) fitX = np.concatenate((np.ones((fitX.shape[0], 1)), fitX), axis=1) h = fitX * theta plt.xlim(Xmin, Xmax) plt.ylim(ymin, ymax) # 绘制训练样本 plt.scatter(X[:, 1].flatten().A[0], y[:, 0].flatten().A[0],marker='x',color='r', linewidth=2) # 绘制拟合曲线 plt.plot(fitX[:, 1], h, color='b') plt.xlabel('Change in water level(x)') plt.ylabel('Water flowing out of the dam(y)') plt.show() # 绘制随样本规模学习曲线 m, n = X.shape trainErrors = np.zeros((1,m)) valErrors = np.zeros((1,m)) for i in range(m): Xtrain = X[0:i+1] ytrain = y[0:i+1] res, timeConsumed = linear_regression.gradient( Xtrain, ytrain, rate=0.001, maxLoop=5000, epsilon=0.1) theta, errors = res trainErrors[0,i] = errors[-1] valErrors[0,i] = linear_regression.J(theta, Xval, yval) plt.plot(np.arange(1,m+1).ravel(), trainErrors.ravel(), color='b', label='Training Error') plt.plot(np.arange(1,m+1).ravel(), valErrors.ravel(), color='g', label='Validation Error') plt.title('Learning curve for linear regression') plt.xlabel('Number of training examples') plt.ylabel('Error') plt.legend() plt.show()
def diagnosePR(): """多项式回归诊断 """ # 多项式回归 poly = PolynomialFeatures(degree=8) XX, XXval, XXtest = [linear_regression.normalize( np.mat(poly.fit_transform(data[:, 1:]))) for data in [X, Xval, Xtest]] initTheta = np.mat(np.ones((XX.shape[1], 1))) theLambdas = [1.0, 0.001, 0.003, 0.01, 0.003, 0.1, 0.3, 1.0, 3.0, 10.0] numTheLambdas = len(theLambdas) trainErrors = np.zeros((1, numTheLambdas)) valErrors = np.zeros((1, numTheLambdas)) thetas = [] for idx, theLambda in enumerate(theLambdas): res, timeConsumed = linear_regression.gradient( XX, y, rate=0.3, maxLoop=500, epsilon=0.01, theLambda=theLambda, initTheta=initTheta) theta, errors = res thetas.append(theta) trainErrors[0, idx] = errors[-1] valErrors[0, idx] = linear_regression.J( theta, XXval, yval, theLambda=theLambda) bestLambda = theLambdas[np.argmin(valErrors)] theta = thetas[np.argmin(valErrors)] error = np.min(valErrors) # # 绘制随样本规模学习曲线 plt.plot(np.arange(1, numTheLambdas + 1).ravel(), trainErrors.ravel(), color='b', label='Training Error') plt.plot(np.arange(1, numTheLambdas + 1).ravel(), valErrors.ravel(), color='g', label='Validation Error') plt.title('Learning curve for polynomial regression') plt.xlabel('lambda') plt.ylabel('Error') plt.legend() plt.show() # 绘制拟合曲线 fitX = np.mat(np.linspace(-60, 45).reshape(-1, 1)) fitX = np.concatenate((np.ones((fitX.shape[0], 1)), fitX), axis=1) fitXX = linear_regression.normalize(np.mat(poly.fit_transform(fitX[:, 1:]))) h = fitXX * theta plt.title('Polynomial regression learning curve(lambda=%.3f) \n validation error=%.3f' % (bestLambda, error)) plt.scatter(X[:, 1].ravel(), y[:, 0].flatten().A[0], marker='x', color='r', linewidth=3) plt.plot(fitX[:, 1], h, color='b') plt.show()
def test_linreg_single_var(): data = pd.read_csv('data/ex1data1.txt', header=None) n = data[0].size x0 = np.ones((n, 1)) x1 = data[0] x1 = x1.reshape(n, 1) x = np.hstack([x0, x1]) y = data[1] y = y.reshape(n, 1) theta = np.zeros((2, 1)) iterations = 1500 alpha = 0.01 theta, j_history = linreg.gradient(theta, x, y, alpha, iterations) print theta print '35,000 people = %f' % (np.array([1,3.5]).dot(theta).flatten() * 10000) print '70,000 people = %f' % (np.array([1,7]).dot(theta).flatten() * 10000)
def test_linreg_compare(): data = pd.read_csv('data/Advertising.csv', index_col=0) x1 = data.values[:,0] y = data.values[:,3] y = y.reshape(y.size,1) x1 = x1.reshape(x1.size,1) x1u, x1s, x1 = util.normalize(x1) x0 = np.ones((x1.size,1)) x = np.hstack([x0,x1]) theta = np.zeros((2,1)) iterations = 1500 alpha = 0.01 theta, j_history = linreg.gradient(theta, x, y, alpha, iterations) print theta #plt.plot(np.arange(iterations), J_history) #plt.xlabel('Iterations') #plt.ylabel('Cost Function') #plt.show() print '$50000 TV dollars = %f widgets' % (np.array([1,(50-x1u)/x1s]).dot(theta).flatten()*1000)
def gradient_descent(x, y, theta, alpha, lambda1, iterations): m = np.size(x, 0) for i in range(iterations): theta = theta - alpha * gradient(x, y, theta, lambda1) return theta