Esempio n. 1
0
    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)


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

    if False:
        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("model 1")  # 57.603042857142825
        print(linear_model1.mse_(Yscore, Y_model1))
        print(mean_squared_error(Yscore, Y_model1))

        print("\nmodel 2")  # 232.16344285714285
        print(linear_model2.mse_(Yscore, Y_model2))
        print(mean_squared_error(Yscore, Y_model2))

    plot2graphs(Xpill, Yscore)
Esempio n. 2
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()
#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
Esempio n. 4
0
def cost_(y_hat, y):
    j_elem = cost_elem_(y_hat, y)
    return np.sum(j_elem)


if __name__ == "__main__":
    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.mse_(Xpill, Yscore))
    print(mean_squared_error(Yscore, Y_model1))
    print(linear_model2.mse_(Xpill, Yscore))
    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)
Esempio n. 5
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()
Esempio n. 6
0
 def mse_(self, y, y_hat):
     return MLR.mse_(self, y, y_hat)
Esempio n. 7
0
    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)

plt.bar(range(0, 10), cost_list)
Esempio n. 8
0
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)

print(Xpill)
print(Yscore)

linear_model1 = MyLR(np.array([[89.0], [-8]]))
linear_model2 = MyLR(np.array([[89.0], [-6]]))

# linear_model1.plot(Xpill, Yscore)
# linear_model2.plot(Xpill, Yscore)

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

print("mine: ", linear_model1.mse_(Yscore, Y_model1))  #MY
# 57.60304285714282

print("not mine: ", mean_squared_error(Yscore, Y_model1))
# 57.603042857142825

print("mine: ", linear_model2.mse_(Yscore, Y_model2))  # MY
# 232.16344285714285

print("not mine: ", mean_squared_error(Yscore, Y_model2))
# 232.16344285714285

# linear_model1.plot(Xpill, Y_model1)
# linear_model2.plot(Xpill, Y_model2)