Exemple #1
0
def support_vector_regressor(X_train, X_test, y_train, y_test):
    """

    """
    model = SVR()
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    if y_test is not None:
        return y_pred, \
               rsquared_score(y_test, y_pred), \
               rmse(y_test, y_pred), \
               mean_absolute_percentage_error(y_test, y_pred), \
               adj_rsquared_score(y_test, y_pred)
    else:
        return "There is no y_test to return the outputs", y_pred
Exemple #2
0
def knn_regressor(X_train, X_test, y_train, y_test, n_neighbors=None):
    """

    """
    model = KNeighborsRegressor(n_neighbors=n_neighbors)
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    if y_test is not None:
        return y_pred, \
               rsquared_score(y_test, y_pred), \
               rmse(y_test, y_pred), \
               mean_absolute_percentage_error(y_test, y_pred), \
               adj_rsquared_score(y_test, y_pred)
    else:
        return "There is no y_test to return the outputs", y_pred
Exemple #3
0
def linear_regression(X_train=None, X_test=None, y_train=None, y_test=None,
                      samples_size=None, number_of_independent_columns=None):
    """

    """
    lr = LinearRegression()
    lr_model = lr.fit(X_train, y_train)
    y_pred = lr_model.predict(X_test)
    if y_test is not None:
        return y_pred, \
               rsquared_score(y_test, y_pred), \
               rmse(y_test, y_pred), \
               mean_absolute_percentage_error(y_test, y_pred), \
               adj_rsquared_score(y_test, y_pred, n=samples_size,
                                  p=number_of_independent_columns)
    else:
        return "There is no y_test to return the outputs", y_pred
Exemple #4
0
def decision_tree_regressor(X_train=None, X_test=None, y_train=None, y_test=None,
                            max_depth=None, min_samples_leaf=None,
                            criterion=None, random_state=None):
    """

    """
    dtr = DecisionTreeRegressor(max_depth=max_depth,
                                min_samples_leaf=min_samples_leaf, criterion=criterion,
                                random_state=random_state)
    dtr_model = dtr.fit(X_train, y_train)
    y_pred = dtr_model.predict(X_test)
    if y_test is not None:
        return y_pred, \
               rsquared_score(y_test, y_pred), \
               rmse(y_test, y_pred), \
               mean_absolute_percentage_error(y_test, y_pred), \
               adj_rsquared_score(y_test, y_pred)
    else:
        return "There is no y_test to return the outputs", y_pred
Exemple #5
0
def random_forest_regression(X_train=None, X_test=None, y_train=None, y_test=None,
                             n_estimators=None, max_depth=None,
                             min_samples_leaf=None, criterion=None, random_state=None,
                             samples_size = None, number_of_independent_columns = None):
    """

    """
    random_forest = RandomForestRegressor(n_estimators=n_estimators, max_depth=max_depth,
                                  min_samples_leaf=min_samples_leaf, criterion=criterion,
                                  random_state=random_state)

    model = random_forest.fit(X_train, y_train)
    y_pred = model.predict(X_test)
    if y_test is not None:
        return y_pred, \
               rsquared_score(y_test, y_pred), \
               rmse(y_test, y_pred), \
               mean_absolute_percentage_error(y_test, y_pred), \
               adj_rsquared_score(y_test, y_pred,
                                  n=samples_size,
                                  p=number_of_independent_columns)
    else:
        return "There is no y_test to return the outputs", y_pred
Exemple #6
0
def evaluate(predicted,
             actual,
             method=None,
             figsize=None,
             ax=None,
             plots=True,
             reports=True,
             samples_size=None,
             number_of_independent_columns=None):
    """

    :param actual: Your y_true
    :param predicted:Your y_pred
    :param figsize:Figure size
    :param ax:axes
    :param plots:True if you want all plots and is true by default
    :param method: Classification or regression
    :param reports: True by default and if you want reports you need to leave it as True
    :param samples_size: Size of the sample and also to be used only for adj_r2_score
    :param number_of_independent_columns: Number of columns which are independent and can only for adj_r2_score
    :return: Returns all the classification metrics


    """

    # Classification

    if len(actual) != len(predicted):
        raise exceptions.MetricValueError(
            "The length of actual {0} and length of "
            "predicted {1} are not the same".format(len(actual),
                                                    len(predicted)))

    elif method == 'Classification':
        print(
            "\n---------------------\033[1mReports\033[0m-------------------------\n"
        )
        print("\033[1mAccuracy score:\033[0m {}".format(
            accuracy(actual, predicted)))
        print("\033[1mRecall score:\033[0m {}".format(recall(
            actual, predicted)))
        print("\033[1mPrecision score:\033[0m {}".format(
            precision(actual, predicted)))
        print("\033[1mF1 score:\033[0m {}".format(f1_score(actual, predicted)))
        print(
            "\n---------------------\033[1mPlots\033[0m-------------------------\n"
        )
        print("\033[1mClassification report:\033[0m: ")
        classification_report_plot(actual, predicted, figsize=figsize, ax=ax)
        print("\033[1mConfusion Matrix:\033[0m: ")
        confusion_matrix_plot(actual, predicted, figsize, ax)
        print("\033[1mROC curve:\033[0m ")
        roc_auc_curve(actual, predicted, figsize, ax)

        # Regression
    if method == 'Regression':
        actual = np.array(actual)
        predicted = np.array(predicted)

        print(
            "\n-----------------------\033[1mReports\033[0m------------------------\n"
        )
        print("\033[1mRMSE:\033[0m {}".format(rmse(actual, predicted)))
        print("\033[1mMAPE:\033[0m {}".format(
            mean_absolute_percentage_error(actual, predicted)))
        print("\033[1mR2 score:\033[0m {}".format(
            rsquared_score(actual, predicted)))
        print("\033[1mAdj R2 score:\033[0m {}".format(
            adj_rsquared_score(actual, predicted, samples_size,
                               number_of_independent_columns)))

        print(
            "\n---------------------\033[1mPlots\033[0m-------------------------\n"
        )
        print("\033[1mRegression Plot 1:\033[0m ")
        regression_line(actual, predicted)
        print("\033[1mRegression Plot 2:\033[0m ")
        sns_reg_plot(actual, predicted)