• 封装GridSearchCV的训练包


    import xgboost as xgb
    from sklearn.model_selection import GridSearchCV
    from sklearn.metrics import make_scorer
    from sklearn.metrics import accuracy_score
    from sklearn.datasets import load_breast_cancer  
    from sklearn.model_selection import train_test_split
    from sklearn.model_selection import ParameterGrid
    from sklearn.model_selection import ParameterSampler
    from scipy.stats.distributions import expon
    import numpy as np
    
    
    
    #从给定分布中生成采样参数
    np.random.seed(0)
    param = {"a":[1,2],"b":expon()}
    param = list(ParameterSampler(param,n_iter=4))
    
    
    
    ##################################################################################
    
    
    def get_model_GridSearchCV(estimator,parameters,X_train,y_train,scoring,cv=5): 
        """
        return:返回训练过的最好模型
        """
        
        #refit:Refit an estimator using the best found parameters on the whole dataset.
        model = GridSearchCV(estimator=estimator,param_grid=parameters,scoring=scoring,cv=5,refit=True)    
        
        model.fit(X_train, y_train)
        
        #打印结果
        print("best score in GridSearchCV:
    ",model.best_score_)
        print("best param in GridSearchCV:
    ",model.best_params_)     
        
        return model.best_estimator_
    
    
    #########################################测试########################################
    
    X,y = load_breast_cancer(return_X_y=True)  
    
    #分隔训练集和测试集
    X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=0,stratify=y)
    
    #配置参数
    param = [{
             "learning_rate":[0.1,0.3,0.6],
             "max_depth":[5,6,7],
             "n_estimators":[100,200,300], 
            }]
    
    
    #scoring = make_scorer(accuracy_score, greater_is_better=True)
    
    estimator = xgb.XGBClassifier(objective="reg:logistic")
    
    #训练模型
    model = get_model_GridSearchCV(estimator=estimator,
                                   parameters=param,
                                   cv=5,
                                   X_train=X_train,
                                   y_train=y_train,
                                   scoring="roc_auc")
    
    #采用训练得模型做测试
    """
    decision_function(*args, **kwargs)    Call decision_function on the estimator with the best found parameters.
    fit(X[, y, groups])    Run fit with all sets of parameters.
    get_params([deep])    Get parameters for this estimator.
    inverse_transform(*args, **kwargs)    Call inverse_transform on the estimator with the best found params.
    predict(*args, **kwargs)    Call predict on the estimator with the best found parameters.
    predict_log_proba(*args, **kwargs)    Call predict_log_proba on the estimator with the best found parameters.
    predict_proba(*args, **kwargs)    Call predict_proba on the estimator with the best found parameters.
    score(X[, y])    Returns the score on the given data, if the estimator has been refit.
    set_params(**params)    Set the parameters of this estimator.
    transform(*args, **kwargs)    Call transform on the estimator with the best found parameters.
    """
    y_pred = model.predict(X_test)
    
    #模型评价
    print(accuracy_score(y_test,y_pred))
  • 相关阅读:
    web-----------HTTP协议
    python基础作业------模拟实现一个ATM + 购物商城程序
    python--------进程与线程
    作业--用户输入数字0-100,判断成绩,用函数
    blog真正的首页
    blog首页视图
    让django完成翻译,迁移数据库模型
    创建Django博客的数据库模型
    创建blog APP
    在PyCharm上创建Django项目
  • 原文地址:https://www.cnblogs.com/wzdLY/p/9840192.html
Copyright © 2020-2023  润新知