python 优化分类器算法并计算其准确性(使用分类数据)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 优化分类器算法并计算其准确性(使用分类数据)相关的知识,希望对你有一定的参考价值。

## OPTIMIZE CLASSIFIER AND CALCULATE HIS ACCURACY(with categorical data)
def classifer_optimize_accuracy(X,Y,test_size,model,tuned_parameters,sscores = 'precision'):

    from sklearn.model_selection import train_test_split
    from sklearn.model_selection import GridSearchCV
    from sklearn.metrics import classification_report

    # Split the dataset in two equal parts
    X_train, X_test, y_train, y_test = train_test_split(
        X, Y, test_size=test_size, random_state=0)

    # define scores to be used
    if sscores=='precision': scores = ['precision']
    elif sscores=='recall': scores = ['recall']    
    elif sscores =='all': scores = ['precision', 'recall']
    else: scores = ['precision']

    # loop of scores
    for score in scores:
        print("##############################################")
        print("# Tuning hyper-parameters for %s" % score)
        print("##############################################\n")
        print()

        clf = GridSearchCV(model, tuned_parameters, cv=5,
                           scoring='%s_macro' % score)
        clf.fit(X_train, y_train)

        print("Best parameters set found on development set:")
        print()
        print(clf.best_params_)
        print()
        print("Grid scores on development set:")
        print()
        means = clf.cv_results_['mean_test_score']
        stds = clf.cv_results_['std_test_score']
        for mean, std, params in zip(means, stds, clf.cv_results_['params']):
            print("%0.3f (+/-%0.03f) for %r"
                  % (mean, std * 2, params))
        print()

        print("Detailed classification report:")
        print()
        print("The model is trained on the full development set.")
        print("The scores are computed on the full evaluation set.")
        print()
        y_true, y_pred = y_test, clf.predict(X_test)
        print(classification_report(y_true, y_pred))
        print()
        
    # return
    return None

以上是关于python 优化分类器算法并计算其准确性(使用分类数据)的主要内容,如果未能解决你的问题,请参考以下文章

)

Python:如何在多标签类的 SVM 文本分类器算法中找到准确度结果

Adaboost算法详解(haar人脸检测)

使用多项式贝叶斯分类器

使用 CNN 算法计算未标记图像的输出

优化算法总结