def main(): data = pd.read_csv("are_blue_pills_magics.csv") Xpill = np.array(data["Micrograms"]).reshape(-1, 1) Yscore = np.array(data["Score"]).reshape(-1, 1) mlr = MyLinearRegression([[1.0], [1.0]]) mlr.fit_(Xpill, Yscore, alpha=0.005, n_cycle=2000) best_theta = mlr.theta print(best_theta) better_y_score = mlr.predict_(Xpill) print(better_y_score) plt.title( "Evolution of the space driving score in function of the quantity of blue pill (in micrograms). \nIn blue the real values and in green the predicted values.", fontsize=8) plt.xlabel("micrograms") plt.ylabel("Score") plt.plot(Xpill, Yscore, 'bo', Xpill, better_y_score, 'g-.', Xpill, better_y_score, "go") plt.show() plt.close() linear_model1 = MyLinearRegression(np.array([[89.0], [-6]])) linear_model2 = MyLinearRegression(np.array([[89.0], [-6]])) Y_model1 = linear_model1.predict_(Xpill) Y_model2 = linear_model2.predict_(Xpill) print(linear_model1.mse_(Y_model1, Yscore)) # 57.60304285714282 print(mean_squared_error(Y_model1, Yscore)) # 57.603042857142825 print(linear_model2.mse_(Y_model2, Yscore)) print("cost = ", linear_model2.cost_(Xpill, Yscore)) # 232.16344285714285 print(mean_squared_error(Y_model2, Yscore)) # # 232.16344285714285 x = [] y = [ ] # On a créé deux listes vides pour contenir les abscisses et les ordonnées move_theta = -14.0 for i in range(1000): # On veut les points de 0 à 100 linear_model1 = MyLinearRegression(np.array([[88.0], [move_theta]])) # for i in range(5): Y_model1 = linear_model1.predict_(Xpill) y.append(float(linear_model1.cost_(Xpill, Yscore))) x.append(float(linear_model1.theta[1])) move_theta += 0.01 # linear_model1.fit_(Xpill, Yscore, alpha = 0.00005, n_cycle=10) plt.plot(x, y) plt.show() plt.close()
def plot_model(x, y, data, theta, alpha=0.001, max_iter=100000): X = np.array(data[["{}".format(x)]]) Y = np.array(data[["{}".format(y)]]) myLR_obj = MyLR(theta, alpha, max_iter) print(myLR_obj.cost_(Y, myLR_obj.predict_(X))) myLR_obj.fit_(X[:, 0].reshape(-1, 1), Y) Y_model = myLR_obj.predict_(X) print(myLR_obj.cost_(Y, Y_model)) plt.plot(X, Y_model, 'gs') plt.plot(X, Y_model, 'g--', label="Spredict(pills)") plt.plot(X, Y, 'bo', label="Strue") plt.grid(True) plt.legend(loc='upper right', bbox_to_anchor=(0.33, 1.15)) plt.ylabel("Space driving score") plt.xlabel("Quantity of blue pill (in micrograms)") plt.show()
def MultiLinearRegression(X, Y, alpha, n_cycle): X_train = np.array(data[X]) Y_train = np.array(data[['Sell_price']]) my_lreg = MyLR([[1.0], [1.0], [1.0], [1.0]]) my_lreg.fit_(X_train,Y_train, alpha, n_cycle) Y_pred = my_lreg.predict_(X_train) print(my_lreg.cost_(X_train, Y_train)) linear_model(X_train, Y_train, Y_pred)
def SingleLinearRegression(X, Y, alpha, n_cycle): X_train = np.array(data[X]).reshape(-1,1) Y_train = np.array(data[Y]).reshape(-1,1) myLR_age = MyLR([[1000.0], [-1.0]]) myLR_age.fit_(X_train, Y_train, alpha, n_cycle) Y_pred = myLR_age.predict_(X_train) print(myLR_age.cost_(X_train, Y_train)) linear_model(X_train, Y_train, Y_pred)
def test_MyLinearRegressing(): X = np.array([[1., 1., 2., 3.], [5., 8., 13., 21.], [34., 55., 89., 144.]]) Y = np.array([[23.], [48.], [218.]]) mylr = MyLR([[1.], [1.], [1.], [1.], [1]], 5e-5, 320000) # Example 0: print(mylr.predict_(X), end="\n\n") # Output: # array([[8.], [48.], [323.]]) # Example 1: print(mylr.cost_elem_(X, Y), end="\n\n") # Output: # array([[37.5], [0.], [1837.5]]) # Example 2: print(mylr.cost_(X, Y), end="\n\n") # Output: # 1875.0 # Example 3: mylr.fit_(X, Y) print(mylr.thetas, end="\n\n") # Output: # array([[18.023..], [3.323..], [-0.711..], [1.605..], [-0.1113..]]) # Example 4: print(mylr.predict_(X), end="\n\n") # Output: # array([[23.499..], [47.385..], [218.079...]]) # Example 5: print(mylr.cost_elem_(X, Y), end="\n\n") # Output: # array([[0.041..], [0.062..], [0.001..]]) # Example 6: print(mylr.cost_(X, Y), end="\n\n")
def reg_linear_grad(x: np.ndarray, y: np.ndarray, theta: np.ndarray, lambda_: float) -> np.ndarray: if (0 in [x.size, y.size, theta.size] or x.shape[0] != y.shape[0] or (x.shape[1] + 1) != theta.shape[0]): return None res = np.zeros(shape=(theta.shape)) m, n = x.shape mylr = MyLR(theta) y_hat = mylr.predict_(x) for i in range(m): y_diff = y_hat[i][0] - y[i][0] res[0][0] += y_diff for j in range(n): res[j + 1][0] += (y_diff * x[i][j]) + (lambda_ * theta[j + 1][0]) / m res = res / m return res
def compare_polynomials(x: np.ndarray, y: np.ndarray, i: int) -> None: theta = [2.5] * (x.shape[1] + 1) alpha = 1e-8 max_iter = int(1e+6) linear_model = MyLR(theta, alpha, max_iter) x_train, x_test, y_train, y_test = data_spliter(x, y, 0.6) linear_model.fit_(x_train, y_train) y_hat = linear_model.predict_(x_test) this_cost = linear_model.cost_(y_test, y_hat) print(i, this_cost) print(linear_model.thetas) plt.bar(i, this_cost, label="$%d_{th} cost: %.3f$" % (i, this_cost))
def univariate_lr(df, feature_name, alpha=5e-5, n_cycle=500000, colors=("g", "lime")): y = np.array(df["Sell_price"]) x_feature = np.array(df[feature_name]) lr = MyLR([1, 1], alpha=alpha, n_cycle=n_cycle) lr.fit_(x_feature, y) print(f"thetas: {lr.thetas}, cost: {lr.cost_(x_feature, y)}") y_hat = lr.predict_(x_feature) # Plot plt.plot(x_feature, y, "o", color=colors[0], label="Sell price") plt.plot(x_feature, y_hat, "o", color=colors[1], label="Predicted sell price") plt.legend(loc="best") plt.xlabel(feature_name) plt.ylabel("Sell price") plt.show()
def multivariate_lr(df): x = np.array(df[["Age", "Thrust_power", "Terameters"]]) y = np.array(df["Sell_price"]) # lr = MyLR([1, 1, 1, 1], 9e-5, 1000000) lr = MyLR([380, -24, 5, -2], 9e-5, 100000) lr.fit_(x, y) print(f"thetas: {lr.thetas}, cost: {lr.cost_(x, y)}") y_hat = lr.predict_(x) # Plot ax1 = plt.subplot(131) x_age = np.array(df["Age"]) ax1.plot(x_age, y, "o", color="b", label="Sell price") ax1.plot(x_age, y_hat, "o", color="dodgerblue", label="Predicted sell price") ax1.set_xlabel("Age") ax1.set_ylabel("Sell price") ax1.legend(loc="best") ax2 = plt.subplot(132) x_thrust = np.array(df["Thrust_power"]) ax2.plot(x_thrust, y, "o", color="g", label="Sell price") ax2.plot(x_thrust, y_hat, "o", color="lime", label="Predicted sell price") ax2.set_xlabel("Thrust_power") ax2.set_ylabel("Sell price") ax2.legend(loc="best") ax3 = plt.subplot(133) x_dist = np.array(df["Terameters"]) ax3.plot(x_dist, y, "o", color="darkviolet", label="Sell price") ax3.plot(x_dist, y_hat, "o", color="violet", label="Predicted sell price") ax3.set_xlabel("Terameters") ax3.set_ylabel("Sell price") ax3.legend(loc="best") # plt.tight_layout() plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1) plt.show()
def vec_reg_linear_grad(x: np.ndarray, y: np.ndarray, theta: np.ndarray, lambda_: float) -> np.ndarray: """Computes the regularized linear gradient of three non-empty numpy.ndarray, without any for-loop. The three arrays must have compatible dimensions. Args: x: has to be a numpy.ndarray, a matrix of dimesion m * n. y: has to be a numpy.ndarray, a vector of dimension m * 1. theta: has to be a numpy.ndarray, a vector of dimension n * 1. lambda_: has to be a float. Returns: A numpy.ndarray, a vector of dimension n * 1, containing the results of the formula for all j. None if y, x, or theta are empty numpy.ndarray. None if y, x or theta does not share compatibles dimensions. Raises: This function should not raise any Exception. """ if (0 in [x.size, y.size, theta.size] or x.shape[0] != y.shape[0] or (x.shape[1] + 1) != theta.shape[0]): return None mylr = MyLR(theta) m = x.shape[0] y_hat = mylr.predict_(x) x = mylr.add_intercept(x) theta_prime = np.concatenate((np.array([[0]]), theta[1:, ...]), axis=0) nabla_j = (x.T.dot(y_hat - y) + lambda_ * theta_prime) / m return nabla_j
sys.path.insert(1, '../ex03') from mylinearregression import MyLinearRegression from csvreader import CsvReader # 1: import data with CsvReader("spacecraft_data.csv", header = True, skip_top = 0, skip_bottom = 0) as csv_file: data = np.array(csv_file.getdata(), float) #Xage = data[:, 0:1] Xage = data[:, 0:3] Yprice = data[:, 3:4] # 2: perform fit #tr = MyLinearRegression([516, -1]) tr = MyLinearRegression([8., -10., 7., -2.]) print(tr.mse_(Xage, Yprice)) print(tr.fit_(Xage, Yprice, 1e-4, 1000)) #print(tr.cost_(Xage, Yprice)) print(tr.mse_(Xage, Yprice)) # 3: print plot plt.plot(Xage[:, 0:1], Yprice, 'bo', label = "Strue") #plt.plot(Xage, tr.predict_(Xage), 'g') plt.plot(Xage[:, 0:1], tr.predict_(Xage), 'go', label ="Spredict") plt.ylabel('sell price') plt.xlabel('age') plt.grid() plt.legend() plt.show()
import numpy as np import matplotlib.pyplot as plt import pandas as pd from sklearn.preprocessing import MinMaxScaler from mylinearregression import MyLinearRegression as MyLR # df = pd.read_csv('./resources/are_blue_pills_magics.csv') df = pd.read_csv('data.csv') Y = df['price'].to_numpy().reshape((len(df['price']), 1)) # print(Y) X = df.drop(['price'], axis=1).values scaler = MinMaxScaler() scaler = scaler.fit(X) X = scaler.transform(X) print(X) model = MyLR(np.zeros((X.shape[1] + 1, 1))) print(Y.shape) print(X.shape) model.fit_(X, Y, 0.1) Y_pred = model.predict_(X) print(model.mse_(X, Y)) plt.plot(X, Y, 'o') plt.plot(X, Y_pred, '+') plt.show() # print(X.describe()) # print(Y)
import seaborn as sns import matplotlib.pyplot as plt from sklearn.metrics import mean_squared_error # Init fl = FL() mypl = MyPL() data = fl.load('../resources/are_blue_pills_magics.csv') ax = sns.scatterplot(x='Micrograms', y='Score', data=data) mylr = MyLR([[60.0], [-1]]) X = np.array(data.loc[:, 'Micrograms'].values) X = X.reshape(X.shape[0], 1) Y = np.array(data.loc[:, 'Score'].values) Y = Y.reshape(Y.shape[0], 1) # Fitting mylr.fit_(X, Y, 0.01, 2000) print(mylr.theta) # Plotting result Y_pred = mylr.predict_(X) data2 = { 'Micrograms': data.loc[:, 'Micrograms'].values, 'Predict': mylr.predict_(X).reshape(X.shape[0]) } ax = sns.lineplot(x='Micrograms', y='Predict', data=data2, ax=ax) #plt.show() print(mean_squared_error(Y_pred, Y)) print(mylr.mse_(Y_pred, Y))
print(myLR_obj.cost_(Y, Y_model)) plt.plot(X, Y_model, 'gs') plt.plot(X, Y_model, 'g--', label="Spredict(pills)") plt.plot(X, Y, 'bo', label="Strue") plt.grid(True) plt.legend(loc='upper right', bbox_to_anchor=(0.33, 1.15)) plt.ylabel("Space driving score") plt.xlabel("Quantity of blue pill (in micrograms)") plt.show() data = pd.read_csv("spacecraft_data.csv") #plot_model("Age", "Sell_price", data, [[1000.0], [-1.0]], alpha = 2.5e-5, max_iter = 10000) #plot_model("Thrust_power", "Sell_price", data, [[0], [10.0]], alpha = 1e-4, max_iter = 10000) #plot_model("Terameters", "Sell_price", data, [[1000.0], [-1.0]], alpha = 1e-4, max_iter = 50000) X = np.array(data[['Age', 'Thrust_power', 'Terameters']]) Y = np.array(data[["Sell_price"]]) myLR_obj = MyLR([1.0, 1.0, 1.0, 1.0], alpha=5e-10, max_iter=10000) print(myLR_obj.cost_(Y, myLR_obj.predict_(X))) myLR_obj.fit_(X, Y) Y_model = myLR_obj.predict_(X) print(myLR_obj.cost_(Y, Y_model)) plt.plot(X[:, 2], Y_model, 'go', label="Spredict(pills)") plt.plot(X[:, 2], Y, 'bo', label="Strue") plt.grid(True) plt.legend(loc='upper right', bbox_to_anchor=(0.33, 1.15)) plt.ylabel("Space driving score") plt.xlabel("Quantity of blue pill (in micrograms)") plt.show()
plt.legend(loc=leg_loc) plt.show() def prepare(data, column_data, x_banner, leg_loc): dataX = data[column_data][1:] dataY = data['Sell_price'][1:] myLR_age = MyLR(np.array([[1.], [1.]]), alpha=2.5e-5) myLR_age.fit_(dataX, dataY) plot_(myLR_age, dataX, dataY, x_banner, leg_loc) data = pd.read_csv("../resources/spacecraft_data.csv") #prepare(data, 'Age', 'x1: age(in years)', 'lower left') #prepare(data, 'Thrust_power', 'x2: thrust power(in 10Km/s)', 'upper left') #prepare(data, 'Terameters', 'x3: distance totalizer value of spacecraft (in Tmeters)', 'upper right') X = np.array(data[['Age','Thrust_power','Terameters']]) Y = np.array(data[['Sell_price']]).reshape((-1, 1)) my_lreg = MyLR(np.array([[300.0], [-10.0], [3.0], [-2.0]]), alpha=2.5e-5, n_cycle=600000) my_lreg.fit_(X, Y) prediction = my_lreg.predict_(X) #real_age = X[:, 0] real_dist = X[:, 2] plt.grid(True) plt.scatter(real_dist, Y, color='b', s=40, label='Sell price') plt.scatter(real_dist, prediction, color='c', s=10, label='Predicted sell price') #plt.xlabel('x1: age(in years)') plt.xlabel('x2: thrust power(in 10Km/s)') plt.ylabel('y: sell price (in keuros)') plt.legend(loc='lower left') plt.show()
import numpy as np from mylinearregression import MyLinearRegression as MyLR x = np.array([[12.4956442], [21.5007972], [31.5527382], [48.9145838], [57.5088733]]) y = np.array([[37.4013816], [36.1473236], [45.7655287], [46.6793434], [59.5585554]]) lr1 = MyLR([2, 0.7]) print(lr1.predict_(x)) # Output: # array([[10.74695094], # [17.05055804], # [24.08691674], # [36.24020866], # [42.25621131]]) # Example 0.1: print(lr1.cost_elem_(lr1.predict_(x), y)) # Output: # array([[77.72116511], # [49.33699664], # [72.38621816], # [37.29223426], # [78.28360514]]) # Example 0.2: print(lr1.cost_(lr1.predict_(x), y)) # Output: # 315.0202193084312 # Example 1.0: lr2 = MyLR([0, 0]) lr2.fit_(x, y)
print(Xage.shape) #tr = MyLinearRegression([516, -1]) tr = MyLinearRegression([0., 0., 0., 0.]) tr2 = MyLinearRegression([8., -10., 7., -2.]) print(tr2.fit_(Xage, Yprice, 1e-4, 1000)) print(tr.normal_equation_(Xage, Yprice)) #print(tr.cost_(Xage, Yprice)) print(tr.mse_(Xage, Yprice)) print(tr2.mse_(Xage, Yprice)) # 3: print plot plt.plot(Xage[:, 0:1], Yprice, 'bo', markersize=4, label="Strue") #plt.plot(Xage, tr.predict_(Xage), 'g') plt.plot(Xage[:, 0:1], tr.predict_(Xage), 'go', markersize=3, label="Spredict_NE") plt.plot(Xage[:, 0:1], tr2.predict_(Xage), 'ro', markersize=3, label="Spredict_LGD") plt.ylabel('sell price') plt.xlabel('age') plt.grid() plt.legend() plt.show()
import pandas as pd import numpy as np import matplotlib.pyplot as plt from mylinearregression import MyLinearRegression as MyLR data = pd.read_csv("../resources/spacecraft_data.csv") X = np.array(data[['Age','Thrust_power','Terameters']]) Y = 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]]) myLR_lgd.fit_(X,Y, alpha = 5e-5, n_cycle = 100000) Y_lgr_model = myLR_lgd.predict_(X) myLR_ne.normalequation_(X,Y) Y_ne_model = myLR_ne.predict_(X) print(myLR_lgd.mse_(Y_lgr_model,Y)) print(myLR_ne.mse_(Y_ne_model,Y)) X1 = np.array(data['Age']).reshape(-1,1) data_plot = plt.plot(X[:,0], Y, 'bo', label='Predicted sell price') ne_plot = plt.plot(X1, Y_lgr_model, '.', color='orange', label='Prediction with LGD') lgd_plot = plt.plot(X1, Y_ne_model, '.', color='springgreen',label='Prediction with NE') plt.xlabel('age (Years)') plt.ylabel('Selling Price (Keuros)') plt.legend(loc=0) plt.grid() plt.show()
def add_polynomial_features(x, power): temp = x.copy() for i in range(2, power + 1): temp = np.append(temp, np.power(x, i), axis=1) return temp x = np.arange(1, 11).reshape(-1, 1) y = np.array([[1.39270298], [3.88237651], [4.37726357], [4.63389049], [7.79814439], [6.41717461], [8.63429886], [8.19939795], [10.37567392], [10.68238222]]) plt.scatter(x, y) plt.show() from polynomial_model import add_polynomial_features from mylinearregression import MyLinearRegression as MyLR # Build the model: x_ = add_polynomial_features(x, 3) my_lr = MyLR(np.ones(4).reshape(-1, 1)) my_lr.fit_(x_, y) ## To get a smooth curve, we need a lot of data points continuous_x = np.arange(1, 10.01, 0.01).reshape(-1, 1) x_ = add_polynomial_features(continuous_x, 3) y_hat = my_lr.predict_(x_) plt.scatter(x, y) # print(my_lr.thetas) plt.plot(continuous_x, y_hat, color='orange') plt.show()
import numpy as np from mylinearregression import MyLinearRegression as MyLR 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')
import numpy as np from mylinearregression import MyLinearRegression as MyLR X = np.array([[1., 1., 2., 3.], [5., 8., 13., 21.], [34., 55., 89., 144.]]) Y = np.array([[23.], [48.], [218.]]) theta = np.array([[1.], [1.], [1.], [1.], [1]]) print("X", X.shape) print("Y", Y.shape) print("theta", theta.shape) # MyLR(). mylr = MyLR(theta) mylr.predict_(X) mylr.cost_elem_(X, Y) mylr.cost_(X, Y) mylr.fit_(X, Y, alpha=1.6e-4, n_cycle=200000) mylr.theta mylr.predict_(X) mylr.cost_elem_(X, Y) mylr.cost_(X, Y)
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)
""" if X.size == 0 or self.theta.size == 0 or \ (X.size != 0 and X.shape[1] + 1 != self.theta.shape[0]): return None # on commence par rajouter une colonne x0 qui vaut 1 X_1 = np.insert(X, 0, [1.], axis=1) return np.dot(X_1, self.theta) if __name__ == '__main__': import numpy as np from mylinearregression import MyLinearRegression as MyLR X = np.array([[1., 1., 2., 3.], [5., 8., 13., 21.], [34., 55., 89., 144.]]) Y = np.array([[23.], [48.], [218.]]) mylr = MyLR([[1.], [1.], [1.], [1.], [1]]) print(mylr.predict_(X)) # => array([[8.], [48.], [323.]]) print(mylr.cost_elem_(X, Y)) # => array([[37.5], [0.], [1837.5]]) print(mylr.cost_(X, Y)) # => 1875.0 mylr.fit_(X, Y, alpha=1.6e-4, n_cycle=200000) print(mylr.theta) # => array([[18.023..], [3.323..], [-0.711..], [1.605..], [-0.1113..]]) print(mylr.predict_(X)) # => array([[23.499..], [47.385..], [218.079...]]) print(mylr.cost_elem_(X, Y)) # => array([[0.041..], [0.062..], [0.001..]]) print(mylr.cost_(X, Y)) # => 0.1056..
print(RMSE_thera) plt.scatter(X3,Y3_new) plt.scatter(X3,Y3) plt.title('Distance et Sell Price: prediction et dataset') plt.xlabel('Distance') plt.ylabel('Sell Price (en keuros)') plt.show() ''' #For Multilinear Regression X = np.array(data[['Age', 'Thrust_power', 'Terameters']]) Y = np.array(data[['Sell_price']]) theta = np.array([[1.0], [-10.], [6.0], [-2.0]]) my_lreg = MyLR(theta) Y_new = my_lreg.predict_(X) print("MSE =") print(my_lreg.mse_(Y, Y_new)) print("New Theta:") print(my_lreg.fit_(X, Y, alpha=2.5e-5, n_cycle=200000)) Y_new = my_lreg.predict_(X) print("MSE2 =") print(my_lreg.mse_(Y, Y_new)) plt.scatter(data['Age'], Y_new) plt.scatter(data['Age'], data['Sell_price']) plt.title('Age et Sell Price: prediction et dataset') plt.xlabel('Age (en annees)') plt.ylabel('Sell Price (en keuros)')
MSE_age = myLR_age.mse_(Xage[:, 0].reshape(-1, 1), Y) print(MSE_age) # exemple 1.b print('exemple 1.b') myLR_thrust = MyLR([[35.0], [4.0]]) myLR_thrust.fit_(Xtp[:, 0].reshape(-1, 1), Y, alpha=1e-4, n_cycle=100000) print(myLR_thrust.thetas) MSE_thrust = myLR_thrust.mse_(Xtp[:, 0].reshape(-1, 1), Y) print(MSE_thrust) # exemple 1.c print('exemple 1.c') myLR_distance = MyLR([[715.0], [-2.0]]) myLR_distance.fit_(Xtm[:, 0].reshape(-1, 1), Y, alpha=2.5e-5, n_cycle=50000) print(myLR_distance.thetas) MSE_distance = myLR_distance.mse_(Xtm[:, 0].reshape(-1, 1), Y) print(MSE_distance) # plots print('plots') plot_model(Xage, Y, myLR_age.predict_(Xage)) plot_model(Xtp, Y, myLR_thrust.predict_(Xtp)) plot_model(Xtm, Y, myLR_distance.predict_(Xtm))
import matplotlib.pyplot as plt import matplotlib.lines as mlines import matplotlib.cm as cm fig, axs = plt.subplots(3) data = pd.read_csv("spacecraft_data.csv") X = np.array(data['Age']).reshape(-1, 1) print(X.shape) Y_price = np.array(data['Sell_price']).reshape(-1, 1) X_thrust = np.array(data['Thrust_power']).reshape(-1, 1) X_meters = np.array(data['Terameters']).reshape(-1, 1) myLR_age = MyLR([[1000.0], [-1.0]]) myLR_age.fit_(X, Y_price, alpha = 2.5e-5, n_cycle = 5000) y_hat = myLR_age.predict_(X) axs[0].scatter(X, Y_price, color = "purple") axs[0].plot(X, y_hat, color = "pink") axs[0].set_title("Sell_price with age") axs[0].set(xlabel = ("Age"), ylabel = ("Sell_price")) myLR_thrust = MyLR([[1000.0], [-1.0]]) y_hat_t = myLR_thrust.fit_(X_thrust, Y_price, alpha = 2.5e-5, n_cycle = 5000) axs[1].scatter(X_thrust, Y_price, color = "purple") axs[1].plot(X_thrust, y_hat_t, color = "pink") axs[1].set_title("Sell_price with thrustpower") axs[1].set(xlabel = ("Thrust_power"), ylabel = ("Sell_price")) #plt.subplot(132) myLR_meters = MyLR([[1000.0], [-1.0]]) y_hat_m = myLR_meters.fit_(X_meters, Y_price, alpha = 2.5e-10, n_cycle = 5000) axs[2].scatter(X_meters, Y_price, color = "purple")
import pandas as pd import numpy as np from sklearn.metrics import mean_squared_error from mylinearregression import MyLinearRegression as MyLR # import matplotlib.font_manager # flist = matplotlib.font_manager.get_fontconfig_fonts() # names = [print(matplotlib.font_manager.FontProperties(fname=fname).get_name()) for fname in flist] # print("fonts:", "\n".join(flist)) data = pd.read_csv("../../ressources/are_blue_pills_magics.csv") Xpill = np.array(data["Micrograms"]).reshape(-1, 1) Yscore = np.array(data["Score"]).reshape(-1, 1) linear_model1 = MyLR(np.array([[89.0], [-8]])) linear_model2 = MyLR(np.array([[89.0], [-6]])) Y_model1 = linear_model1.predict_(Xpill) Y_model2 = linear_model2.predict_(Xpill) print(Xpill) print(linear_model1.cost_(Y_model1, Yscore)) # 57.60304285714282 print(mean_squared_error(Yscore, Y_model1)) # 57.603042857142825 MyLR.plot(Xpill, Yscore, Y_model1) linear_model1.plotcost(Xpill, Yscore) print(linear_model2.cost_(Y_model2, Yscore)) # 232.16344285714285 print(mean_squared_error(Yscore, Y_model2)) # 232.16344285714285 MyLR.plot(Xpill, Yscore, Y_model2)
def main(): data = pd.read_csv("spacecraft_data.csv") X = np.array(data[['Age','Thrust_power','Terameters']]) Y = np.array(data[['Sell_price']]) # myLR_age = MyLR([[700.0], [-30.0]]) # # print(myLR_age.theta) # myLR_age.fit_(X[:,0].reshape(-1,1), Y, alpha = 2.5e-5, n_cycle = 100) # RMSE_age = myLR_age.mse_(myLR_age.predict_(X[:,0].reshape(-1,1)),Y) # # print(myLR_age.theta) # # print(RMSE_age) # plt.title("Evolution of the sell price of spacecrafts with respect to the age of the spacecraft\nand representation of the predicted values of our first model.", fontsize = 8) # plt.ylabel("sell price") # plt.xlabel("age") # # print(X[:,0].reshape(-1,1)) # # print("predict : ",myLR_age.predict_(X[:,0].reshape(-1,1))) # # plt.plot(X[:,0].reshape(-1,1), Y, 'bo', X[:,0].reshape(-1,1),myLR_age.predict_(X[:,0].reshape(-1,1)), 'go') # # plt.show() # # print(X[:,1].reshape(-1,1)) # myLR_thrust = MyLR([[15.3],[4.4]]) # myLR_thrust.fit_(X[:,1].reshape(-1,1), Y, alpha = 2.5e-5, n_cycle = 200) # # print(myLR_thrust.mse_(myLR_thrust.predict_(X[:,0].reshape(-1,1)),Y)) # # print(myLR_thrust.theta) # plt.title("Evolution of the sell price of spacecrafts with respect to the thrust power\nof the spacecraft engines and representation of the predicted values of our second model.", fontsize = 8) # plt.ylabel("sell price") # plt.xlabel("thrust") # # plt.plot(X[:,1].reshape(-1,1), Y, 'bo', X[:,1].reshape(-1,1),myLR_thrust.predict_(X[:,1].reshape(-1,1)), 'go') # # plt.show() # # print(X[:,1].reshape(-1,1)) # myLR_dist = MyLR([[0.0],[1.5]]) # myLR_dist.fit_(X[:,2].reshape(-1,1), Y, alpha = 0.0005, n_cycle = 5) # # print(myLR_thrust.mse_(myLR_thrust.predict_(X[:,0].reshape(-1,1)),Y)) # # print(myLR_thrust.theta) # plt.title("Evolution of the sell price of spacecrafts\nwith respect to the terameters driven and the predicted values of our third model.", fontsize = 8) # plt.ylabel("sell price") # plt.xlabel("distance") # # plt.plot(X[:,2].reshape(-1,1), Y, 'bo',myLR_dist.predict_(X[:,2].reshape(-1,1)),Y ,'go') # # plt.show() my_lreg = MyLR([360.3584, -23.438, 5.7636, -2.6267]) # print(my_lreg.predict_(X)) # print(my_lreg.mse_(Y,my_lreg.predict_(X))) my_lreg.fit_(X,Y, alpha = 1e-4, n_cycle = 60) print(my_lreg.theta) print(my_lreg.mse_(Y,my_lreg.predict_(X))) plt.title("Evolution of the sell prices of spacecrafts and evolution of predicted sell prices\nof spacecrafts with the multi-variables hypothesis, with respect to the age.", fontsize = 8) plt.ylabel("sell price") plt.xlabel("age") plt.plot(X[:,0], Y, 'bo', X[:,0], my_lreg.predict_(X), 'go') plt.show() plt.plot(X[:,1], Y, 'bo', X[:,1], my_lreg.predict_(X), 'go') plt.show() plt.plot(X[:,2], Y, 'bo', X[:,2], my_lreg.predict_(X), 'go') plt.show()
Xage = np.array(data[['Age']]) Xtp = np.array(data[['Thrust_power']]) Xtm = np.array(data[['Terameters']]) X = np.array(data[['Age', 'Thrust_power', 'Terameters']]) Y = np.array(data[['Sell_price']]) # exemple 2.a print('exemple 2.a') my_lreg = MyLR([1.0, 1.0, 1.0, 1.0]) print(my_lreg.mse_(X, Y)) # Output : # 144044.877... my_lreg.fit_(X, Y, alpha=9.5e-5, n_cycle=900000) # best predict print(my_lreg.thetas) # Output : # array([[334.994...],[-22.535...],[5.857...],[-2.586...]]) print(my_lreg.mse_(X, Y)) # Output : # 586.896999... # plots print('plots') plot_model(Xage, Y, my_lreg.predict_(X)) plot_model(Xtp, Y, my_lreg.predict_(X)) plot_model(Xtm, Y, my_lreg.predict_(X))
import pandas as pd import numpy as np import matplotlib.pyplot as plt import sys sys.path.append("../ex07") from mylinearregression import MyLinearRegression as MyLR sys.path.append("../ex10") from polinomial_model import add_polynomial_features data = pd.read_csv("are_blue_pills_magics.csv") X = np.array(data["Micrograms"]).reshape(-1, 1) Y = np.array(data["Score"]).reshape(-1, 1) myLR_obj = MyLR([90.0, -1.0, 1.0, 2.0], alpha=5e-6, max_iter=20000) print(myLR_obj.cost_(Y, add_polynomial_features(X, 3))) myLR_obj.fit_(add_polynomial_features(X, 3), Y) Y_model = myLR_obj.predict_(add_polynomial_features(X, 3)) print(myLR_obj.cost_(Y, Y_model)) plt.plot(X, Y_model, 'go', label="Spredict(pills)") plt.plot(X, Y, 'bo', label="Strue") plt.grid(True) plt.legend(loc='upper right', bbox_to_anchor=(0.33, 1.15)) plt.ylabel("Space driving score") plt.xlabel("Quantity of blue pill (in micrograms)") plt.show()