import pandas as pd import numpy as np from mylinearregression import MyLinearRegression as MyLR import matplotlib.pyplot as plt path = "ressources/day01/resources/" data = pd.read_csv(path + "spacecraft_data.csv") X = np.array(data[['Age', 'Thrust_power', 'Terameters']]) Y = np.array(data[['Sell_price']]) my_lreg = MyLR([1.0, 1.0, 1.0, 1.0]) my_lreg.theta = my_lreg.theta.reshape(-1, 1) print(X.shape) print(Y.shape) print(my_lreg.theta.shape) print(my_lreg.mse_(X, Y)) data.plot.scatter("Thrust_power", "Sell_price") x = np.linspace(0, 200, 100) y = my_lreg.theta[0] + (my_lreg.theta[2] * X[2]) # plt.plot(X[2], y, '-r', label='Linear model 1', color = "green") plt.plot(x, my_lreg.theta[0] + (my_lreg.theta[2] * x), '-r', label='Linear model 1', color="red") plt.legend(loc='upper left') plt.grid() plt.show(block=False) # 144044.877...
import pandas as pd import numpy as np #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))
from mylinearregression import MyLinearRegression as MyLR import numpy as np 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)) print(mylr.cost_elem_(X,Y)) print(mylr.cost_(X,Y)) mylr.theta = mylr.fit_(X, Y, alpha = 1.6e-4, n_cycle=100000) print(mylr.predict_(X)) print(mylr.cost_elem_(X,Y)) print(mylr.cost_(X,Y))
#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, )