Пример #1
0
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()
Пример #2
0
import pandas as pd
import numpy as np

#from sklearn.metrics import mean_squared_error
from mylinearregression import MyLinearRegression as MyLR

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)

linear_model1 = MyLR(np.array([[89.0], [-8]]))
linear_model2 = MyLR(np.array([[89.0], [-6]]))

print(linear_model1.mse_(Xpill, Yscore))
Пример #3
0
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()
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)
Пример #5
0
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))
Пример #6
0
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)
Y_new2 = myLR_ne.predict_(X)

print("LR MSE")
print(myLR_lgd.mse_(Y, Y_new1))
print("LR RMSE")
print(myLR_lgd.rmse_(Y, Y_new1))
print("NE MSE")
print(myLR_ne.mse_(Y, Y_new2))
print("NE RMSE")
print(myLR_ne.rmse_(Y, Y_new2))
print("LR R2SCORE")
print(myLR_lgd.r2score_(Y, Y_new1))
print("LR R2SCORE check")
print(r2_score(Y, Y_new1))
print("NE R2SCORE")
print(myLR_ne.r2score_(Y, Y_new2))
print("NE R2SCORE check")
print(r2_score(Y, Y_new2))
'''
Пример #7
0
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)')
plt.show()
Пример #8
0
Xage = np.array(data[['Age']])
Xtp = np.array(data[['Thrust_power']])
Xtm = np.array(data[['Terameters']])

Y = np.array(data[['Sell_price']])

# exemple 1.a
print('exemple 1.a')

myLR_age = MyLR([[650.0], [-10.0]])

myLR_age.fit_(Xage[:, 0].reshape(-1, 1), Y, alpha=2.5e-3, n_cycle=50000)
print(myLR_age.thetas)

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')
Пример #9
0
import numpy as np
import pandas as pd
#import matplotlib.pyplot as mlp
from mylinearregression import MyLinearRegression as MyLR
#from sklearn.metrics import mean_squared_error

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([[89.0], [-8]]))
linear_model2 = MyLR(np.array([[89.0], [-6]]))
Y_model1 = linear_model1.predict_(Xpill)
Y_model2 = linear_model2.predict_(Xpill)

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_model1))
Пример #10
0
    plt.show()
    plt.cla()


data = pd.read_csv("../resources/spacecraft_data.csv")

Y = np.array(data['Sell_price']).reshape(-1, 1)
X = np.array(data['Age']).reshape(-1, 1)
theta = np.array([[700.0], [-20.0]])
model_age = MyLR(theta, X, Y)
#draw_regression(model_age)

X = np.array(data['Thrust_power']).reshape(-1, 1)
theta = np.array([[0.0], [40.0]])
model_thrust = MyLR(theta, X, Y)
#draw_regression(model_thrust)

X = np.array(data['Terameters']).reshape(-1, 1)
theta = np.array([[800.0], [-2.0]])
model_tera = MyLR(theta, X, Y)
#draw_regression(model_tera)

X = np.array(data[['Age', 'Thrust_power', 'Terameters']])
my_lreg = MyLR([1.0, 1.0, 1.0, 1.0], X, Y)
print(my_lreg.mse_())
my_lreg.fit_(alpha=5e-5, n_cycle=600000)
print(my_lreg.theta)
print(my_lreg.mse_())

draw_multi_regression(my_lreg)
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()
Пример #12
0
Y_model_distance = linear_model.predict_(X3)
plt.subplot(133)
data_plot = plt.plot(X3, Y, 'mo', label='Sell price')
predict_plot = plt.plot(X3,
                        Y_model_distance,
                        '.',
                        color='violet',
                        label='Predicted sell price')
plt.xlabel('distance (Terameters))')
plt.ylabel('sell price (keuros)')
plt.legend()
plt.grid()

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')
Пример #13
0
from sklearn.metrics import mean_squared_error
from mylinearregression import MyLinearRegression as MyLR
import matplotlib.pyplot as plt

path = "ressources/day01/resources/"
data = pd.read_csv(path + "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("NB1", linear_model1.mse_(Y_model1, Yscore))
print("NB1", linear_model1.mse_(Xpill, Yscore))
# 57.60304285714282
print("NB2", mean_squared_error(Yscore, Y_model1))
# 57.603042857142825
# print("NB1", linear_model2.mse_(Y_model2, Yscore))
print("NB3", linear_model2.mse_(Xpill, Yscore))
# 232.16344285714285
print("NB4", mean_squared_error(Yscore, Y_model2))
# 232.16344285714285

data.plot.scatter("Micrograms", "Score")
# plt.show()

x = np.linspace(0, 10, 100)
y = linear_model1.theta[0] + (linear_model1.theta[1] * x)
plt.plot(x, y, '-r', label='Linear model 1')
def draw_multi_regression(mylr):

    #mylr.fit_()
    mylr.predict_()

    # Plot in function of age
    fig, ax = plt.subplots()
    ax.scatter(mylr.X[:, 0], mylr.Y)
    ax.scatter(mylr.X[:, 0], mylr.Y_hat, c="blue")
    plt.xlabel("Age")
    plt.ylabel("Sell_price")
    plt.title("")
    fig.legend(loc="lower left")
    plt.show()
    plt.cla()


data = pd.read_csv("../resources/spacecraft_data.csv")

Y = np.array(data['Sell_price']).reshape(-1, 1)
X = np.array(data[['Age', 'Thrust_power', 'Terameters']])

myLR_ne = MyLR([1., 1., 1., 1.], X, Y)
myLR_lgd = MyLR([1., 1., 1., 1.], X, Y)
myLR_lgd.fit_(alpha=5e-5, n_cycle=10000)
myLR_ne.normalequation_()
print(myLR_lgd.mse_())
print(myLR_ne.mse_())

draw_multi_regression(myLR_ne)
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()
Пример #16
0
import matplotlib.pyplot as plt
from mylinearregression import MyLinearRegression as MyLR

data = pd.read_csv("../resources/are_blue_pills_magics.csv")
linear_model1 = MyLR([[89.0], [-8]])
linear_model2 = MyLR([[89.0], [-6]])

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(linear_model1.mse_(Y_model1, Yscore))
linear_model1.fit_(Xpill, Yscore, alpha=0.0005, n_cycle=10000)

Y_model1 = linear_model1.predict_(Xpill)
print(linear_model1.mse_(Y_model1, Yscore))
plot1 = plt.plot(Xpill, Yscore, 'co', label='$S_{score}(pills)$')
plot2 = plt.plot(Xpill, Y_model1, 'g--o', label='$S_{predict}(pills)$')
plt.ylabel('Space driving score')
plt.xlabel('Quantity of blue pills (Mg)')
plt.legend(loc=0)
plt.grid()
plt.show()

theta0 = [89, 90, 85, 95, 87, 86]
theta1 = np.arange(-14,-4, 0.01)
linear_models = np.zeros((len(theta0),len(theta1)))
Пример #17
0
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...
# Training
print("Train models")

models = []
# thetas = [0., 0., 0., 0., 0.]
thetas = [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]
alpha = 1e-13
n_cycle = 50000
lambda_ = 0.

x_train = add_polynomial_features(x_train, 3)
x_test = add_polynomial_features(x_test, 3)

print("\tLinear Regression model (x1)")
m0 = MLR(thetas=thetas, alpha=alpha, n_cycle=n_cycle)
print(m0.mse_(x_train, y_train))
m0.fit_(x_train, y_train)
print(m0.mse_(x_train, y_train))

models.append(m0)

print("\tRidge Regression models (x9)")
for i in range(0, 9):
    lambda_ += 0.1
    mi = MR(thetas=thetas, alpha=alpha, n_cycle=n_cycle, lambda_=lambda_)
    print(mi.mse_(x_train, y_train))
    mi.fit_(x_train, y_train)
    print(mi.mse_(x_train, y_train), "\n")
    models.append(mi)

# Plots