示例#1
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)
示例#2
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")
示例#3
0
def plot_cost(x: np.ndarray, y: np.ndarray) -> None:
    plt.xlabel("$θ_1$")
    plt.ylabel("cost function $J(θ_0, θ_1)$")
    plt.grid()

    linear_model = MyLR(np.array([[0], [0]]), max_iter=500)
    thetas_0 = range(85, 95, 2)
    for t0 in thetas_0:
        linear_model.thetas[0][0] = t0

        npoints = 100
        y_cost = [0] * npoints
        thetas1 = np.linspace(-15, -3.8, npoints)
        for i, t1 in enumerate(thetas1):
            linear_model.thetas[1][0] = t1
            y_hat = linear_model.predict_(x)
            y_cost[i] = linear_model.cost_(y, y_hat)
        plt.plot(thetas1, y_cost, label="$J(θ_0=%d, θ_1)$" % t0)

    plt.legend()
    plt.show()
import numpy as np


import pandas as pd
from sklearn.metrics import mean_squared_error
from my_linear_regression import MyLinearRegression as MyLR



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.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')
    for v1_x in range(len(x_matrix)):
        for v1_y in range(len(x_matrix[0])):
            for v2_y in range(len(theta[0])):
                new_m[v1_x][v2_y] += x_matrix[v1_x][v1_y] * theta[v1_y][v2_y]
    y_hat = np.array([elem for lst in new_m for elem in lst])
    for i, elem in enumerate(y_hat):
        plt.vlines(x=x[i], ymin=y[i], ymax=elem, colors='green', ls='--', lw=2)
    cost = 2 * sum(
        [pow((e1 - e2), 2) / (2 * len(y)) for e1, e2 in zip(y, y_hat)])

    plt.plot(x, y_hat, color="#00ff00")
    plt.xlabel("X")
    plt.ylabel("Y")
    title = "Cost : " + str(cost)[:9]
    plt.title(title)
    plt.show()


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)

linear_model1.plot(Xpill, Yscore)
# plot(Xpill, Yscore, np.array([89.0, -8.0]))
# print(linear_model2.mse_(Xpill, Yscore))
示例#6
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)
#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
示例#8
0
Xpill = np.array(data["Micrograms"]).reshape(-1,1)
Yscore = np.array(data["Score"]).reshape(-1,1)
thetas = np.array([1, 1])
#plt.plot(Xpill, Yscore, 'o')

mlr = MyLR(thetas, alpha=0.05, n_cycle=5000)
#th = mlr.fit_(Xpill, Yscore)
#print(th)
#plt.plot(Xpill, (th[1] * Xpill + th[0]), '-r')
#plt.show()

for j in range(80, 100, 5):
    res = []
    for i in range(-11, -7, 1):
        mlr.thetas = np.array([j, i])
        dummy, y_hat = mlr.predict_(Xpill)
        res.append(mlr.mse_(Yscore, y_hat))
    np.array(res)
    plt.plot(np.arange(-11, -7), res)
plt.show()

for j in range(80, 100, 5):
    res = []
    for i in range(-11, -7, 1):
        mlr.thetas = np.array([j, i])
        dummy, y_hat = mlr.predict_(Xpill)
        res.append(mean_squared_error(Yscore, y_hat))
    np.array(res)
    plt.plot(np.arange(-11, -7), res, '-r')
plt.show()
    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
示例#10
0
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)
#y_7 = lr7.predict_(x7)
#y_8 = lr8.predict_(x8)
#y_9 = lr9.predict_(x9)

print(lr3.cost_(x3, Y))

plt.plot(X, Y, 'o')
plt.plot(X, y_1, 'g')
plt.plot(X, y_2, 'r')
plt.plot(X, y_3, 'b')
示例#11
0
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()
示例#12
0
import numpy as np
from my_linear_regression 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("Example 0.0")
print(lr1.predict_(x))
# Output:
# array([[10.74695094],
#        [17.05055804],
#        [24.08691674],
#        [36.24020866],
#        [42.25621131]])

# Example 0.1:
print("\nExample 0.1")
print(lr1.cost_elem_(lr1.predict_(x), y))
# 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))
示例#13
0
def print_costfn(t0, y):
    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')
示例#14
0
    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)