Exemplo n.º 1
0
def test_MyLinearRegressing():
    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), end="\n\n")
    # Output:
    # array([[10.74695094],
    #        [17.05055804],
    #        [24.08691674],
    #        [36.24020866],
    #        [42.25621131]])

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

    # Example 0.2:
    print(lr1.cost_(lr1.predict_(x), y), end="\n\n")
    # Output:
    # 315.0202193084312

    # Example 1.0:
    # lr2 = MyLR([0, 0])
    lr2 = MyLR([1, 1], 5e-8, 1500000)
    lr2.fit_(x, y)
    print(lr2.thetas, end="\n\n")
    # Output:
    # array([[1.40709365],
    #        [1.1150909]])

    # Example 1.1:
    print(lr2.predict_(x), end="\n\n")
    # Output:
    # array([[15.3408728],
    #        [25.38243697],
    #        [36.59126492],
    #        [55.95130097],
    #        [65.53471499]])

    # Example 1.2:
    print(lr2.cost_elem_(lr2.predict_(x), y), end="\n\n")
    # Output:
    # array([[35.6749755],
    #        [4.14286023],
    #        [1.26440585],
    #        [29.30443042],
    #        [22.27765992]])

    # Example 1.3:
    print(lr2.cost_(lr2.predict_(x), y), end="\n\n")
Exemplo n.º 2
0
def plot2graphs(x: np.ndarray, y: np.ndarray) -> None:
    linear_model = MyLR(np.array([[89.0], [-8]]), max_iter=500)

    flag = 3
    if flag & 1:
        linear_model.fit_(x, y)
        y_hat = linear_model.predict_(x)
        plot_regression(x, y, y_hat)
    if flag & 2:
        plot_cost(x, y)
Exemplo n.º 3
0
#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')
    plt.plot(x_test, y_test, 'ro')
    plt.show()
    i += 1
    array2 = n[sep:, :]
    return (array1[:, :-1], array2[:, :-1], array1[:, -1], array2[:, -1])


if __name__ == "__main__":
    data = pd.read_csv("../resources/are_blue_pills_magics.csv")
    x = np.array(data[['Micrograms']])
    y = np.array(data[['Score']])

    lst = data_spliter(x, y, 0.5)
    x_train = lst[0]
    y_train = lst[2]
    y_train = y_train[:, np.newaxis]
    x_test = lst[1]
    y_test = lst[3]
    y_test = y_test[:, np.newaxis]

    i = 2
    my_lr = MyLinearRegression([[1], [1]])
    my_lr.fit_(x_train, y_train)
    y_hat = my_lr.predict_(x_test)
    print(my_lr.cost_(y_hat, y_test))

    while i <= 10:
        x_ = add_polynomial_features(x_train, i)
        my_lr = MyLinearRegression(np.ones(i + 1).reshape(-1, 1))
        my_lr.fit_(x_, y_train)
        x_2 = add_polynomial_features(x_test, i)
        y_hat = my_lr.predict_(x_2)
        print(my_lr.cost_(y_hat, y_test))
        i += 1
Exemplo n.º 5
0
    print(mean_squared_error(Yscore, Y_model2))

    print(Yscore)
    #print(linear_model1.cost_(linear_model1.predict_(Xpill), Yscore))
    #linear_model1.fit_(Xpill, Yscore)
    print(linear_model1.thetas)
    theta1_array = np.array([-14, -12, -10, -8, -6, -4])
    cost_array = np.zeros(6, )
    for j in range(87, 92):
        for i in range(6):
            print(theta1_array[i])
            pre = predict_(Xpill, np.array([j, theta1_array[i]]))
            #print(pre)
            #print(cost_(pre, Yscore))
            cost_array[i] = cost_(pre, Yscore)
        plt.plot(theta1_array, cost_array)
    print(cost_array)
    #plt.plot(theta1_array, cost_array)
    plt.grid()
    plt.show()

    linear_model1.fit_(Xpill, Yscore)
    Y_model1 = linear_model1.predict_(Xpill)
    plt.plot(Xpill, Yscore, '.', color='b')
    plt.plot(Xpill, Y_model1, 'x', color='lime')
    plt.plot(Xpill, Y_model1, 'r--', color='lime')
    plt.xlabel('Quantity of blue pill (in micrograms)')
    plt.ylabel('Space driving score')
    plt.title
    plt.grid()
    plt.show()
Exemplo n.º 6
0
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)
#lr7.fit_(x7, Y)
#lr8.fit_(x8, Y)
#lr9.fit_(x9, Y)

y_1 = lr1.predict_(x1)
y_2 = lr2.predict_(x2)
y_3 = lr3.predict_(x3)
y_4 = lr4.predict_(x4)
#y_5 = lr5.predict_(x5)
#y_6 = lr6.predict_(x6)
Exemplo n.º 7
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))
plt.show()
Exemplo n.º 8
0
from my_linear_regression import MyLinearRegression
from polynomial_model import add_polynomial_features

