def create_lr():
    try:
        with open("config.mlr", "r") as f:
            config_str = f.read()
        config = eval(config_str, {"array": np.array})

        lr = MyLR(**config)
    except FileNotFoundError:
        lr = MyLR(np.zeros(2))

    return lr
示例#2
0
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")
示例#3
0
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)
示例#4
0
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)
示例#5
0
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
示例#6
0
def reg_linear_grad(x: np.ndarray, y: np.ndarray, theta: np.ndarray, lambda_: float) -> np.ndarray:
    if (0 in [x.size, y.size, theta.size] or x.shape[0] != y.shape[0] or
        (x.shape[1] + 1) != theta.shape[0]):
        return None
    res = np.zeros(shape=(theta.shape))
    m, n = x.shape
    mylr = MyLR(theta)
    y_hat = mylr.predict_(x)
    for i in range(m):
        y_diff = y_hat[i][0] - y[i][0]
        res[0][0] += y_diff
        for j in range(n):
            res[j + 1][0] += (y_diff * x[i][j]) + (lambda_ * theta[j + 1][0]) / m
    res = res / m
    return res
示例#7
0
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))
示例#8
0
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()
示例#9
0
def univariate_lr(df, feature_name, alpha=5e-5, n_cycle=500000, colors=("g", "lime")):
    y = np.array(df["Sell_price"])
    x_feature = np.array(df[feature_name])

    lr = MyLR([1, 1], alpha=alpha, n_cycle=n_cycle)
    lr.fit_(x_feature, y)
    print(f"thetas: {lr.thetas}, cost: {lr.cost_(x_feature, y)}")

    y_hat = lr.predict_(x_feature)

    # Plot
    plt.plot(x_feature, y, "o", color=colors[0], label="Sell price")
    plt.plot(x_feature, y_hat, "o",
             color=colors[1], label="Predicted sell price")

    plt.legend(loc="best")
    plt.xlabel(feature_name)
    plt.ylabel("Sell price")
    plt.show()
示例#10
0
def multivariate_lr(df):
    x = np.array(df[["Age", "Thrust_power", "Terameters"]])
    y = np.array(df["Sell_price"])

    # lr = MyLR([1, 1, 1, 1], 9e-5, 1000000)
    lr = MyLR([380, -24, 5, -2], 9e-5, 100000)
    lr.fit_(x, y)
    print(f"thetas: {lr.thetas}, cost: {lr.cost_(x, y)}")

    y_hat = lr.predict_(x)

    # Plot
    ax1 = plt.subplot(131)
    x_age = np.array(df["Age"])
    ax1.plot(x_age, y, "o", color="b", label="Sell price")
    ax1.plot(x_age, y_hat, "o",
             color="dodgerblue", label="Predicted sell price")
    ax1.set_xlabel("Age")
    ax1.set_ylabel("Sell price")
    ax1.legend(loc="best")

    ax2 = plt.subplot(132)
    x_thrust = np.array(df["Thrust_power"])
    ax2.plot(x_thrust, y, "o", color="g", label="Sell price")
    ax2.plot(x_thrust, y_hat, "o",
             color="lime", label="Predicted sell price")
    ax2.set_xlabel("Thrust_power")
    ax2.set_ylabel("Sell price")
    ax2.legend(loc="best")

    ax3 = plt.subplot(133)
    x_dist = np.array(df["Terameters"])
    ax3.plot(x_dist, y, "o", color="darkviolet", label="Sell price")
    ax3.plot(x_dist, y_hat, "o",
             color="violet", label="Predicted sell price")
    ax3.set_xlabel("Terameters")
    ax3.set_ylabel("Sell price")
    ax3.legend(loc="best")

    # plt.tight_layout()
    plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
    plt.show()
示例#11
0
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()
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")
示例#13
0
def vec_reg_linear_grad(x: np.ndarray, y: np.ndarray, theta: np.ndarray, lambda_: float) -> np.ndarray:
    """Computes the regularized linear gradient of three non-empty numpy.ndarray, without any for-loop. The three arrays must have compatible dimensions.
    Args:
      x: has to be a numpy.ndarray, a matrix of dimesion m * n.
      y: has to be a numpy.ndarray, a vector of dimension m * 1.
      theta: has to be a numpy.ndarray, a vector of dimension n * 1.
      lambda_: has to be a float.
    Returns:
      A numpy.ndarray, a vector of dimension n * 1, containing the results of the formula for all j.
      None if y, x, or theta are empty numpy.ndarray.
      None if y, x or theta does not share compatibles dimensions.
    Raises:
      This function should not raise any Exception.
    """
    if (0 in [x.size, y.size, theta.size] or x.shape[0] != y.shape[0] or
        (x.shape[1] + 1) != theta.shape[0]):
        return None
    mylr = MyLR(theta)
    m = x.shape[0]
    y_hat = mylr.predict_(x)
    x = mylr.add_intercept(x)
    theta_prime = np.concatenate((np.array([[0]]), theta[1:, ...]), axis=0)
    nabla_j = (x.T.dot(y_hat - y) + lambda_ * theta_prime) / m
    return nabla_j
