import pandas as pd import numpy as np from normal_equation_model import graph_model from mylinearregression import MyLinearRegression as MyLR data = pd.read_csv("../resources/spacecraft_data.csv") X_train = np.array(data[['Age', 'Thrust_power', 'Terameters']]) Y_train = np.array(data['Sell_price']).reshape(-1, 1) myLR_ne = MyLR([[1.0], [1.0], [1.0], [1.0]]) myLR_lgd = MyLR([[1.0], [1.0], [1.0], [1.0]]) #Linear Gradient Descent myLR_lgd.fit_(X_train, Y_train, alpha=5e-5, n_cycle=10000) Y_pred_lgd = myLR_lgd.predict_(X_train) print('COST Linear gradient descent:', myLR_lgd.cost_(X_train, Y_train)) #Normal Equation myLR_ne.normalequation_(X_train, Y_train) Y_pred_ne = myLR_ne.predict_(X_train) print('COST Normal equation:', myLR_ne.cost_(X_train, Y_train)) graph_model(X_train, Y_train, Y_pred_lgd, Y_pred_ne)
import matplotlib.pyplot as plt data = pd.read_csv("../resources/spacecraft_data.csv") X = np.array(data[['Age', 'Thrust_power', 'Terameters']]) X1 = np.array(data[['Age', 'Thrust_power', 'Terameters']]) Y = np.array(data['Sell_price']).reshape(-1, 1) theta1 = np.array([[1.], [1.], [1.], [1.]]) theta2 = np.array([[1.], [1.], [1.], [1.]]) myLR_ne = MyLR(theta1) myLR_lgd = MyLR(theta2) myLR_lgd.fit_(X, Y, alpha=5e-5, n_cycle=2000) Y_new1 = myLR_lgd.predict_(X) myLR_ne.normalequation_(X, Y) print(myLR_ne.theta) Y_new2 = myLR_ne.predict_(X) ''' print("MSE = ") print(myLR_lgd.theta) print(myLR_lgd.mse_(Y, Y_new1)) print("MSE = ") print(myLR_ne.theta) print(myLR_ne.mse_(Y, Y_new2)) ''' plt.scatter(data.Age, Y_new1, color='green') plt.scatter(data.Age, Y_new2, color='red') plt.scatter(data.Age, Y, color='blue') plt.title('Linear Regression vs. Normal Equation Comparaison') plt.xlabel('Age en annee')
def draw_multi_regression(mylr): #mylr.fit_() mylr.predict_() # Plot in function of age fig, ax = plt.subplots() ax.scatter(mylr.X[:, 0], mylr.Y) ax.scatter(mylr.X[:, 0], mylr.Y_hat, c="blue") plt.xlabel("Age") plt.ylabel("Sell_price") plt.title("") fig.legend(loc="lower left") plt.show() plt.cla() data = pd.read_csv("../resources/spacecraft_data.csv") Y = np.array(data['Sell_price']).reshape(-1, 1) X = np.array(data[['Age', 'Thrust_power', 'Terameters']]) myLR_ne = MyLR([1., 1., 1., 1.], X, Y) myLR_lgd = MyLR([1., 1., 1., 1.], X, Y) myLR_lgd.fit_(alpha=5e-5, n_cycle=10000) myLR_ne.normalequation_() print(myLR_lgd.mse_()) print(myLR_ne.mse_()) draw_multi_regression(myLR_ne)