Пример #1
0
y = cancer.target

X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.2,
                                                    random_state=42)

model = lgb.LGBMClassifier()
param_dists = {
    "n_estimators": [400, 700, 1000],
    "colsample_bytree": [0.7, 0.8],
    "max_depth": [15, 20, 25],
    "num_leaves": [50, 100, 200],
    "reg_alpha": [1.1, 1.2, 1.3],
    "reg_lambda": [1.1, 1.2, 1.3],
    "min_split_gain": [0.3, 0.4],
    "subsample": [0.7, 0.8, 0.9],
    "subsample_freq": [20]
}

gs = TuneSearchCV(model, param_dists, n_trials=5, scoring="accuracy")
gs.fit(X_train, y_train)
print(gs.cv_results_)

pred = gs.predict(X_test)
correct = 0
for i in range(len(y_test)):
    if pred[i] == y_test[i]:
        correct += 1
print("Accuracy:", correct / len(pred))
Пример #2
0
from tune_sklearn import TuneSearchCV
from sklearn.linear_model import SGDClassifier
from sklearn import datasets
from sklearn.model_selection import train_test_split
from ray.tune.schedulers import MedianStoppingRule
import numpy as np

digits = datasets.load_digits()
x = digits.data
y = digits.target
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.2)

clf = SGDClassifier()
parameter_grid = {"alpha": (1e-4, 1), "epsilon": (0.01, 0.1)}

scheduler = MedianStoppingRule(grace_period=10.0)

tune_search = TuneSearchCV(clf,
                           parameter_grid,
                           search_optimization="bayesian",
                           n_iter=3,
                           early_stopping=scheduler,
                           max_iters=10)
tune_search.fit(x_train, y_train)

pred = tune_search.predict(x_test)
accuracy = np.count_nonzero(np.array(pred) == np.array(y_test)) / len(pred)
print(accuracy)
Пример #3
0
                           verbose=1)
tune_search.fit(X_train, y_train, sample_weight=sample_weights)

# bayesian search
tune_search = TuneSearchCV(rf,
                           param_random,
                           search_optimization='bayesian',
                           max_iters=100,
                           scoring='accuracy',
                           n_jobs=12,
                           cv=cv,
                           verbose=1)
tune_search.fit(X_train, y_train, sample_weight=sample_weights)

# scores
clf_predictions = tune_search.predict(X_test)
tune_search.best_params_
print(tune_search.cv_results_)
tune_search.cv_results_['mean_test_score'].mean()
tune_search.best_score_

# model scores
clf_f1_score = sklearn.metrics.f1_score(y_test, clf_predictions)
clf_accuracy_score = sklearn.metrics.accuracy_score(y_test, clf_predictions)
print(f'f1_score: {clf_f1_score}')
# print(f'optimal_max_depth: {max_depth}')
# print(f'optimal_n_features: {n_features}')
# print(f'optimal_max_leaf_nodes {max_leaf_nodes}')
# print(f'optimal_n_estimators {n_estimators}')
# save_id = f'{max_depth}{n_features}{max_leaf_nodes}{n_estimators}{str(clf_f1_score)[2:6]}'