Ejemplo n.º 1
0
def main():
    X, y = read_dataset("../data/regression_example2.txt", ',')
    # 5 points into training set, 5 points into test set
    X_TRAIN = X[:int(X.shape[0] / 2), ]
    y_train = y[:int(X.shape[0] / 2), ]
    X_TEST = X[int(X.shape[0] / 2):, ]
    y_test = y[int(X.shape[0] / 2):, ]
    print("Reading-in ready")

    ########################################################
    # FIND REGRESSION POLYNOMIAL OF DEGREE 4 of form y = ax^4 + bx^3 + cx^2 + dx WITH REGULARIZER
    ########################################################

    # TRY TO CHANGE LAMBDA VALUE TO SOME OTHER NUMBER: TRY - [0, 1, 10, 30, 100, 10000]
    lmbda = 10000
    X_TRAIN_2 = X_TRAIN**2
    X_TRAIN_3 = X_TRAIN**3
    X_TRAIN_4 = X_TRAIN**4
    X_TRAIN_FINAL = np.concatenate((X_TRAIN_4, X_TRAIN_3, X_TRAIN_2, X_TRAIN),
                                   axis=1)
    w = find_weights_with_regularizer(X_TRAIN_FINAL, y_train, lmbda)

    xt_range = np.arange(0, 11, 0.5).reshape((-1, 1))
    xt_range_2 = xt_range**2
    xt_range_3 = xt_range**3
    xt_range_4 = xt_range**4
    xt_range_final = np.concatenate(
        (xt_range_4, xt_range_3, xt_range_2, xt_range), axis=1)
    regression_predictions = np.dot(xt_range_final, w)

    # TRAINING DATA and TEST DATA
    pl.scatter(X_TRAIN, y_train, s=20, color='blue')
    pl.scatter(X_TEST, y_test, s=20, color='orange')
    pl.plot(xt_range, regression_predictions, color='red')
    pl.title(
        "RIDGE REGRESSION $y = ax^4 + bx^3 + cx^2 + dx$ with $\lambda = $" +
        str(lmbda))
    pl.xlabel("x")
    pl.ylabel("y")
    pl.show()

    for i in range(5):
        pass
Ejemplo n.º 2
0
def main():
    X, y = read_dataset("../data/regression_example1.txt", ',')
    print("Reading-in ready")

    #####################################
    # FIND REGRESSION LINE of form y = ax^3 + bx^2 + cx + d
    #####################################

    x_range = []
    regression_predictions = []

    # YOUR CODE STARTS HERE

    # YOUR CODE ENDS HERE

    pl.scatter(X, y, s=10)
    pl.plot(x_range, regression_predictions, color='red')
    pl.title("Linear regression $y = ax^2 + bx^2 + cx + d$ fit to data")
    pl.xlabel("City population size, million")
    pl.ylabel("Hot-dog selling van profit, K")
    pl.show()
Ejemplo n.º 3
0
def main():
    X, y = read_dataset("../data/train.txt", ' ')
    T = np.loadtxt("../data/test.txt")
    print("Loading done")
Ejemplo n.º 4
0
def main():
    X, y = read_dataset("../data/regression_example1.txt", ',')
    print("Reading-in ready")

    # ONLY DATA
    # draw data with labels
    pl.scatter(X, y, s=10)
    pl.title("Data")
    pl.xlabel("City population size, million")
    pl.ylabel("Hot-dog selling van profit, K")
    pl.show()

    #####################################
    # FIND REGRESSION LINE of form y = ax
    #####################################
    w = find_weights(X, y)

    # we found line parameters, now draw line
    x_range = np.arange(5, 23, 0.5).reshape((-1, 1))
    regression_predictions = np.dot(x_range, w)

    pl.scatter(X, y, s=10)
    pl.plot(x_range, regression_predictions, color='red')
    pl.title("Linear regression $y = ax$ fit to data")
    pl.xlabel("City population size, million")
    pl.ylabel("Hot-dog selling van profit, K")
    pl.show()

    #########################################
    # FIND REGRESSION LINE of form y = ax + b
    #########################################
    Xb = np.ones((X.shape[0], 1))
    Xb = np.concatenate((X, Xb), axis=1)
    w = find_weights(Xb, y)

    # we found line parameters, now draw line
    x_range = np.arange(5, 23, 0.5).reshape((-1, 1))
    x_range_b = np.ones((x_range.shape[0], 1))
    x_range_b = np.concatenate((x_range, x_range_b), axis=1)
    regression_predictions = np.dot(x_range_b, w)

    pl.scatter(X, y, s=10)
    pl.plot(x_range, regression_predictions, color='red')
    pl.title("Linear regression $y = ax + b$ fit to data")
    pl.xlabel("City population size, million")
    pl.ylabel("Hot-dog selling van profit, K")
    pl.show()

    ########################################################
    # FIND REGRESSION POLYNOMIAL OF DEGREE 2 of form y = ax^2
    ########################################################
    X2_2 = X**2
    X2 = np.concatenate((X2_2, X), axis=1)
    w = find_weights(X2, y)

    x_range = np.arange(5, 23, 0.5).reshape((-1, 1))
    x_range_2_2 = x_range**2
    x_range_2 = np.concatenate((x_range_2_2, x_range), axis=1)
    regression_predictions = np.dot(x_range_2, w)

    pl.scatter(X, y, s=10)
    pl.plot(x_range, regression_predictions, color='red')
    pl.title("Linear regression $y = ax^2$ fit to data")
    pl.xlabel("City population size, million")
    pl.ylabel("Hot-dog selling van profit, K")
    pl.show()

    ########################################################
    # FIND REGRESSION POLYNOMIAL OF DEGREE 4 of form y = ax^4 + bx^3 + cx^2 + dx
    ########################################################
    X4_2 = X**2
    X4_3 = X**3
    X4_4 = X**4
    X4 = np.concatenate((X4_4, X4_3, X4_2, X), axis=1)
    w = find_weights(X4, y)

    x_range = np.arange(5, 23, 0.5).reshape((-1, 1))
    x_range_4_2 = x_range**2
    x_range_4_3 = x_range**3
    x_range_4_4 = x_range**4
    x_range_4 = np.concatenate(
        (x_range_4_4, x_range_4_3, x_range_4_2, x_range), axis=1)
    regression_predictions = np.dot(x_range_4, w)

    pl.scatter(X, y, s=10)
    pl.plot(x_range, regression_predictions, color='red')
    pl.title("Linear regression $y = ax^4 + bx^3 + cx^2 + dx$ fit to data")
    pl.xlabel("City population size, million")
    pl.ylabel("Hot-dog selling van profit, K")
    pl.show()