Exemplo n.º 1
0
import numpy as np
from my_linear_regression import My_linear_regression as MLR


def add_polynomial_features(x, power):
    power = range(1, power + 1)
    init = x
    for pow in power:
        x = np.column_stack((x, init**pow))
    return x


data = pd.read_csv("../resources/spacecraft_data.csv")
X = np.array(data[['Age', 'Thrust_power', 'Terameters']])
Y = np.array(data["Sell_price"])
LR = MLR(np.ones((X.shape[1] + 1, 1)))
print(LR.cost_function(LR.predict(X), Y))
LR.fit(X, Y, alpha=1e-5, max_iter=5000000)
# print(LR.cost_function(LR.predict(X), Y))

# P = add_polynomial_features(X, 2)
# LR.theta = np.full(3 ** 2 + 1,1)
# LR.fit(P, Y, alpha = 1e-9, max_iter = 5000000)
# print(LR.cost_function(LR.predict(P), Y))
# P = add_polynomial_features(X, 3)
# LR.theta = np.full(10 + 3,1)
# LR.fit(P, Y, alpha = 1e-12, max_iter = 5000000)
# print(LR.cost_function(LR.predict(P), Y))
# P = add_polynomial_features(X, 4)
# LR.theta = np.full(10 + 6,1)
# LR.fit(P, Y, alpha = 1e-9, max_iter = 5000000)
Exemplo n.º 2
0
data = pd.read_csv("are_blue_pills_magics.csv")
X = np.array(data[['Micrograms']])
Y = np.array(data[['Score']])

x1 = poly_feat(X, 2)
x2 = poly_feat(X, 3)
x3 = poly_feat(X, 4)
x4 = poly_feat(X, 5)
x5 = poly_feat(X, 6)
x6 = poly_feat(X, 7)
x7 = poly_feat(X, 8)
x8 = poly_feat(X, 9)
x9 = poly_feat(X, 10)

