def dataPredictions(X_train, X_test, y_train, y_test): print("training and testing machine learning model") print("Shape of training_set - X_train: " + str(X_train.shape) + ", y_train: " + str(y_train.shape)) print("Shape of testing_set - X_test: " + str(X_test.shape) + ", y_test: " + str(y_test.shape)) print("\nLinear regression model ") print( "Training without longitude and latitude (no polynomial features) and predicting median_house_value" ) theta_std, X_train, cost_std = lr.batch_regressionModel( 400, X_train[:, 2:], y_train[:, :], 0.01) print("Testing with the Training set") print("Test Values:") print(X_test[:5, :]) print("Comparing the Results (Original - Prediction)") print(X_test.shape[1]) test_one = np.ones((X_test.shape[0], X_test.shape[1] - 1)) test_one[:, 1:] = X_test[:, 2:] print( str(y_test[:5, :]) + " <===> " + str(lr.hypothesis(theta_std, test_one[:5, :]))) fig1, axs1 = plt.subplots(1, 1) fig1.canvas.set_window_title('CaliforniaHousingDataPredictions') fig1.suptitle("CaliforniaHousing Dataset - prediction cost") axs1.plot(cost_std)
def testRegNormalEquations(): df = pd.read_csv("testData/ex1data2.txt", delimiter=',', header=None) X = df.values theta, X = lr.regularized_normalEquation(X[:, :2], X[:, 2:3], 1) print("<-------------------->") print("Testing Regularized Normal Equations on Dataset 1.2") print("<-------------------->") print("Theta:") print(theta) print("<-------------------->") print( "Estimate the price of a 1650 sq-ft, 3 br house using normal equations" ) estimate = np.matrix([1, 1650, 3]) print("Predicted price of a 1650 sq-ft, 3 br house " + str(lr.hypothesis(theta, estimate))) print("<-------------------->") print()
def testRegularizedLinearRegression(): print("\n\n Testing Regularized Linear Regression:") df = pd.read_csv("testData/ex1data2.txt", delimiter=',', header=None) X_clean = df.copy().values df, mu, sigma = featureScaling(df) X = df.values y = X[:, 2:3] theta, X, cost = lr.regularized_linearRegression( 400, 0.01, 1, X[:, :2], X[:, 2:3]) print("<-------------------->") print("Reg. linear Regression using Dataset 1.2 and lambda = 1") print( "Estimate the price of a 1650 sq-ft, 3 br house using gradient descent" ) estimate = np.matrix( [1, (1650 - mu[0]) / sigma[0], (3 - mu[1]) / sigma[1]]) print("Predicted price of a 1650 sq-ft, 3 br house " + str( removeFeatureScaling(lr.hypothesis(theta, estimate), mu[2], sigma[2]))) print("")
def testNormalEquations(): df = pd.read_csv("testData/ex1data2.txt", delimiter=',', header=None) X = df.values theta, cost = lr.normalEquations_regressionModel(X[:, :2], X[:, 2:3]) print("<-------------------->") print("Testing Normal Equations on Dataset 1.2") print("<-------------------->") print("Theta:") print(theta) print("<-------------------->") print("Cost:") print(float(cost[0])) print("<-------------------->") print( "Estimate the price of a 1650 sq-ft, 3 br house using normal equations" ) estimate = np.matrix([1, 1650, 3]) print("Predicted price of a 1650 sq-ft, 3 br house " + str(lr.hypothesis(theta, estimate))) print("<-------------------->") print()
def testLinearRegression(): print("\n\n Testing Linear Regression:") df = pd.read_csv("testData/ex1data2.txt", delimiter=',', header=None) X_clean = df.copy().values print(df.head()) print(df.describe()) df, mu, sigma = featureScaling(df) X = df.values y = X[:, 2:3] theta, X, cost = lr.batch_regressionModel(400, X[:, :2], X[:, 2:3], 0.01) plotLinearRegression( X_clean, theta, cost, removeFeatureScaling(np.min(lr.hypothesis(theta, X)), mu[2], sigma[2]), removeFeatureScaling(np.max(lr.hypothesis(theta, X)), mu[2], sigma[2]), removeFeatureScaling(np.min(X[:, 1]), mu[0], sigma[0]), removeFeatureScaling(np.max(X[:, 1]), mu[0], sigma[0])) print("<-------------------->") print("Final theta") print(theta) print("<-------------------->") print("Testing for training Example 7,8,9,10") print( str(X[6, 0]) + " | " + str(removeFeatureScaling(X[6, 1], mu[0], sigma[0])) + " | " + str(removeFeatureScaling(X[6, 2], mu[1], sigma[1]))) print( str(X[7, 0]) + " | " + str(removeFeatureScaling(X[7, 1], mu[0], sigma[0])) + " | " + str(removeFeatureScaling(X[7, 2], mu[1], sigma[1]))) print( str(X[8, 0]) + " | " + str(removeFeatureScaling(X[8, 1], mu[0], sigma[0])) + " | " + str(removeFeatureScaling(X[8, 2], mu[1], sigma[1]))) print( str(X[9, 0]) + " | " + str(removeFeatureScaling(X[9, 1], mu[0], sigma[0])) + " | " + str(removeFeatureScaling(X[9, 2], mu[1], sigma[1]))) print("theta*the Training Examples") solution_seven = lr.hypothesis(theta, X[6, 0:3]) solution_eight = lr.hypothesis(theta, X[7, 0:3]) solution_nine = lr.hypothesis(theta, X[8, 0:3]) solution_ten = lr.hypothesis(theta, X[9, 0:3]) print("Remove feature Scaling") print("<> " + str(removeFeatureScaling(y[6], mu[2], sigma[2])) + " - " + str(removeFeatureScaling(solution_seven, mu[2], sigma[2])) + " <>") print("<> " + str(removeFeatureScaling(y[7], mu[2], sigma[2])) + " - " + str(removeFeatureScaling(solution_eight, mu[2], sigma[2])) + " <>") print("<> " + str(removeFeatureScaling(y[8], mu[2], sigma[2])) + " - " + str(removeFeatureScaling(solution_nine, mu[2], sigma[2])) + " <>") print("<> " + str(removeFeatureScaling(y[9], mu[2], sigma[2])) + " - " + str(removeFeatureScaling(solution_ten, mu[2], sigma[2])) + " <>") print("<-------------------->") print( "Estimate the price of a 1650 sq-ft, 3 br house using gradient descent" ) estimate = np.matrix( [1, (1650 - mu[0]) / sigma[0], (3 - mu[1]) / sigma[1]]) print("Predicted price of a 1650 sq-ft, 3 br house " + str( removeFeatureScaling(lr.hypothesis(theta, estimate), mu[2], sigma[2]))) print("<-------------------->") print("Plotting cost")