def main(): data = pd.read_csv("./data/data.csv") x = np.array(data["km"]).reshape(-1, 1) y = np.array(data["price"]).reshape(-1, 1) lr = MyLR(np.zeros(x.shape[1] + 1), alpha=1e-3, max_iter=10000) # Standardisation lr.setup_zscore(x) # Before training print("Starting cost:", lr.cost_(x, y), end="\n\n") plot_all(x, y, lr) # Training model cost_history = lr.fit_(x, y, get_cost=True) # Plotting cost evolution plt.plot(cost_history, "m-") plt.show() # After training print("Ending cost:", lr.cost_(x, y)) print("Thetas: ", lr.thetas, end="\n\n") plot_all(x, y, lr) # Save config in a file params = lr.get_params_() with open("config.mlr", "w") as f: f.write(repr(params)) print("-> config.mlr created")
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 get_poly_cost(x, y, poly, alpha=0.001, n_cycle=1000, thetas=None): if thetas is None: thetas = [1] * (poly + 1) lr = MyLR(thetas, alpha=alpha, n_cycle=n_cycle) poly_x = add_polynomial_features(x, poly) lr.fit_(poly_x, y) cost = lr.cost_(poly_x, y) print(f"Poly {poly}: {cost} | {repr(lr.thetas)}") return cost
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 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 draw_cost_function(mylr): fig, ax = plt.subplots() #renvoie une figure et des axes t0 = mylr.theta[0] thetas_0 = [t0 - 20, t0 - 10, t0, t0 + 10, t0 + 20] for theta_0 in thetas_0: theta = np.linspace(-14, 4, 100).reshape(100, 1) theta = np.insert(theta, 0, theta_0, axis=1) y = np.array([]) for i in range(theta.shape[0]): tmp_lr = MyLR(theta[i].reshape(2, 1), mylr.X, mylr.Y) dot = tmp_lr.cost_() y = np.append(y, dot) plt.plot(theta[:, 1], y) plt.xlabel("Theta1") plt.ylabel("Cost function J(Theta0, Theta1") plt.title( "Evolution of the cost function J in fuction of Theta0 for different values of Theta1" ) plt.show() plt.cla()
# 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(lr2.thetas) # Output: # array([[1.40709365], # [1.1150909 ]]) # Example 1.1: print(lr2.predict_(x)) # Output: # array([[15.3408728 ], # [25.38243697], # [36.59126492],
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]]) res = mylr.predict_(X) print(res) res = mylr.cost_elem_(X, Y) print(res) res = mylr.cost_(X, Y) print(res) res = mylr.fit_(X, Y, alpha=1.6e-4, n_cycle=200000) print(res) res = mylr.predict_(X) print(res) res = mylr.cost_elem_(X, Y) print(res) res = mylr.cost_(X, Y) print(res)
var = temp std = math.sqrt(var / (len(x) - 1)) return (x - mu) / std csv_data = pd.read_csv("../resources/are_blue_pills_magics.csv") y_n = [] x = np.array(csv_data["Micrograms"]).reshape(-1, 1) x = minmax(x) y = np.array(csv_data["Score"]).reshape(-1, 1) y = minmax(y) plt.scatter(x, y) x9 = add_polynomial_features(x, 9) # mylr4 = MyLR([[10.0],[-21.0 ], [-0.28], [4.63], [6.73]],alpha=5e-3) mylr9 = MyLR( [[0.99549772], [-3.04228406], [11.0342294], [-12.5192794], [-7.56251887], [4.59267205], [9.57475922], [5.99224473], [-1.55560663], [-7.52630899]], alpha=0.55) mylr9.fit_(x9, y) print(mylr9.cost_(x9, y)) continuous_x = np.arange(0, 1, 0.001).reshape(-1, 1) x_9 = add_polynomial_features(continuous_x, 9) y_hat = mylr9.predict_(x_9) print(mylr9.thetas) # print(x_9) # print(y) plt.plot(continuous_x, y_hat, color='orange') plt.show()
plt.show() print(linear_model.mse_(Y_model_age, Y)) X = np.array(data[['Age', 'Thrust_power', 'Terameters']]) my_lreg = MyLR([[1000.0], [0.0], [1000.0], [1.0]]) my_lreg.fit_(X, Y, alpha=0.00005, n_cycle=125) Y_multi_model = my_lreg.predict_(X) plt.subplot(131) data_plot = plt.plot(X1, Y, 'o', color='darkblue', label='Sell price') plt.plot(X1, Y_multi_model, 'c.') plt.grid() plt.subplot(132) data_plot = plt.plot(X2, Y, 'go', label='Sell price') plt.plot(X2, Y_multi_model, '.', color='lime') plt.grid() plt.subplot(133) data_plot = plt.plot(X3, Y, 'mo', label='Sell price') plt.plot(X3, Y_multi_model, '.', color='violet') plt.grid() plt.show() print(my_lreg.cost_(Y_multi_model, 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()
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()
return x data = pd.read_csv("../resources/spacecraft_data.csv") X = np.array(data[['Age','Thrust_power','Terameters']]) Y = np.array(data[['Sell_price']]) reg = MLR([1.0, 1.0, 1.0, 1.0]) # power = 1 # reg.fit_(X,Y, alpha = 6.5e-5, n_cycle = 1000000) # print("cost ", power, " : ", reg.cost_(X, Y), "\n") power = 2 reg.thetas = np.full(3 ** power + 1, 1.0) P = add_polynomial_features(X, power) reg.fit_(P, Y, alpha = 1e-9, n_cycle = 5000) print("cost ", power, " : ", reg.cost_(P, Y), "\n") # power = 3 # reg.thetas = np.full(4 * power + 1, 1.0) # P = add_polynomial_features(X, power) # reg.fit_(P, Y, alpha = 3e-13, n_cycle = 100000) # print("cost ", power, " : ", reg.cost_(reg.predict_(P), Y), "\n") # power = 4 # reg.thetas = np.full(4 * power, 1) # P = add_polynomial_features(X, power) # reg.fit_(P, Y, alpha = 1e-17, n_cycle = 1000000) # print("cost ", power, " : ", reg.cost_(reg.predict_(P), Y), "\n") # power = 5 # reg.thetas = np.full(power * 3 + 1, 1.0)
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)
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..
#cost function linear4 = MyLR(np.array([[89.0], [-14]])) theta0 = 89.0 fig = plt.figure() axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) plt.title("Cost for different theta0") plt.xlabel("theta1") plt.ylabel("cost (j(theta0, theta1))") #plt.axis([-14, 20, -3, 140]) n_samples = 6 colors = cm.rainbow(np.linspace(0, 1, n_samples)) for j in range(0, n_samples): costs = [] for theta1 in range(-13, -2): #every curve costs.append(linear4.cost_(Xpill, Yscore)) linear4.theta = np.array([[theta0], [theta1]]) theta0 += 1 print(costs) plt.plot(range(-13, -2), costs, color=colors[j]) # print("------") #plt.margins(0) axes.set_xlim([-14, -4]) axes.set_ylim([20, 140]) plt.show() #for i in range(-4, -14): # plt.scatter(i, )
#from sklearn.metrics import mean_squared_error from mylinearregression import MyLinearRegression as MyLR from linear_model import linear_model, cost_model data = pd.read_csv('../resources/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([[0.0], [0.0]])) linear_model1.theta = linear_model1.fit_(Xpill, Yscore, alpha=0.05, n_cycle=1000) Y_model1 = linear_model1.predict_(Xpill) cost_elem = linear_model1.cost_(Xpill, Yscore) #cost_model(linear_model1.theta, cost_elem) linear_model(Xpill, Y_model1, Yscore) print(linear_model1.theta) #print(linear_model1.mse_(Yscore, Y_model1)) #print(mean_squared_error(Yscore, Y_model1)) #print(linear_model2.mse_(Yscore, Y_model2)) #print(mean_squared_error(Yscore, Y_model2))
mylr8 = MyLinearRegression([[88.85], [-9.0], [0], [0], [0], [0], [0], [0], [0]]) mylr9 = MyLinearRegression([[88.85], [-9.0], [0], [0], [0], [0], [0], [0], [0], [0]]) mylr2.fit_(x2, y_train) mylr2.alpha = 0.00001 mylr2.fit_(x2, y_train) mylr2.alpha = 0.00003 mylr2.fit_(x2, y_train) mylr2.alpha = 0.0001 mylr2.fit_(x2, y_train) mylr2.alpha = 0.0003 mylr2.fit_(x2, y_train) y_n.append(mylr2.cost_(x2_test, y_test)) mylr3.fit_(x3, y_train) mylr3.alpha = 0.00001 mylr3.fit_(x3, y_train) mylr3.alpha = 0.00003 mylr3.fit_(x3, y_train) mylr3.alpha = 0.0001 mylr3.fit_(x3, y_train) mylr3.alpha = 0.0003 mylr3.fit_(x3, y_train) mylr3.alpha = 0.001 mylr3.fit_(x3, y_train) y_n.append(mylr3.cost_(x3_test, y_test)) mylr4.fit_(x4, y_train)