if __name__ == "__main__":
    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()

    i = 2
    arr = np.zeros(9)
    l = list(range(2, 11))
    while i <= 10:
        x_ = add_polynomial_features(x, i)
        my_lr = MyLinearRegression(np.ones(i + 1).reshape(-1, 1))
        my_lr.fit_(x_, y)
        arr[i - 2] = (my_lr.cost_(my_lr.predict_(x_), y))

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

        plt.scatter(x, y)
        plt.plot(continuous_x, y_hat, color='orange')
        plt.show()
        i += 1
    plt.bar(l, arr, color='orange')
    plt.show()
    print(arr)
Exemplo n.º 9
0
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")
plt.plot(tp, y_hat, "o", markersize=2)
plt.show()
Exemplo n.º 10
0
        theta_list = []
        for t1 in np.arange(-8 - 100, -8 + 100, 0.1):
            lr = MyLR(thetas=[t0, t1], alpha=1e-3, max_iter=50000)
            y_ = lr.predict(x)
            mse_c = lr.cost_(y, y_)  #[0][0]
            cost_list.append(mse_c)
            theta_list.append(t1)
            # print(cost_list[-1])
        label = "θ[0]=" + str(int(t0 * 10) / 10)
        print(label, "done!")
        plt.plot(theta_list, cost_list, label=label)
    plt.xlabel("θ[1]")
    plt.ylabel("MSE(θ[0], θ[1])")
    plt.legend(loc='upper left')
    plt.show()


# lr = MyLR(thetas=[89.0, -8], alpha=5e-8, max_iter=1500000)
lr = MyLR(thetas=[0, 0], alpha=1e-3, max_iter=50000)
plot_cost(Xpill, Yscore)
# lr = MyLR(thetas=[49.9399813, -1.42892958], alpha=1e-7, max_iter=2000000)

# lr.gradient(Xpill, Yscore)
# print(lr.mse_(Yscore, lr.predict(Xpill)).mean())
# lr = MyLR(thetas=[89.0, -8], alpha=5e-8, max_iter=1500000)
# lr.gradient(Xpill, Yscore)
# print(lr.mse_(Yscore, lr.predict(Xpill)).mean())

lr.fit_(Xpill, Yscore)
# plot(Xpill, Yscore, lr.theta)
Exemplo n.º 11
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.º 12
0
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))
# Output:
# 315.0202193084312

lr2 = MyLR([0, 0])
# Example 1.0:
lr2.fit_(x, y, alpha=5e-4, n_cycle=1500000)
print(lr2.thetas)
# Output:
# array([   [1.40709365],
# [1.1150909 ]])

# Example 1.1:
print(lr2.predict_(x))
# Output:
# array([   [15.3408728 ],
#           [25.38243697],
#           [36.59126492],
#           [55.95130097],
#           [65.53471499]])

# Example 1.2:
Exemplo n.º 13
0
    for i in np.linspace(t0 - 10, t0 + 50, 3000):
        linear_model3 = MyLR(np.array([[-10], [i]]))
        Y_model3 = linear_model3.predict_(Xpill)
        plt.plot(linear_model3.thetas[1], linear_model3.cost_(y, Y_model3),
                 'gs')


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]]))
Y_model1 = linear_model1.predict_(Xpill)
Y_model2 = linear_model2.predict_(Xpill)

linear_model1_2 = MyLR(linear_model1.fit_(Xpill, Yscore))
Y_model1_2 = linear_model1_2.predict_(Xpill)

print(linear_model1.cost_(Yscore, Y_model1) * 2)
print(mean_squared_error(Yscore, Y_model1))
print(linear_model1.cost_(Yscore, Y_model2) * 2)
print(mean_squared_error(Yscore, Y_model2))

plt.plot(Xpill, Y_model1_2, 'gs')
plt.plot(Xpill, Y_model1_2, 'g--', label="Spredict(pills)")
plt.plot(Xpill, Yscore, '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()
Exemplo n.º 14
0
	print("# Example 1:")
	print(mylr.cost_elem_(X,Y))
	print("# Output:")
	print("array([[37.5], [0.], [1837.5]])")
	print()

	print("# Example 2:")
	print(mylr.cost_(X,Y))
	print("# Output:")
	print(1875.0)
	print()

	# sys.lol()
	print("# Example 3:")
	mylr.fit_(X, Y)
	print(mylr.theta)
	print("# Output:")
	print("array([[18.023..], [3.323..], [-0.711..], [1.605..], [-0.1113..]])")
	print()

	print("# Example 4:")
	print(mylr.predict(X))
	print("# Output:")
	print("array([[23.499..], [47.385..], [218.079...]])")
	print()

	print("# Example 5:")
	print(mylr.cost_elem_(X,Y))
	print("# Output:")
	print("array([[0.041..], [0.062..], [0.001..]])")
Exemplo n.º 15
0
    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))
    #plot(x_test, y_test, y_hat, features)