示例#14
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()
示例#15
0
    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()
def prepare(data, column_data, x_banner, leg_loc):
    dataX = data[column_data][1:]
    dataY = data['Sell_price'][1:]
    myLR_age = MyLR(np.array([[1.], [1.]]), alpha=2.5e-5)
    myLR_age.fit_(dataX, dataY)
    plot_(myLR_age, dataX, dataY, x_banner, leg_loc)
    plt.legend(loc=leg_loc)
    plt.show()


def prepare(data, column_data, x_banner, leg_loc):
    dataX = data[column_data][1:]
    dataY = data['Sell_price'][1:]
    myLR_age = MyLR(np.array([[1.], [1.]]), alpha=2.5e-5)
    myLR_age.fit_(dataX, dataY)
    plot_(myLR_age, dataX, dataY, x_banner, leg_loc)


data = pd.read_csv("../resources/spacecraft_data.csv")
#prepare(data, 'Age', 'x1: age(in years)', 'lower left')
#prepare(data, 'Thrust_power', 'x2: thrust power(in 10Km/s)', 'upper left')
#prepare(data, 'Terameters', 'x3: distance totalizer value of spacecraft (in Tmeters)', 'upper right')
X = np.array(data[['Age','Thrust_power','Terameters']])
Y = np.array(data[['Sell_price']]).reshape((-1, 1))
my_lreg = MyLR(np.array([[300.0], [-10.0], [3.0], [-2.0]]), alpha=2.5e-5, n_cycle=600000)
my_lreg.fit_(X, Y)
prediction = my_lreg.predict_(X)
#real_age = X[:, 0]
real_dist = X[:, 2]
plt.grid(True)
plt.scatter(real_dist, Y, color='b', s=40, label='Sell price')
plt.scatter(real_dist, prediction, color='c', s=10, label='Predicted sell price')
#plt.xlabel('x1: age(in years)')
plt.xlabel('x2: thrust power(in 10Km/s)')
plt.ylabel('y: sell price (in keuros)')
plt.legend(loc='lower left')
plt.show()
示例#18
0
#
# t0 = t1 * t0
# values = [MyLR(t0).mse_(Xpill, Yscore) for i in thetazeros]
# t1 = MyLR(t0).cost_(Xpill, Yscore)
# plt.plot(thetazeros, values, 'k')
#

plt.show()

fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
Z = MyLR(np.array([[Xi], [Yi]])).cost_(Xpill, Yscore)

