吴裕雄 python 机器学习——模型选择参数优化暴力搜索寻优GridSearchCV模型

import scipy

from sklearn.datasets import load_digits
from sklearn.metrics import classification_report
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV,RandomizedSearchCV

#模型选择参数优化暴力搜索寻优GridSearchCV模型
def test_GridSearchCV():
    ‘‘‘
    测试 GridSearchCV 的用法。使用 LogisticRegression 作为分类器,主要优化 C、penalty、multi_class 等参数
    ‘‘‘
    ### 加载数据
    digits = load_digits()
    X_train,X_test,y_train,y_test=train_test_split(digits.data, digits.target,test_size=0.25,random_state=0,stratify=digits.target)
    #### 参数优化 ######
    tuned_parameters = [{‘penalty‘: [‘l1‘,‘l2‘],
                        ‘C‘: [0.01,0.05,0.1,0.5,1,5,10,50,100],
                        ‘solver‘:[‘liblinear‘],
                        ‘multi_class‘: [‘ovr‘]},
                        {‘penalty‘: [‘l2‘],
                        ‘C‘: [0.01,0.05,0.1,0.5,1,5,10,50,100],
                         ‘solver‘:[‘lbfgs‘],
                        ‘multi_class‘: [‘ovr‘,‘multinomial‘]},
                        ]
    clf=GridSearchCV(LogisticRegression(tol=1e-6),tuned_parameters,cv=10)
    clf.fit(X_train,y_train)
    print("Best parameters set found:",clf.best_params_)
    print("Grid scores:")
#     for params, mean_train_score, mean_test_score in clf.cv_results_.params,cv_results_.mean_train_score,cv_results_.mean_test_score:
#         print("\t%0.3f (+/-%0.03f) for %s" % (mean_train_score, mean_test_score() * 2, params))
    print((clf.cv_results_["mean_train_score"], clf.cv_results_["mean_test_score"] * 2, clf.cv_results_["params"]))

    print("Optimized Score:",clf.score(X_test,y_test))
    print("Detailed classification report:")
    y_true, y_pred = y_test, clf.predict(X_test)
    print(classification_report(y_true, y_pred))

#调用test_GridSearchCV()
test_GridSearchCV()

原文地址:https://www.cnblogs.com/tszr/p/10802679.html

时间: 2024-10-08 14:20:16

吴裕雄 python 机器学习——模型选择参数优化暴力搜索寻优GridSearchCV模型的相关文章

吴裕雄 python 机器学习——模型选择参数优化随机搜索寻优RandomizedSearchCV模型

import scipy from sklearn.datasets import load_digits from sklearn.metrics import classification_report from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.model_selection import GridS

吴裕雄 python 机器学习——模型选择分类问题性能度量

import numpy as np import matplotlib.pyplot as plt from sklearn.svm import SVC from sklearn.datasets import load_iris from sklearn.preprocessing import label_binarize from sklearn.multiclass import OneVsRestClassifier from sklearn.model_selection imp

吴裕雄 python 机器学习——模型选择回归问题性能度量

from sklearn.metrics import mean_absolute_error,mean_squared_error #模型选择回归问题性能度量mean_absolute_error模型 def test_mean_absolute_error(): y_true=[1,1,1,1,1,2,2,2,0,0] y_pred=[0,0,0,1,1,1,0,0,0,0] print("Mean Absolute Error:",mean_absolute_error(y_tr

吴裕雄 python 机器学习——模型选择损失函数模型

from sklearn.metrics import zero_one_loss,log_loss def test_zero_one_loss(): y_true=[1,1,1,1,1,0,0,0,0,0] y_pred=[0,0,0,1,1,1,1,1,0,0] print("zero_one_loss<fraction>:",zero_one_loss(y_true,y_pred,normalize=True)) print("zero_one_loss&

吴裕雄 python 机器学习——K均值聚类KMeans模型

import numpy as np import matplotlib.pyplot as plt from sklearn import cluster from sklearn.metrics import adjusted_rand_score from sklearn.datasets.samples_generator import make_blobs def create_data(centers,num=100,std=0.7): X, labels_true = make_b

吴裕雄 python 机器学习——支持向量机SVM非线性分类SVC模型

import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm from sklearn.model_selection import train_test_split def load_data_classfication(): ''' 加载用于分类问题的数据集 ''' # 使用 scikit-learn 自带的 iris 数据集 iris=datasets.lo

吴裕雄 python 机器学习——支持向量机非线性回归SVR模型

import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model,svm from sklearn.model_selection import train_test_split def load_data_regression(): ''' 加载用于回归问题的数据集 ''' diabetes = datasets.load_diabetes() #使用 scikit-lea

吴裕雄 python 机器学习——集成学习随机森林RandomForestClassifier分类模型

import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklearn.model_selection import train_test_split def load_data_classification(): ''' 加载用于分类问题的数据集 ''' # 使用 scikit-learn 自带的 digits 数据集 digits=datasets.load_d

吴裕雄 python 机器学习——集成学习梯度提升决策树GradientBoostingRegressor回归模型

import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,ensemble from sklearn.model_selection import train_test_split def load_data_regression(): ''' 加载用于回归问题的数据集 ''' #使用 scikit-learn 自带的一个糖尿病病人的数据集 diabetes = datasets.load_di