美文网首页
使用交叉验证法和网格搜索进行模型评估代码实践

使用交叉验证法和网格搜索进行模型评估代码实践

作者: 万州客 | 来源:发表于2022-05-01 12:59 被阅读0次

    坚持 ,坚持

    一,代码

    from sklearn.datasets import load_wine
    from sklearn.model_selection import cross_val_score
    from sklearn.model_selection import ShuffleSplit
    from sklearn.model_selection import LeaveOneOut
    from sklearn.svm import SVC
    
    wine = load_wine()
    svc = SVC(kernel='linear')
    # scores = cross_val_score(svc, wine.data, wine.target, cv=6)
    # print('交叉验证得分:{}'.format(scores))
    # print('交叉验证平均分:{}'.format(scores.mean()))
    
    # shuffle_split = ShuffleSplit(test_size=.2, train_size=.7, n_splits=10)
    # scores = cross_val_score(svc, wine.data, wine.target, cv=shuffle_split)
    # print('随机拆分交叉验证模型得分:{}'.format(scores))
    
    cv = LeaveOneOut()
    scores = cross_val_score(svc, wine.data, wine.target, cv=cv)
    print('迭代次数:{}'.format(scores))
    print('模型平均分:{}'.format(scores.mean()))
    
    import numpy as np
    from sklearn.datasets import load_wine
    from sklearn.model_selection import cross_val_score
    from sklearn.model_selection import GridSearchCV
    from sklearn.linear_model import Lasso
    from sklearn.model_selection import train_test_split
    from sklearn.svm import SVC
    
    wine = load_wine()
    X_train, X_test, y_train, y_test = train_test_split(
        wine.data, wine.target, random_state=0
    )
    best_score = 0
    for alpha in [0.01, 0.1, 1.0, 10.0]:
        for max_iter in [100, 1000, 5000, 10000]:
            lasso = Lasso(alpha=alpha, max_iter=max_iter)
            scores = cross_val_score(lasso, X_train, y_train, cv=6)
            # lasso.fit(X_train, y_train)
            # score = lasso.score(X_test, y_test)
            score = np.mean(scores)
            if score > best_score:
                best_score = score
                best_parameters = {'alpha': alpha, '最大迭代次数': max_iter}
    print('模型最高分为:{:.3f}'.format(best_score))
    print('最佳参数设置:{}'.format(best_parameters))
    
    params = {'alpha': [0.01, 0.1, 1.0, 10.0],
              'max_iter': [100, 1000, 5000, 10000]}
    grid_search = GridSearchCV(lasso, params, cv=6)
    grid_search.fit(X_train, y_train)
    print('模型最高分为:{:.3f}'.format(grid_search.score(X_test, y_test)))
    print('最佳参数设置:{}'.format(grid_search.best_params_))
    
    

    二,效果

    C:\Users\ccc\AppData\Local\Programs\Python\Python38\python.exe D:/Code/Metis-Org/app/service/time_series_detector/algorithm/ai_test.py
    迭代次数:[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
     1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
     1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1.
     1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.
     1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
     1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1.
     1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
     1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
    模型平均分:0.9550561797752809
    
    Process finished with exit code 0
    
    C:\Users\ccc\AppData\Local\Programs\Python\Python38\python.exe D:/Code/Metis-Org/app/service/time_series_detector/algorithm/ai_test.py
    模型最高分为:0.865
    最佳参数设置:{'alpha': 0.01, '最大迭代次数': 100}
    模型最高分为:0.819
    最佳参数设置:{'alpha': 0.01, 'max_iter': 100}
    
    Process finished with exit code 0
    

    相关文章

      网友评论

          本文标题:使用交叉验证法和网格搜索进行模型评估代码实践

          本文链接:https://www.haomeiwen.com/subject/hozlyrtx.html