# Plot the surface.
surf = ax.plot_surface(X,
                       Y,
                       Z,
                       cmap=cm.coolwarm,
                       linewidth=0,
                       antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
示例#19
0
import numpy as np
from mylinearregression import MyLinearRegression as MyLR

x = np.array([[12.4956442], [21.5007972], [31.5527382], [48.9145838],
              [57.5088733]])
y = np.array([[37.4013816], [36.1473236], [45.7655287], [46.6793434],
              [59.5585554]])

lr1 = MyLR([2, 0.7])

# Example 0.0:
# print(lr1.predict_(x))
# Output:
# array([   [10.74695094],
#           [17.05055804],
#           [24.08691674],
#           [36.24020866],
#           [42.25621131]])

# Example 0.1:
# cost_elems = lr1.cost_elem_(lr1.predict_(x),y)
# print(cost_elems)
# Output:
# array([   [77.72116511],
#           [49.33699664],
#           [72.38621816],
#           [37.29223426],
#           [78.28360514]])

# Example 0.2:
# print(lr1.cost_(lr1.predict_(x),y))
示例#20
0
    """
    Computes the normalized version of a non-empty numpy.ndarray using the min-max
    standardization.
    Args:
    x: has to be an numpy.ndarray, a vector.
    Returns:
    x' as a numpy.ndarray.
    None if x is a non-empty numpy.ndarray.
    Raises:
    This function shouldn't raise any Exception.
    """
    return (x - np.min(x)) / (np.max(x) - np.min(x))



X = np.array([[1., 1., 2., 3.], [5., 8., 13., 21.], [34., 55., 89., 144.]])
X = minmax(X)
Y = np.array([[23.], [48.], [218.]])
Y = minmax(Y)
mylr = MyLR(np.array([[1.], [1.], [1.], [1.], [1.]]))

plot_(mylr, X, Y)
mylr.fit_(X, Y)
plot_(mylr, X, Y)
plt.show()
#plot_cost_(mylr)
#mylr.fit_(X, Y)
print(mylr.thetas)
#plot_cost_(mylr)
#plot_(mylr, X, Y)
示例#21
0

def add_polynomial_features(x, power):
    temp = x.copy()
    for i in range(2, power + 1):
        temp = np.append(temp, np.power(x, i), axis=1)
    return temp


x = np.arange(1, 11).reshape(-1, 1)
y = np.array([[1.39270298], [3.88237651], [4.37726357], [4.63389049],
              [7.79814439], [6.41717461], [8.63429886], [8.19939795],
              [10.37567392], [10.68238222]])
plt.scatter(x, y)
plt.show()

from polynomial_model import add_polynomial_features
from mylinearregression import MyLinearRegression as MyLR
# Build the model:
x_ = add_polynomial_features(x, 3)
my_lr = MyLR(np.ones(4).reshape(-1, 1))
my_lr.fit_(x_, y)
## To get a smooth curve, we need a lot of data points
continuous_x = np.arange(1, 10.01, 0.01).reshape(-1, 1)
x_ = add_polynomial_features(continuous_x, 3)
y_hat = my_lr.predict_(x_)
plt.scatter(x, y)
# print(my_lr.thetas)
plt.plot(continuous_x, y_hat, color='orange')
plt.show()
示例#22
0
文件: plot.py 项目: Karocyt/PiscineML
#! /usr/bin/env python3

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
示例#23
0
import pandas as pd
import numpy as np
from mylinearregression import MyLinearRegression as MyLR
import matplotlib.pyplot as plt

data = pd.read_csv("../resources/spacecraft_data.csv")
X = np.array(data[['Age', 'Thrust_power', 'Terameters']])
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)
print(myLR_ne.theta)
Y_new2 = myLR_ne.predict_(X)
'''
print("MSE = ")
print(myLR_lgd.theta)
print(myLR_lgd.mse_(Y, Y_new1))
print("MSE = ")
print(myLR_ne.theta)
print(myLR_ne.mse_(Y, Y_new2))
'''
plt.scatter(data.Age, Y_new1, color='green')
plt.scatter(data.Age, Y_new2, color='red')
示例#24
0
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)
示例#25
0
#!/usr/bin/env python

#%%
from polynomial_model import add_polynomial_features
from mylinearregression import MyLinearRegression as MyLR
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(1, 11).reshape(-1, 1)
y = np.array([[1.39270298], [3.88237651], [4.37726357], [4.63389049],
              [7.79814439], [6.41717461], [8.63429886], [8.19939795],
              [10.37567392], [10.68238222]])

plt.scatter(x, y)
# %%
power = 3
x_ = add_polynomial_features(x, power)
my_lr = MyLR(np.ones(power + 1).reshape(-1, 1), 0.00001, 100000)
my_lr.fit_(x_, y)

continuous_x = np.arange(1, 10.01, 0.01).reshape(-1, 1)
x_ = add_polynomial_features(continuous_x, power)
y_hat = my_lr.predict_(x_)

plt.scatter(x, y)
plt.plot(continuous_x, y_hat, color='orange')
plt.show()

# %%
        This function should not raise any Exception.
        """
        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..
示例#27
0
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()
示例#28
0
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)
示例#29
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...
import math
import matplotlib.pyplot as plt

from mylinearregression import MyLinearRegression as MyLR
from polynomial_model import add_polynomial_features

data = pd.read_csv("../../day01/resources/are_blue_pills_magics.csv")

X = np.array(data.Micrograms).reshape(-1, 1)
Y = np.array(data.Score).reshape(-1, 1)

x = []
myLR = []
for i in range(0, 9):
    print("For power {} :".format(i + 2))
    x.append(add_polynomial_features(X, i + 2))
    thetas = np.full((i + 3, 1), 1.0)
    myLR.append(MyLR(thetas))

    alpha = 1 / math.pow(10, 3 + i * 2)
    myLR[i].fit_(x[i], Y, alpha=alpha, n_cycle=250000)

    MSE = myLR[i].mse_(x[i], Y)

    # print("thetas = {}".format(myLR[i].thetas))
    print("mse = {}\n".format(MSE))
    plt.bar(i + 2, MSE, label="power {}".format(i + 2))

plt.legend(prop={'size': 10})
plt.show()