模型调参(机器学习)

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.svm import SVC
import optuna
from sklearn.datasets import load_iris
import pandas as pd
x = pd.DataFrame(load_iris().data, columns=load_iris().feature_names)
y = load_iris().target


'''
optuna根据需要,支持多种不同的搜索方式(含有low和high值的都是左闭右开):
对待搜索的超参数指定一个取值空间,通过trial参数来实现:
trial.suggest_int(name,value1,value2):为超参数指定整型取值
trial.suggest_float(name,value1,value2):为超参数指定浮点型取值
trial.suggest_categorical(name,value1,value2):为超参数指定类型取值
trial.suggest_uniform(name,value1,value2):为超参数指定取值空间,且对每个值进行相同采样
trial.suggest_loguniform(name,value1,value2):为超参数指定取值空间,且在对数域对每个值进行相同采样
trial.suggest_discrete_uniform(name,value1,value2):为超参数指定取值空间,且对每个值以相同可能采样
'''

def objective(trial, x, y):
    X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.5)
    param = {
        "C": trial.suggest_float("C", 0.1, 1),
        "degree": trial.suggest_int("degree", 1,4),
        "gamma": trial.suggest_categorical("gamma", ['scale', 'auto']),
        "kernel": trial.suggest_categorical("kernel", ['linear', 'poly', 'rbf', 'sigmoid']),
    }
    reg = SVC(**param)
    reg.fit(X_train, y_train)
    y_pred = reg.predict(X_test)
    score = accuracy_score(y_test, y_pred)
    return score  # 必须要有一个评价指标返回

# 设定学习规律
study = optuna.create_study(direction="maximize")
func = lambda trial: objective(trial, x, y)
# 编译执行
study.optimize(func)
study.best_trial.params #最优参数

你可能感兴趣的:(模型调参(机器学习))