Пример #1
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,
    )
Пример #2
0
                    default=1e-3,
                    help='Tolerance passed to sklearn.svm.SVC')
parser.add_argument('--probability',
                    action='store_true',
                    default=False,
                    dest='probability',
                    help="Use probability for SVC")

params = bench.parse_args(parser)

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

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 = y_train[y_train.columns[0]].nunique()

clf = SVC(C=params.C,
          kernel=params.kernel,
          cache_size=params.cache_size_mb,
          tol=params.tol,
          gamma=params.gamma,
          probability=params.probability,
          degree=params.degree)

fit_time, _ = bench.measure_function_time(clf.fit,
                                          X_train,
                                          y_train,
                                          params=params)
Пример #3
0
def main():
    from sklearn.svm import SVC

    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))

    clf = SVC(C=params.C,
              kernel=params.kernel,
              cache_size=params.cache_size_mb,
              tol=params.tol,
              gamma=params.gamma,
              probability=params.probability,
              random_state=43,
              degree=params.degree)

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

    if params.probability:
        state_predict = 'predict_proba'
        clf_predict = clf.predict_proba
        train_acc = None
        test_acc = None

        predict_train_time, y_pred = bench.measure_function_time(clf_predict,
                                                                 X_train,
                                                                 params=params)
        train_log_loss = bench.log_loss(y_train, y_pred)
        train_roc_auc = bench.roc_auc_score(y_train, y_pred)

        _, y_pred = bench.measure_function_time(clf_predict,
                                                X_test,
                                                params=params)
        test_log_loss = bench.log_loss(y_test, y_pred)
        test_roc_auc = bench.roc_auc_score(y_test, y_pred)
    else:
        state_predict = 'prediction'
        clf_predict = clf.predict
        train_log_loss = None
        test_log_loss = None
        train_roc_auc = None
        test_roc_auc = None

        predict_train_time, y_pred = bench.measure_function_time(clf_predict,
                                                                 X_train,
                                                                 params=params)
        train_acc = bench.accuracy_score(y_train, y_pred)

        _, y_pred = bench.measure_function_time(clf_predict,
                                                X_test,
                                                params=params)
        test_acc = bench.accuracy_score(y_test, y_pred)

    bench.print_output(
        library='sklearn',
        algorithm='SVC',
        stages=['training', state_predict],
        params=params,
        functions=['SVM.fit', f'SVM.{state_predict}'],
        times=[fit_time, predict_train_time],
        metric_type=['accuracy', 'log_loss', 'roc_auc', 'n_sv'],
        metrics=[
            [train_acc, test_acc],
            [train_log_loss, test_log_loss],
            [train_roc_auc, test_roc_auc],
            [int(clf.n_support_.sum()),
             int(clf.n_support_.sum())],
        ],
        data=[X_train, X_train],
        alg_instance=clf,
    )
Пример #4
0
def main():
    from sklearn.svm import SVC

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

    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))

    clf = SVC(C=params.C,
              kernel=params.kernel,
              cache_size=params.cache_size_mb,
              tol=params.tol,
              gamma=params.gamma,
              probability=params.probability,
              random_state=43)

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

    if params.probability:
        state_predict = 'predict_proba'
        accuracy_type = 'log_loss'

        def metric_call(x, y):
            return bench.log_loss(x, y)

        clf_predict = clf.predict_proba
    else:
        state_predict = 'predict'
        accuracy_type = 'accuracy[%]'

        def metric_call(x, y):
            return bench.accuracy_score(x, y)

        clf_predict = clf.predict

    predict_train_time, y_pred = bench.measure_function_time(clf_predict,
                                                             X_train,
                                                             params=params)
    train_acc = metric_call(y_train, y_pred)

    predict_test_time, y_pred = bench.measure_function_time(clf_predict,
                                                            X_test,
                                                            params=params)
    test_acc = metric_call(y_test, y_pred)

    bench.print_output(library='sklearn',
                       algorithm='svc',
                       stages=['training', state_predict],
                       params=params,
                       functions=['SVM.fit', f'SVM.{state_predict}'],
                       times=[fit_time, predict_train_time],
                       accuracy_type=accuracy_type,
                       accuracies=[train_acc, test_acc],
                       data=[X_train, X_train],
                       alg_instance=clf)