from scipy.stats import loguniform import sklearn from sklearn.datasets import load_iris from sklearn.metrics import accuracy_score from sklearn.model_selection import RandomizedSearchCV, StratifiedKFold, train_test_split from sklearn.pipeline import make_pipeline from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC iris = load_iris() X_train, X_test, y_train, y_test = train_test_split( iris.data, iris.target, test_size=0.25, stratify=iris.target, random_state=42, ) model = make_pipeline( StandardScaler(), SVC(), ) param_distributions = { "svc__C": loguniform(1e-2, 1e2), "svc__gamma": loguniform(1e-4, 1e0), "svc__kernel": ["rbf"], } cv = StratifiedKFold( n_splits=5, shuffle=True, random_state=42, ) search = RandomizedSearchCV( estimator=model, param_distributions=param_distributions, n_iter=8, scoring="accuracy", cv=cv, random_state=42, refit=True, ) search.fit(X_train, y_train) best_params = search.best_params_ best_params_display = { "svc__C": round(float(best_params["svc__C"]), 4), "svc__gamma": round(float(best_params["svc__gamma"]), 4), "svc__kernel": best_params["svc__kernel"], } test_predictions = search.predict(X_test) test_accuracy = accuracy_score(y_test, test_predictions) print(f"scikit-learn {sklearn.__version__}") print(f"candidate settings tried: {len(search.cv_results_['params'])}") print(f"cross-validation folds: {search.n_splits_}") print(f"best params: {best_params_display}") print(f"best CV accuracy: {search.best_score_:.3f}") print(f"held-out accuracy: {test_accuracy:.3f}")