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))
Ejemplo n.º 2
0
import pandas as pd
import numpy as np
from sklearn.metrics import mean_squared_error
from my_linear_regression import MyLinearRegression as MyLR


if __name__ == '__main__':
    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.plot(Xpill, Yscore, "o", Xpill,
                       Y_model1, "x--", Xpill, Y_model2, "b")
    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))