Ejemplo n.º 1
0
def finish_part_c(train_X,
                  train_y,
                  test_X,
                  test_y,
                  data_type,
                  seed_value,
                  verbose=True,
                  to_return=False):
    """
       Finishes the part(c) of the homework5 of EE559.
    :param train_X: feature matrix for training data.
    :param train_y: label vector for training data.
    :param test_X: feature matrix for test data.
    :param test_y: label vector for test data.
    :param data_type: "unnormalized" or "standardized".
    :param seed_value: value to control starting weight vectors.
    :param verbose: whether to print information.
    :param to_return: whether to return accuracy value or not.
    Outputs the requried information.
    """
    perceptron_model = Perceptron(max_iter=10000, tol=None, random_state=0)
    np.random.seed(seed_value)
    coef = np.random.rand(len(np.unique(train_y)), train_X.shape[1])
    intercept = np.random.rand(len(np.unique(train_y)), 1)
    perceptron_OvR = OneVsRestClassifier(estimator=perceptron_model)
    perceptron_OvR.coef_ = coef  # Initialize coefficients with the specific seed value
    perceptron_OvR.intercept_ = intercept  # Initialize intercepts with the specific seed value
    perceptron_OvR.fit(train_X, train_y)
    train_y_pred = perceptron_OvR.predict(train_X)
    test_y_pred = perceptron_OvR.predict(test_X)
    if verbose:
        print("The weights for each feature for " + data_type + " data",
              perceptron_OvR.coef_)
        print("The constants for each feature for " + data_type + " data",
              perceptron_OvR.intercept_)

    if to_return:
        return get_accuracy(train_y,
                            train_y_pred,
                            log_word=data_type + " training data",
                            to_return=to_return)
    else:
        get_accuracy(train_y,
                     train_y_pred,
                     log_word=data_type + " training data")
        get_accuracy(test_y, test_y_pred, log_word=data_type + " test data")
        print(" ")