示例#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")
示例#2
0
def plot_cost(x, y):
    """Plot the data and prediction line from three non-empty numpy.ndarray.
	Args:
	x: has to be an numpy.ndarray, a vector of dimension m * 1.
	y: has to be an numpy.ndarray, a vector of dimension m * 1.
	theta: has to be an numpy.ndarray, a vector of dimension 2 * 1.
	Returns:
	Nothing.
	Raises:
	This function should not raise any Exceptions.
	"""
    # plt.plot(x, y, 'o')
    # x = np.linspace(-15,5,100)
    plt.ylim((10, 50))
    plt.xlim((-13, -4.5))
    ran = 15
    upd = ran * 2 / 6
    for t0 in np.arange(89 - ran, 89 + ran, upd):
        cost_list = []
        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()
示例#3
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)
示例#4
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()
示例#5
0
# **************************************************************************** #

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error

from my_linear_regression import MyLinearRegression as MyLR

data = pd.read_csv("../subjects/day01/resources/are_blue_pills_magics.csv")
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()
示例#6
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))
示例#7
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)
import pandas as pd
import numpy as np
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")
# print(data)

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_model2.plot_costs(Xpill, Yscore)
linear_model2.plot_best_h(Xpill, Yscore)

# Y_model1 = linear_model1.predict_(Xpill)
# Y_model2 = linear_model2.predict_(Xpill)

# print(linear_model1.cost_(Yscore, Y_model1))
# 57.60304285714282
# print(mean_squared_error(Yscore, Y_model1))
# 57.603042857142825

# print(linear_model2.cost_(Yscore, Y_model2))
# 232.16344285714285
# print(mean_squared_error(Yscore, Y_model2))
# 232.16344285714285
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(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))
示例#10
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')
示例#11
0
from sklearn.metrics import mean_squared_error
from my_linear_regression import MyLinearRegression as MyLR


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')


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")
示例#12
0
import numpy as np
from my_linear_regression import MyLinearRegression as MyLR

if __name__ == "__main__":
	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("# Example 0:")
	print(mylr.predict(X))
	print("# Output:")
	print("array([[8.], [48.], [323.]])")
	print()

	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:")
示例#13
0
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error
from my_linear_regression import MyLinearRegression as MyLR
import matplotlib.pyplot as plt
from polynomial_model import add_polynomial_features

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


def continuous_plot(x, y, i, lr):
    # Build the model:
    # Plot:
    ## To get a smooth curve, we need a lot of data points
    continuous_x = np.arange(1, 7.01, 0.01).reshape(-1, 1)
    x_ = add_polynomial_features(continuous_x, i)
    y_hat = lr.predict(x_)
    print(x.shape, y.shape)
    plt.scatter(x.T[0], y)
    plt.plot(continuous_x, y_hat, color='orange')
    plt.show()


cost = []

x = add_polynomial_features(Xpill, 10)
big_theta = [[2.03333758e-06], [4.76503382e-06], [1.29939248e-05],
示例#14
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()
示例#15
0
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error
from my_linear_regression import MyLinearRegression as MyLR
import matplotlib.pyplot as plt

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)

# print("Me: ", linear_model1.mse_(Yscore, Y_model1))
# print("Sc: ", mean_squared_error(Yscore, Y_model1))
# print()
#
# print("Me: ", linear_model2.mse_(Yscore, Y_model2))
# print("Sc: ", mean_squared_error(Yscore, Y_model2))


def plot(x, y, theta):
    """Plot the data and prediction line from three non-empty numpy.ndarray.
	Args:
	x: has to be an numpy.ndarray, a vector of dimension m * 1.
	y: has to be an numpy.ndarray, a vector of dimension m * 1.
	theta: has to be an numpy.ndarray, a vector of dimension 2 * 1.
	Returns:
	Nothing.
	Raises:
from matplotlib import pyplot as plt
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')
示例#17
0
import numpy as np
from my_linear_regression import MyLinearRegression as MyLR

if __name__ == "__main__":
    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]])
    theta = np.array([2, 0.7])

    x = x.reshape(len(x), 1)
    y = y.reshape(len(y), 1)
    theta = theta.reshape(len(theta), 1)

    lr1 = MyLR(theta)

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

    # Example 0.1:
    print(lr1.cost_elem_(lr1.predict_(x), y))
    # Output: