Ejemplo n.º 1
0
def main():
    from sklearn.neighbors import KNeighborsRegressor

    # Load generated data
    X_train, X_test, y_train, y_test = bench.load_data(params)
    params.n_classes = len(np.unique(y_train))

    # Create a regression object
    knn_regr = KNeighborsRegressor(n_neighbors=params.n_neighbors,
                                   weights=params.weights,
                                   algorithm=params.method,
                                   metric=params.metric,
                                   n_jobs=params.n_jobs)

    # Measure time and accuracy on fitting
    train_time, _ = bench.measure_function_time(
        knn_regr.fit, X_train, y_train, params=params)
    if params.task == 'regression':
        y_pred = knn_regr.predict(X_train)
        train_rmse = bench.rmse_score(y_train, y_pred)
        train_r2 = bench.r2_score(y_train, y_pred)

    # Measure time and accuracy on prediction
    if params.task == 'regression':
        predict_time, yp = bench.measure_function_time(knn_regr.predict, X_test,
                                                       params=params)
        test_rmse = bench.rmse_score(y_test, yp)
        test_r2 = bench.r2_score(y_test, yp)
    else:
        predict_time, _ = bench.measure_function_time(knn_regr.kneighbors, X_test,
                                                      params=params)

    if params.task == 'regression':
        bench.print_output(
            library='sklearn',
            algorithm=knn_regr._fit_method + '_knn_regr',
            stages=['training', 'prediction'],
            params=params,
            functions=['knn_regr.fit', 'knn_regr.predict'],
            times=[train_time, predict_time],
            metric_type=['rmse', 'r2_score'],
            metrics=[[train_rmse, test_rmse], [train_r2, test_r2]],
            data=[X_train, X_test],
            alg_instance=knn_regr,
        )
    else:
        bench.print_output(
            library='sklearn',
            algorithm=knn_regr._fit_method + '_knn_search',
            stages=['training', 'search'],
            params=params,
            functions=['knn_regr.fit', 'knn_regr.kneighbors'],
            times=[train_time, predict_time],
            metric_type=None,
            metrics=[],
            data=[X_train, X_test],
            alg_instance=knn_regr,
        )
Ejemplo n.º 2
0
def main():
    from sklearn.svm import NuSVR

    X_train, X_test, y_train, y_test = bench.load_data(params)
    y_train = np.asfortranarray(y_train).ravel()

    if params.gamma is None:
        params.gamma = 1.0 / X_train.shape[1]

    cache_size_bytes = bench.get_optimal_cache_size(
        X_train.shape[0], max_cache=params.max_cache_size)
    params.cache_size_mb = cache_size_bytes / 1024**2
    params.n_classes = len(np.unique(y_train))

    regr = NuSVR(C=params.C,
                 nu=params.nu,
                 kernel=params.kernel,
                 cache_size=params.cache_size_mb,
                 tol=params.tol,
                 gamma=params.gamma,
                 degree=params.degree)

    fit_time, _ = bench.measure_function_time(regr.fit,
                                              X_train,
                                              y_train,
                                              params=params)
    params.sv_len = regr.support_.shape[0]

    predict_train_time, y_pred = bench.measure_function_time(regr.predict,
                                                             X_train,
                                                             params=params)
    train_rmse = bench.rmse_score(y_train, y_pred)
    train_r2 = bench.r2_score(y_train, y_pred)

    _, y_pred = bench.measure_function_time(regr.predict,
                                            X_test,
                                            params=params)
    test_rmse = bench.rmse_score(y_test, y_pred)
    test_r2 = bench.r2_score(y_test, y_pred)

    bench.print_output(
        library='sklearn',
        algorithm='nuSVR',
        stages=['training', 'prediction'],
        params=params,
        functions=['NuSVR.fit', 'NuSVR.predict'],
        times=[fit_time, predict_train_time],
        metric_type=['rmse', 'r2_score', 'n_sv'],
        metrics=[
            [train_rmse, test_rmse],
            [train_r2, test_r2],
            [int(regr.n_support_.sum()),
             int(regr.n_support_.sum())],
        ],
        data=[X_train, X_train],
        alg_instance=regr,
    )
Ejemplo n.º 3
0
def main():
    from sklearn.ensemble import RandomForestRegressor

    # Load and convert data
    X_train, X_test, y_train, y_test = bench.load_data(params)
    y_train = y_train.values.ravel()
    y_test = y_test.values.ravel()

    # Create our random forest regressor
    regr = RandomForestRegressor(
        criterion=params.criterion,
        n_estimators=params.num_trees,
        max_depth=params.max_depth,
        max_features=params.max_features,
        min_samples_split=params.min_samples_split,
        max_leaf_nodes=params.max_leaf_nodes,
        min_impurity_decrease=params.min_impurity_decrease,
        bootstrap=params.bootstrap,
        random_state=params.seed,
        n_jobs=params.n_jobs)

    fit_time, _ = bench.measure_function_time(regr.fit,
                                              X_train,
                                              y_train,
                                              params=params)

    y_pred = regr.predict(X_train)
    train_rmse = bench.rmse_score(y_train, y_pred)
    train_r2 = bench.r2_score(y_train, y_pred)

    predict_time, y_pred = bench.measure_function_time(regr.predict,
                                                       X_test,
                                                       params=params)
    test_rmse = bench.rmse_score(y_test, y_pred)
    test_r2 = bench.r2_score(y_test, y_pred)

    bench.print_output(
        library='sklearn',
        algorithm='df_regr',
        stages=['training', 'prediction'],
        params=params,
        functions=['df_regr.fit', 'df_regr.predict'],
        times=[fit_time, predict_time],
        metric_type=['rmse', 'r2_score'],
        metrics=[[train_rmse, test_rmse], [train_r2, test_r2]],
        data=[X_train, X_test],
        alg_instance=regr,
    )
Ejemplo n.º 4
0
def main():
    from sklearn.linear_model import ElasticNet

    # Load data
    X_train, X_test, y_train, y_test = bench.load_data(params)

    # Create our regression object
    regr = ElasticNet(fit_intercept=params.fit_intercept,
                      l1_ratio=params.l1_ratio,
                      alpha=params.alpha,
                      tol=params.tol,
                      max_iter=params.maxiter,
                      copy_X=False)
    # Time fit
    fit_time, _ = bench.measure_function_time(regr.fit,
                                              X_train,
                                              y_train,
                                              params=params)

    # Time predict
    predict_time, y_pred = bench.measure_function_time(regr.predict,
                                                       X_train,
                                                       params=params)

    train_rmse = bench.rmse_score(y_train, y_pred)
    train_r2 = bench.r2_score(y_train, y_pred)
    y_pred = regr.predict(X_test)
    test_rmse = bench.rmse_score(y_test, y_pred)
    test_r2 = bench.r2_score(y_test, y_pred)

    bench.print_output(
        library='sklearn',
        algorithm='elasticnet',
        stages=['training', 'prediction'],
        params=params,
        functions=['ElasticNet.fit', 'ElasticNet.predict'],
        times=[fit_time, predict_time],
        metric_type=['rmse', 'r2_score', 'iter'],
        metrics=[
            [train_rmse, test_rmse],
            [train_r2, test_r2],
            [int(regr.n_iter_), int(regr.n_iter_)],
        ],
        data=[X_train, X_train],
        alg_instance=regr,
    )
Ejemplo n.º 5
0
def main():
    from sklearn.linear_model import Lasso

    # Load data
    X_train, X_test, y_train, y_test = bench.load_data(params)

    # Create our regression object
    regr = Lasso(fit_intercept=params.fit_intercept,
                 alpha=params.alpha,
                 tol=params.tol,
                 max_iter=params.maxiter,
                 copy_X=False)

    # Time fit
    fit_time, _ = bench.measure_function_time(regr.fit,
                                              X_train,
                                              y_train,
                                              params=params)

    # Time predict
    predict_time, yp = bench.measure_function_time(regr.predict,
                                                   X_train,
                                                   params=params)

    train_rmse = bench.rmse_score(y_train, yp)
    train_r2 = bench.r2_score(y_train, yp)
    yp = regr.predict(X_test)
    test_rmse = bench.rmse_score(y_test, yp)
    test_r2 = bench.r2_score(y_test, yp)

    bench.print_output(
        library='sklearn',
        algorithm='lasso',
        stages=['training', 'prediction'],
        params=params,
        functions=['Lasso.fit', 'Lasso.predict'],
        times=[fit_time, predict_time],
        metric_type=['rmse', 'r2_score'],
        metrics=[[train_rmse, test_rmse], [train_r2, test_r2]],
        data=[X_train, X_test],
        alg_instance=regr,
    )
Ejemplo n.º 6
0
def main():
    from sklearn.linear_model import LinearRegression

    # Load data
    X_train, X_test, y_train, y_test = bench.load_data(
        params, generated_data=['X_train', 'y_train'])

    # Create our regression object
    regr = LinearRegression(fit_intercept=params.fit_intercept,
                            n_jobs=params.n_jobs,
                            copy_X=False)

    # Time fit
    fit_time, _ = bench.measure_function_time(regr.fit,
                                              X_train,
                                              y_train,
                                              params=params)

    # Time predict
    predict_time, yp = bench.measure_function_time(regr.predict,
                                                   X_test,
                                                   params=params)

    test_rmse = bench.rmse_score(y_test, yp)
    test_r2 = bench.r2_score(y_test, yp)
    yp = regr.predict(X_train)
    train_rmse = bench.rmse_score(y_train, yp)
    train_r2 = bench.r2_score(y_train, yp)

    bench.print_output(
        library='sklearn',
        algorithm='lin_reg',
        stages=['training', 'prediction'],
        params=params,
        functions=['Linear.fit', 'Linear.predict'],
        times=[fit_time, predict_time],
        metric_type=['rmse', 'r2_score'],
        metrics=[[train_rmse, test_rmse], [train_r2, test_r2]],
        data=[X_train, X_test],
        alg_instance=regr,
    )