• sklearn_模型遍历


    # _*_ coding = utf_8 _*_
    import matplotlib.pyplot as plt
    import seaborn as sns
    import pandas as pd
    from sklearn.model_selection import StratifiedShuffleSplit
    from sklearn.metrics import accuracy_score, log_loss
    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.svm import SVC
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
    from sklearn.naive_bayes import GaussianNB
    from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
    from sklearn.linear_model import LogisticRegression
    # from sklearn.cluster import KMeans
    
    classifiers = [
        KNeighborsClassifier(3),
        SVC(probability=True),
        DecisionTreeClassifier(),
        RandomForestClassifier(),
        AdaBoostClassifier(),
        GradientBoostingClassifier(),
        GaussianNB(),
        LinearDiscriminantAnalysis(),
        QuadraticDiscriminantAnalysis(),
        LogisticRegression()]
    
    log_cols = ["Classifier", "Accuracy"]
    log      = pd.DataFrame(columns=log_cols)
    
    sss = StratifiedShuffleSplit(n_splits=10, test_size=0.1, random_state=0)
    # sss对象用于划分数据集
    import sklearn.datasets as datasets
    X,y=datasets.make_blobs(100,centers=10)
    
    print(X.shape)
    # X为特征集
    
    # y为Label集
    
    acc_dict = {}
    
    for train_index, test_index in sss.split(X, y):
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]
    
        for clf in classifiers:
            name = clf.__class__.__name__
            clf.fit(X_train, y_train)
            train_predictions = clf.predict(X_test)
            acc = accuracy_score(y_test, train_predictions)
            if name in acc_dict:
                acc_dict[name] += acc
            else:
                acc_dict[name] = acc
    print(acc_dict)
    for clf in acc_dict:
        acc_dict[clf] = acc_dict[clf] / 10.0
        # 计算平均准确率
        log_entry = pd.DataFrame([[clf, acc_dict[clf]]], columns=log_cols)
        log = log.append(log_entry)
    
    plt.xlabel('Accuracy')
    plt.title('Classifier Accuracy')
    
    sns.set_color_codes("muted")
    sns.barplot(x='Accuracy', y='Classifier', data=log, color="b")
    # 画条形图分析
    plt.show()
    自动化学习。
  • 相关阅读:
    .ssh/config 文件的解释算法及配置原则
    不fq安装 golang tools
    iOS 组件化的几篇文章
    第一把机械键盘 ikbc C-87
    我推荐的 Java Web 学习路线
    【吐槽向】iOS 中的仿射变换
    Spring 对缓存的抽象
    Spring 事务管理笔记
    Swift 笔记汇总
    Java 类加载器
  • 原文地址:https://www.cnblogs.com/wuzaipei/p/9471358.html
Copyright © 2020-2023  润新知