lr1 = MLR([80, 1, 1])
lr2 = MLR([80, 1, 1, 1])
lr3 = MLR([80, 1, 1, 1, 1])
lr4 = MLR([80, 1, 1, 1, 1, 1])
lr5 = MLR([80, 1, 1, 1, 1, 1, 1])
lr6 = MLR([1, 1, 1, 1, 1, 1, 1, 1])
lr7 = MLR([1, 1, 1, 1, 1, 1, 1, 1, 1])
lr8 = MLR([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
lr9 = MLR([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

lr1.fit_(x1, Y)
lr2.fit_(x2, Y)
lr3.fit_(x3, Y)
lr4.fit_(x4, Y)
#lr5.fit_(x5, Y)
#lr6.fit_(x6, Y)
Exemplo n.º 3
0
#normalise new_train
for i in range(10):
    new_train[:, i] = minmax(new_train[:, i])

#for plotting of polynomial curves - cotinuous data set over range of original data
#then add polynomial features and normalise
continuous_x = np.arange(1, 7.01, 0.01).reshape(-1, 1)
x_ = add_polynomial_features(continuous_x, 10)
for i in range(10):
    x_[:, i] = minmax(x_[:, i])

thetas = np.ones(11).reshape(-1, 1)

cost_values = []
thetas_list = []
mlr = MLR(thetas, alpha=0.009, n_cycle=5000)
for degree in range(2, 11):
    mlr.thetas = thetas[:degree + 1]
    thetas_list.append(mlr.fit_(new_train[:, :degree], y_train))
    cost_values.append(
        mlr.mse_(y_train,
                 mlr.predict_(new_train[:, :degree])[1]))

i = 2
for elem in thetas_list:
    mlr.thetas = elem
    y_hat = mlr.predict_(x_[:, :i])[1]
    plt.plot(continuous_x, y_hat, '--')
    plt.title(str(degree))
    plt.title(('degree = ' + str(i) + ' cost: ' + str(cost_values[i - 2])))
    plt.plot(x_train, y_train, 'go')
Exemplo n.º 4
0
x_test = np.array([5, 4.3, 2, 2, 5, 6, 3.5]).reshape(-1, 1)
y_test = np.array([39, 52, 70, 58, 50, 32, 62]).reshape(-1, 1)
plt.plot(x_test, y_test, 'o')

new = add_polynomial_features(x, 10)

thetas = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
cost_values = []

#for plotting of polynomial curves - cotinuous data set over range of original data
continuous_x = np.arange(1, 7.01, 0.01).reshape(-1, 1)
x_ = add_polynomial_features(continuous_x, 10)

degree = 2
mlr = MLR(np.array(thetas[:degree + 1]), alpha=0.002, n_cycle=1500)
mlr.fit_(new[:, :degree], y)
cost_values.append(mlr.mse_(y, mlr.predict_(new[:, :degree])[1]))
y_hat = mlr.predict_(x_[:, :degree])[1]
plt.plot(continuous_x, y_hat, '--')
plt.title(str(degree))
plt.show()

degree = 3
mlr = MLR(np.array(thetas[:degree + 1]), alpha=0.00005, n_cycle=4000)
mlr.fit_(new[:, :degree], y)
cost_values.append(mlr.mse_(y, mlr.predict_(new[:, :degree])[1]))
y_hat = mlr.predict_(x_[:, :degree])[1]
plt.plot(x, y, 'o')
plt.plot(continuous_x, y_hat, '--')
plt.title(str(degree))
Exemplo n.º 5
0
import pandas as pd
import numpy as np
from my_linear_regression import My_linear_regression as MLR

data = pd.read_csv("../resources/spacecraft_data.csv")
X = np.array(data[['Age', 'Thrust_power', 'Terameters']])
Y = np.array(data["Sell_price"])
LR = MLR(np.array([1.0, 1.0, 1.0, 1.0]))
print(LR.cost_function(LR.predict(X), Y))
LR.fit(X, Y, alpha=1e-6, max_iter=60000000)
print(LR.cost_function(LR.predict(X), Y))
# LR.plot(X, Y, LR_age.linear_regression_cost_function(LR_age.predict(X), Y))
Exemplo n.º 6
0
thetas = np.array([1, 1])
plt.ylabel("price")

#mlr_age = MLR(thetas, alpha=0.01, n_cycle=4000)
#plot(mlr_age, age, y, "age")

#mlr_thrust = MLR(thetas, alpha=0.00001, n_cycle=30)
#plot(mlr_thrust, tp, y, "thrust power")

#mlr_tm = MLR(thetas, alpha=0.00022, n_cycle=76000)
#plot(mlr_tm, tm, y, "terameters")

thetas = np.array([1, 1, 1, 1])

mlr_multi = MLR(thetas, alpha=0.00009, n_cycle=100)
#mlr_multi.plot_cost_change(features, y)
th = mlr_multi.fit_(features, y)
print(th)
y_hat = th[0]
i = 1
while i < 4:
    y_hat += th[i] * features[:, i - 1:i]
    i += 1

plt.plot(age, y, "ob")
plt.xlabel("age")
plt.plot(age, y_hat, "o", markersize=2)
plt.show()
plt.plot(tp, y, "ob")
plt.xlabel("thrust power")
Exemplo n.º 7
0
#                                                     +:+ +:+         +:+      #
#    By: ecross <*****@*****.**>                      +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/05/04 12:33:11 by ecross            #+#    #+#              #
#    Updated: 2020/05/04 12:46:15 by ecross           ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_regression

from my_linear_regression import MyLinearRegression as MLR

if __name__ == "__main__":

    x, y = make_regression(n_samples=100, n_features=1, noise=10)
    theta = np.array([1, 1])

    #mlr = MLR(theta, 0.001, 500)
    #theta1 = mlr.fit_(x, y)
    #plt.plot(x, y, 'o')
    #plt.plot(x, (theta1[1] * x + theta1[0]), '-r')

    mlr = MLR(theta, 0.5, 1100)
    theta1 = mlr.fit_(x, y)
    plt.plot(x, y, 'o')
    plt.plot(x, (theta1[1] * x + theta1[0]), '-g')

    plt.show()
Exemplo n.º 8
0
for i in range(3):
    plt.plot(x_train[:, i:i + 1], y_train, 'go')
    plt.title(data.columns[i])
    plt.plot(x_test[:, i:i + 1], y_test, 'ro', markersize=3)
    plt.show()

#initialise thetas as array with feature number + 1 zeros
thetas = np.zeros(new_features.shape[1] + 1)

#should be able to use same alpha and cycle number for all, as same data

#carry out linear regression on training data

cost_list = []

mlr = MLR(thetas, alpha=0.1, n_cycle=400)
mlr.fit_(x_train, y_train)
y_hat = mlr.predict_(x_test)[1]
cost_list.append(mlr.mse_(y_test, y_hat))
plot(x_test, y_test, y_hat, features)

#carry out 9 ridge regressions on training data, with lambda from 0.1 to 0.9

mrg = MRG(thetas, alpha=0.1, n_cycle=400)
for i in range(1, 10):
    mrg.lambda_ = i / 10
    mrg.thetas = thetas
    plt.title('lambda = ' + str(i / 10))
    mrg.fit_(x_train, y_train)
    y_hat = mrg.predict_(x_test)[1]
    cost_list.append(mlr.mse_(y_test, y_hat))