Exemplo n.º 16
0
from my_linear_regression import MyLinearRegression as MyLR
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error

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]))
linear_model1.fit_ulti(Xpill, Yscore)
#Y_model1 = linear_model1.predict_(Xpill)
#Y_model2 = linear_model2.predict_(Xpill)
#print(Y_model1)
#print(Y_model2)
#print(linear_model1.cost_(Xpill, Yscore))
theta = linear_model1.fit_(Xpill, Yscore)
#print(theta)
Exemplo n.º 17
0
    # print("Ex 1.a")
    # my_lreg = MyLR([1.0, 1.0], alpha=1e-3, max_iter=60000)
    # my_lreg.fit_(X_1, Y)
    # my_lreg.scatter(X_1, Y)
    #
    # print("Ex 1.b")
    # my_lreg = MyLR([1.0, 1.0], alpha=1e-4, max_iter=60000)
    # my_lreg.fit_(X_2, Y)
    # my_lreg.scatter(X_2, Y)
    #
    # print("Ex 1.c")
    # my_lreg = MyLR([1.0, 1.0], alpha=1e-4, max_iter=500000)
    # my_lreg.fit_(X_3, Y)
    # my_lreg.scatter(X_3, Y)

    my_lreg = MyLR([1.0, 1.0, 1.0, 1.0], alpha=9e-5, max_iter=500000)
    # print(my_lreg.mse_(X,Y).sum() / 2)
    # print("144044.877...")
    # print()

    my_lreg.fit_(X, Y)
    print(my_lreg.theta)
    print("array([[334.994...],[-22.535...],[5.857...],[-2.586...]])")
    print()
    my_lreg.multi_scatter(X, Y)

    # print(my_lreg.mse_(X,Y))
    # print("586.896999...")
    # print()
Exemplo n.º 18
0
# Output:
# array([[77.72116511],
#        [49.33699664],
#        [72.38621816],
#        [37.29223426],
#        [78.28360514]])

# Example 0.2:
print("\nExample 0.2")
print(lr1.cost_(lr1.predict_(x), y))
# Output:
# 315.0202193084312

# Example 1.0:
lr2 = MyLR([1, 1], 5e-8, 1500000)
lr2.fit_(x, y)
print("\nExample 1.0")
print(lr2.thetas)
# Output:
# array([[1.40709365],
#        [1.1150909 ]])

# Example 1.1:
print("\nExample 1.1")
print(lr2.predict_(x))
# Output:
# array([[15.3408728 ],
#        [25.38243697],
#        [36.59126492],
#        [55.95130097],
#        [65.53471499]])
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.cost_(Xpill, Yscore)) # 57.60304285714282 >>>
print(mean_squared_error(Yscore, Y_model1)) # 57.603042857142825 >>>
# print(linear_model2.cost_(Xpill, Yscore)) # 232.16344285714285
# print(mean_squared_error(Yscore, Y_model2))

x= Xpill
y = Yscore
plt.scatter(x, y)
linear_model1.fit_(x, y)
plt.xlabel('Quantity of blue pill (in micrograms)')
plt.ylabel('Space driving score')
plt.title('simple plot')
plt.plot(x, linear_model1.predict_(x), color='green')
plt.legend(['S_true', 'S_predict'])
plt.show()




legends = []
plt.xlabel('theta1')
plt.ylabel('cost func J(theta0, theta1)')
theta0s = np.arange(linear_model1.thetas[0] - 15, linear_model1.thetas[0] + 15, 5)
theta1 = linear_model1.thetas[1].copy()
Exemplo n.º 20
0
#  [ 4.83925060e-06],
#  [ 1.31593092e-05],
#  [ 3.83642999e-05],
#  [ 1.13422797e-04],
#  [ 3.26767863e-04],
#  [ 8.75990025e-04],
#  [ 2.00179965e-03],
#  [ 2.99573196e-03],
#  [-1.12062352e-03],
#  [ 9.40458287e-05]]
# big_theta += (np.array(big_theta_futur) - np.array(big_theta)) * 10
# big_theta = big_theta.tolist()

# lr = MyLR(thetas=big_theta, alpha=5e-16, max_iter=10000000)
# lr.fit_(x, Yscore)

for i in range(3, 4):
    x = add_polynomial_features(Xpill, i)
    lr = MyLR(thetas=[0] * (i + 1),
              alpha=1 / (100 * (10**i)),
              max_iter=10000 * (10**i))
    lr.fit_(x, Yscore)
    cost.append(lr.cost)
    plt.ioff()
    continuous_plot(x, Yscore, i, lr)

for i, c in enumerate(cost):
    plt.plot(c, label="Polynomial of degree " + str(i + 2))
plt.legend(loc='best')
plt.show()