Esempio n. 1
0
import matplotlib.pyplot as plt
import pandas as pd
from mylinearregression import MyLR

data = pd.read_csv("./resources/spacecraft_data.csv")
X = np.array(data[["Age", "Thrust_power", "Terameters"]])
Y = np.array(data[["Sell_price"]])
theta = np.array([[1.], [1.], [1.], [1.]])
mylr_ne = MyLR(theta)
mylr_lgd = MyLR(theta)

Y_pred = mylr_ne.predict_(X)

############### Gradient descente ############
print("Basic cost = " + str(mylr_lgd.mse_(X, Y)))
mylr_lgd.fit_(X, Y, alpha=5e-5, n_cycle=10000)
print("Cost after gradient descente = " + str(mylr_lgd.mse_(X, Y)))
print("Theta after gradient descente = " + str(mylr_lgd.theta))
Y_grad = mylr_lgd.predict_(X)
##############################################

#print()

############# Normale Equation ###############
mylr_ne.normalequation_(X, Y)
print("Cost after Normale equation = " + str(mylr_ne.mse_(X, Y)))
print("Theta after normale equation = " + str(mylr_ne.theta))
Y_ne = mylr_ne.predict_(X)
##############################################

plt.plot(X.T[0], Y, 'co:')
plt.plot(Xthrust, pred_thrust, 'r.')
plt.grid('True')

plt.subplot(233)
plt.ylabel('y : sell price')
plt.xlabel('x3 : Terameters')
plt.plot(Xtera, Yprice, 'yo')
pred_tera = mylr_tera.predict_(Xtera)
plt.plot(Xtera, pred_tera, 'k.')
plt.grid('True')

plt.subplot(234)
plt.ylabel('y : sell price')
plt.xlabel('x1 : age')
plt.plot(Xage, Yprice, 'bo')
mylr_age.fit_(Xage, Yprice, alpha=2.5e-5, n_cycle=1000)
pred_age = mylr_age.predict_(Xage)
plt.plot(Xage, pred_age, 'm.')
plt.grid('True')

plt.subplot(235)
plt.ylabel('y : sell price')
plt.xlabel('x2 : Thrust Power')
plt.plot(Xthrust, Yprice, 'go')
mylr_thrust.fit_(Xthrust, Yprice, alpha=2.5e-5, n_cycle=1000)
pred_thrust = mylr_thrust.predict_(Xthrust)
plt.plot(Xthrust, pred_thrust, 'r.')
plt.grid('True')

plt.subplot(236)
plt.ylabel('y : sell price')