예제 #1
0
# **************************************************************************** #

import numpy as np
import sys
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))
예제 #2
0
# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    so_much_hyp.py                                     :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: ythomas <*****@*****.**>                     +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/02/12 13:18:07 by ythomas           #+#    #+#              #
#    Updated: 2020/02/12 15:10:23 by ythomas          ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

import numpy as np
import matplotlib.pyplot as plt
from mylinearregression import MyLR
import pandas as pd

data = pd.read_csv("./resources/saturn_asteroids.csv")
X = np.array(data[['x1', 'x2']])
X2 = np.array(data[['x1**2', 'x2**2']])
Y = np.array(data[['y']])
hypo1 = MyLR([1., 1.])
hypo2 = MyLR([1., 1.])
print(hypo1.predict_(X))
print(hypo2.predict_(X2))
#hypo1.fit_(X[:,0], Y, alpha = 1e-4, n_cycle = 1e5)
#hypo2.fit_(X[:,1], Y, alpha = 1e-4, n_cycle = 1e5)
#hypo1.rmse_(X[:,0],Y)
#hypo2.rmse_(X[:,0],Y)
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/02/10 12:22:13 by ythomas           #+#    #+#              #
#    Updated: 2020/02/10 15:13:21 by ythomas          ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

import numpy as np
from mylinearregression import MyLR
import sys
import matplotlib.pyplot as plt
import pandas as pd

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)
theta1 = np.array([[89.0], [-8]])
theta2 = np.array([[89.0], [-6]])
mylm1 = MyLR(theta1)
mylm2 = MyLR(theta2)
print(Xpill)
print("========")
print(Yscore)
Y_lm1 = mylm1.predict_(Xpill)
plt.plot(Xpill, Yscore, 'g^')
y = Xpill * theta1[1] + theta1[0]
plt.plot(Xpill, y)
plt.grid(True)
plt.show()
print(mylm1.mse_(Xpill, Yscore))
print(mylm2.mse_(Xpill, Yscore))
#print(mylr.theta)

theta_age = np.array([[1000.0], [-1.0]])
theta_thrust = np.array([[0.], [-1.0]])
theta_tera = np.array([[800.0], [-1.0]])

Xage2 = np.hstack((Xage, np.full((Xage.shape[0], 1), 1)))
mylr_age = MyLR(theta_age)
mylr_thrust = MyLR(theta_thrust)
mylr_tera = MyLR(theta_tera)

plt.subplot(231)
plt.ylabel('y : sell price')
plt.xlabel('x1 : age')
plt.plot(Xage, Yprice, 'bo')
pred_age = mylr_age.predict_(Xage)
plt.plot(Xage, pred_age, 'm.')
plt.grid('True')

plt.subplot(232)
plt.ylabel('y : sell price')
plt.xlabel('x2 : Thrust Power')
plt.plot(Xthrust, Yprice, 'go')
pred_thrust = mylr_thrust.predict_(Xthrust)
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')