精华内容
下载资源
问答
  • [Sklearn 常用分类器]

    2020-09-23 20:18:45
    [Sklearn 常用分类器] 转载 (https://blog.csdn.net/weixin_41571493/article/details/83011147)

    [Sklearn 常用分类器]
    转载
    (https://blog.csdn.net/weixin_41571493/article/details/83011147)

    ### KNN Classifier    
    from sklearn.neighbors import KNeighborsClassifier
     
    clf = KNeighborsClassifier()
    clf.fit(train_x, train_y)
    __________________________________________________________
     
    ### Logistic Regression Classifier    
    from sklearn.linear_model import LogisticRegression
     
    clf = LogisticRegression(penalty='l2')
    clf.fit(train_x, train_y)
    __________________________________________________________
     
    ### Random Forest Classifier    
    from sklearn.ensemble import RandomForestClassifier
     
    clf = RandomForestClassifier(n_estimators=8)
    clf.fit(train_x, train_y)
    __________________________________________________________
     
    ### Decision Tree Classifier    
    from sklearn import tree
     
    clf = tree.DecisionTreeClassifier()
    clf.fit(train_x, train_y)
    __________________________________________________________
     
    ### GBDT(Gradient Boosting Decision Tree) Classifier    
    from sklearn.ensemble import GradientBoostingClassifier
     
    clf = GradientBoostingClassifier(n_estimators=200)
    clf.fit(train_x, train_y)
    __________________________________________________________
     
    ###AdaBoost Classifier
    from sklearn.ensemble import  AdaBoostClassifier
     
    clf = AdaBoostClassifier()
    clf.fit(train_x, train_y)
    __________________________________________________________
     
    ### GaussianNB
    from sklearn.naive_bayes import GaussianNB
     
    clf = GaussianNB()
    clf.fit(train_x, train_y)
    __________________________________________________________
     
    ### Linear Discriminant Analysis
    from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
     
    clf = LinearDiscriminantAnalysis()
    clf.fit(train_x, train_y)
    __________________________________________________________
     
    ### Quadratic Discriminant Analysis
    from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
     
    clf = QuadraticDiscriminantAnalysis()
    clf.fit(train_x, train_y)
    __________________________________________________________
     
    ### SVM Classifier    
    from sklearn.svm import SVC
     
    clf = SVC(kernel='rbf', probability=True)
    clf.fit(train_x, train_y)
    __________________________________________________________
     
    ### Multinomial Naive Bayes Classifier    
    from sklearn.naive_bayes import MultinomialNB
     
    clf = MultinomialNB(alpha=0.01)
    clf.fit(train_x, train_y)
    
    展开全文
  • 常用分类器笔记

    2019-08-23 16:45:56
    常用几种分类器的应用笔记 # -- coding: utf-8 -- import pandas as pd import numpy as np from matplotlib import pyplot as plt import warnings warnings.filterwarnings("ignore", category=Future...

    常用几种分类器的应用笔记

    # -- coding: utf-8 --
    import pandas as pd
    import numpy as np
    from matplotlib import pyplot as plt
    import warnings
    warnings.filterwarnings("ignore", category=FutureWarning, module="sklearn", lineno=196)
    
    train = pd.read_excel('D:/PyWork/train_data.xlsx')
    test = pd.read_excel('D:/PyWork/test_data.xlsx')
    m, n = train.shape
    train_da= np.array(train)
    train_data = train_da[:, :n-1]
    train_label = train_da[:, n-1]
    
    test_da = np.array(test)
    test_data = test_da[:, :n-1]
    test_label = test_da[:, n-1]
    
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.tree import DecisionTreeClassifier
    
    
    clf = DecisionTreeClassifier(random_state=15)  # 决策树
    # rfc = RandomForestClassifier(n_estimators=15, random_state=15)  # 随机深林
    rfc = RandomForestClassifier(
            n_estimators=15,  # 是森林里树的数量,通常数量越大,效果越好
            criterion='gini',
            max_depth=None, min_samples_split=2,
            min_samples_leaf=1, min_weight_fraction_leaf=0.0,
            max_features='auto',  # 是分割节点时考虑的特征的随机子集的大小 这个值越低,方差减小得越多,但是偏差的增大也越多。
            max_leaf_nodes=None,
            bootstrap=True,
            oob_score=False,
            n_jobs=1,  # 线程数
            random_state=None, verbose=0,
            warm_start=False, class_weight=None)
    
    clf.fit(train_data, train_label)
    rfc.fit(train_data, train_label)
    # 获取模型的评分
    score_1 = clf.score(test_data, test_label)  # 0.57, 0.92
    score_2 = rfc.score(test_data, test_label)
    
    # 有意思的输出
    importances = rfc.feature_importances_  # 输出 自变量的总要程度
    std = np.std([tree.feature_importances_ for tree in rfc.estimators_],
                 axis=0)
    indices = np.argsort(importances)[::-1]
    # Print the feature ranking
    print("Feature ranking:")
    
    for f in range(train_data.shape[1]):
        print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
    # Plot the feature importances of the forest
    plt.figure()  # 画特征重要性图
    plt.title("Feature importances")
    plt.bar(range(train_data.shape[1]), importances[indices],
           color="r", yerr=std[indices], align="center")
    plt.xticks(range(train_data.shape[1]), indices)
    plt.xlim([-1, train_data.shape[1]])
    plt.show()
    
    print("决策树:评分{}".format(score_1), "随机森林:评分{}".format(score_2))
    
    # from sklearn import tree
    # import graphviz  # 导出器以 Graphviz 格式导出决策树
    # dot_data = tree.export_graphviz(clf, out_file=None)
    # graph = graphviz.Source(dot_data)
    # graph.render("irs")
    
    
    from sklearn import svm
    
    clf_rbf = svm.SVC(kernel='rbf', C=1, gamma=0.2)  # poly,linear
    clf_rbf.fit(train_data, train_label)
    score_rbf = clf_rbf.score(test_data, test_label)
    print("svm score  is : %f" % score_rbf)
    
    # ==============基于Scikit-learn接口的分类================
    from sklearn.datasets import load_iris
    import xgboost as xgb
    from xgboost import plot_importance
    from matplotlib import pyplot as plt
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import accuracy_score
    
    # 加载样本数据集
    iris = load_iris()
    # X,y = iris.data,iris.target
    # X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1234565) # 数据集分割
    
    # 训练模型
    model = xgb.XGBClassifier(max_depth=3,
                              learning_rate=0.1,
                              n_estimators=100,
                              silent=True,
                              objective='multi:relu')   # softmax
    model.fit(train_data, train_label)
    
    # 对测试集进行预测
    y_pred = model.predict(test_data)
    
    # 计算准确率
    accuracy = accuracy_score(test_label,y_pred)
    print("accuarcy: %.2f%%" % (accuracy*100.0))
    
    # 显示重要特征
    plot_importance(model)
    plt.show()
    
    
    
    展开全文
  • 机器学习29:Sklearn库常用分类器及效果比较 1.Sklearn库常用分类器: #【1】 KNN Classifier # k-近邻分类器 from sklearn.neighbors import KNeighborsClassifier clf = KNeighborsClassifier() clf.fit...

    机器学习29:Sklearn库常用分类器及效果比较

    1.Sklearn库常用分类器:

    #【1】 KNN Classifier   
    # k-近邻分类器 
    from sklearn.neighbors import KNeighborsClassifier
     
    clf = KNeighborsClassifier()
    clf.fit(train_x, train_y)
    
    
     
    #【2】 Logistic Regression Classifier    
    # 逻辑回归分类器
    from sklearn.linear_model import LogisticRegression
     
    clf = LogisticRegression(penalty='l2')
    clf.fit(train_x, train_y)
    
    
    
    #【3】 Random Forest Classifier    
    # 随机森林分类器
    from sklearn.ensemble import RandomForestClassifier
     
    clf = RandomForestClassifier(n_estimators=8)
    clf.fit(train_x, train_y)
    
    
     
    #【4】 Decision Tree Classifier 
    # 决策树分类器   
    from sklearn import tree
     
    clf = tree.DecisionTreeClassifier()
    clf.fit(train_x, train_y)
    
    
    
    #【5】 SVM Classifier  
    # 支持向量机分类器  
    from sklearn.svm import SVC
     
    clf = SVC(kernel='rbf', probability=True)
    clf.fit(train_x, train_y)
    
    
    
    #【6】 Multinomial Naive Bayes Classifier   
    # 多项式朴素贝叶斯分类器 
    from sklearn.naive_bayes import MultinomialNB
     
    clf = MultinomialNB(alpha=0.01)
    clf.fit(train_x, train_y)
    
    
    
    #【7】 GBDT(Gradient Boosting Decision Tree) Classifier    
    # 梯度增强决策树分类器
    from sklearn.ensemble import GradientBoostingClassifier
     
    clf = GradientBoostingClassifier(n_estimators=200)
    clf.fit(train_x, train_y)
    
    
     
    #【8】AdaBoost Classifier
    from sklearn.ensemble import  AdaBoostClassifier
     
    clf = AdaBoostClassifier()
    clf.fit(train_x, train_y)
    
    
     
    #【9】 GaussianNB
    # 高斯贝叶斯分类器
    from sklearn.naive_bayes import GaussianNB
     
    clf = GaussianNB()
    clf.fit(train_x, train_y)
    
    
    
    #【10】 Linear Discriminant Analysis
    # 线性判别分析
    from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
     
    clf = LinearDiscriminantAnalysis()
    clf.fit(train_x, train_y)
    
    
    
    #【11】 Quadratic Discriminant Analysis
    # 二次判别分析
    from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
     
    clf = QuadraticDiscriminantAnalysis()
    clf.fit(train_x, train_y)
    
    
    
    

    2.Slearn常见分类器的效果比较:

                本段代码摘抄自Sklearn常见分类起的效果比较,效果图可以点进原文查看,也可以在python上运行查看。

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.colors import ListedColormap
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import StandardScaler
    from sklearn.datasets import make_moons, make_circles, make_classification
    from sklearn.neural_network import BernoulliRBM
    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.svm import SVC
    # from sklearn.gaussian_process import GaussianProcess
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
    from sklearn.naive_bayes import GaussianNB
    from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
    
    h = .02  # step size in the mesh
    
    names = ["Nearest Neighbors", "Linear SVM", "RBF SVM",
             "Decision Tree", "Random Forest", "AdaBoost",
             "Naive Bayes", "QDA", "Gaussian Process","Neural Net", ]
    
    classifiers = [
        KNeighborsClassifier(3),
        SVC(kernel="linear", C=0.025),
        SVC(gamma=2, C=1),
        DecisionTreeClassifier(max_depth=5),
        RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1),
        AdaBoostClassifier(),
        GaussianNB(),
        QuadraticDiscriminantAnalysis(),
        #GaussianProcess(),
        #BernoulliRBM(),
        ]
    
    X, y = make_classification(n_features=2, n_redundant=0, n_informative=2,
                               random_state=1, n_clusters_per_class=1)
    rng = np.random.RandomState(2)
    X += 2 * rng.uniform(size=X.shape)
    linearly_separable = (X, y)
    
    datasets = [make_moons(noise=0.3, random_state=0),
                make_circles(noise=0.2, factor=0.5, random_state=1),
                linearly_separable
                ]
    
    figure = plt.figure(figsize=(27, 9))
    i = 1
    # iterate over datasets
    for ds_cnt, ds in enumerate(datasets):
        # preprocess dataset, split into training and test part
        X, y = ds
        X = StandardScaler().fit_transform(X)
        X_train, X_test, y_train, y_test = \
            train_test_split(X, y, test_size=.4, random_state=42)
    
        x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
        y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
        xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                             np.arange(y_min, y_max, h))
    
        # just plot the dataset first
        cm = plt.cm.RdBu
        cm_bright = ListedColormap(['#FF0000', '#0000FF'])
        ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
        if ds_cnt == 0:
            ax.set_title("Input data")
        # Plot the training points
        ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
        # and testing points
        ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright, alpha=0.6)
        ax.set_xlim(xx.min(), xx.max())
        ax.set_ylim(yy.min(), yy.max())
        ax.set_xticks(())
        ax.set_yticks(())
        i += 1
    
        # iterate over classifiers
        for name, clf in zip(names, classifiers):
            ax = plt.subplot(len(datasets), len(classifiers) + 1, i)
            clf.fit(X_train, y_train)
            score = clf.score(X_test, y_test)
    
            # Plot the decision boundary. For that, we will assign a color to each
            # point in the mesh [x_min, m_max]x[y_min, y_max].
            if hasattr(clf, "decision_function"):
                Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
            else:
                Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
    
            # Put the result into a color plot
            Z = Z.reshape(xx.shape)
            ax.contourf(xx, yy, Z, cmap=cm, alpha=.8)
    
            # Plot also the training points
            ax.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=cm_bright)
            # and testing points
            ax.scatter(X_test[:, 0], X_test[:, 1], c=y_test, cmap=cm_bright,
                       alpha=0.6)
    
            ax.set_xlim(xx.min(), xx.max())
            ax.set_ylim(yy.min(), yy.max())
            ax.set_xticks(())
            ax.set_yticks(())
            if ds_cnt == 0:
                ax.set_title(name)
            ax.text(xx.max() - .3, yy.min() + .3, ('%.2f' % score).lstrip('0'),
                    size=15, horizontalalignment='right')
            i += 1
    
    plt.tight_layout()
    plt.show()

     

    展开全文
  • 【机器学习】Sklearn 常用分类器(全)

    万次阅读 多人点赞 2018-10-11 13:39:58
    【机器学习】Sklearn 常用分类器(全) ### KNN Classifier from sklearn.neighbors import KNeighborsClassifier clf = KNeighborsClassifier() clf.fit(train_x, train_y) __________________________________...

    【机器学习】Sklearn 常用分类器(全)

    ### KNN Classifier    
    from sklearn.neighbors import KNeighborsClassifier
    
    clf = KNeighborsClassifier()
    clf.fit(train_x, train_y)
    __________________________________________________________
    
    ### Logistic Regression Classifier    
    from sklearn.linear_model import LogisticRegression
    
    clf = LogisticRegression(penalty='l2')
    clf.fit(train_x, train_y)
    __________________________________________________________
    
    ### Random Forest Classifier    
    from sklearn.ensemble import RandomForestClassifier
    
    clf = RandomForestClassifier(n_estimators=8)
    clf.fit(train_x, train_y)
    __________________________________________________________
    
    ### Decision Tree Classifier    
    from sklearn import tree
    
    clf = tree.DecisionTreeClassifier()
    clf.fit(train_x, train_y)
    __________________________________________________________
    
    ### GBDT(Gradient Boosting Decision Tree) Classifier    
    from sklearn.ensemble import GradientBoostingClassifier
    
    clf = GradientBoostingClassifier(n_estimators=200)
    clf.fit(train_x, train_y)
    __________________________________________________________
    
    ###AdaBoost Classifier
    from sklearn.ensemble import  AdaBoostClassifier
    
    clf = AdaBoostClassifier()
    clf.fit(train_x, train_y)
    __________________________________________________________
    
    ### GaussianNB
    from sklearn.naive_bayes import GaussianNB
    
    clf = GaussianNB()
    clf.fit(train_x, train_y)
    __________________________________________________________
    
    ### Linear Discriminant Analysis
    from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
    
    clf = LinearDiscriminantAnalysis()
    clf.fit(train_x, train_y)
    __________________________________________________________
    
    ### Quadratic Discriminant Analysis
    from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
    
    clf = QuadraticDiscriminantAnalysis()
    clf.fit(train_x, train_y)
    __________________________________________________________
    
    ### SVM Classifier    
    from sklearn.svm import SVC
    
    clf = SVC(kernel='rbf', probability=True)
    clf.fit(train_x, train_y)
    __________________________________________________________
    
    ### Multinomial Naive Bayes Classifier    
    from sklearn.naive_bayes import MultinomialNB
    
    clf = MultinomialNB(alpha=0.01)
    clf.fit(train_x, train_y)

     

     

    展开全文
  • Sklearn常用分类器总结

    千次阅读 2019-08-19 23:10:49
    常用分类器: SVM、KNN、贝叶斯、线性回归、逻辑回归、决策树、随机森林、xgboost、GBDT、boosting、神经网络NN。 ### KNN Classifier from sklearn.neighbors import KNeighborsClassifier clf = ...
  • MATLAB中分类器有:K近邻分类器,随机森林分类器,朴素贝叶斯,集成学习方法,鉴别分析分类器,支持向量机。 现将其主要函数使用方法总结如下,更多细节需参考MATLAB 帮助文件。设:  训练样本 :train_data  ...
  • sklearn常用分类器及代码实现

    千次阅读 2016-12-12 11:07:05
    常用分类器包括SVM、KNN、贝叶斯、线性回归、逻辑回归、决策树、随机森林、xgboost、GBDT、boosting、神经网络NN。 代码如下: from sklearn.metrics import precision_recall_fscore_support def ...
  • 常用分类器的效果对比

    万次阅读 2015-10-21 18:30:40
    机器学习发展到现在,许多被证实有效的分类算法被提出,例如我们经常会用到的K-近邻分类器、朴素贝叶斯分类器、支持向量机(SVM)、决策树算法等。大家平时在用的时候可能并不太清楚每种分类算法适合哪种类型的数据,...
  • 常用分类器之R语言篇

    2021-01-21 20:04:03
    常用的是准确率(Accuracy),即分类器是否能正确划分样本;正例覆盖率(Sensitivity)是指正类的样本被预测正确的比例;精确度(Positive Predictive Value)是指被预测为正类的样本中预测正确的比例。代码如下:...
  • 在训练之后,分类器将对象的特征与有关类别的特征做比较,并返回最大匹配的类别。根据所选的分类器,类别的可能性或者分类的可行度等可能额外的信息将被给出。一般来说,可以区分两种对图像数据的分类方法。一种方法...
  • 常用电阻器分类知识大全 1电位电位是一种机电元件,他*电刷在电阻体上的滑动,取得与电刷位移成一定关系的输出电压。 1.1 合成碳膜电位电阻体是用经过研磨的碳黑,石墨,石英等材料涂敷于基体表面而成,该...
  • 电工常用低压电器的分类 什么是低压电器呢?低压电器通常是指工作在交流电压1000V以下与直流电压1200V及以下电路中的电器。 其种类可以按在电气线路中的地位、作用和动作方式分类。按在电气线路中的地位作用分类如下...
  • 下面介绍常用的儿种分类方法: 通常可分为固定电阻、可变电阻、敏感电阻等。 固定电阻可分为:碳膜电阻、金属膜电阻、金属氧化膜电阻、金属玻璃釉电阻、无机实心电阻、有机实心电阻、化学沾积膜...
  • 评估分类器性能的常用度量的链接地址 评估分类器性能的常用度量 一.基本的术语 正元组:感兴趣的主要类的元组 负元组:其他元组 例如:给定两个类,正元组可能是buys_computer=yes,负元组是buys_computer=no。 真正...
  • 电容是电子设备中大量使用的电子元件之一,广泛应用于电路中的隔直通交,耦合,旁路,滤波,调谐回路,能量转换,控制等方面。
  • 机器学习常用分类器比较

    万次阅读 2018-04-22 21:15:15
    传统的机器学习的监督学习分类分类和回归,分类是争对离散的数据,而回归是争对连续的数据,在数据预处理好的基础上要对数据进行预测,通常采用CV交叉验证来进行模型评价和选择。这篇文章通过连续的数据结合sklearn...
  • 人脸识别中常用的几种分类器 在人脸识别中有几种常用分类器,一是最邻近分类器;二是线性分类器 (1)最邻近分类器 最近邻分类器是模式识别领域中最常用的分类方法之一,其直观简单,在通常的应用环境中非常...
  • 电阻的功能:电阻对流过自身电流有着阻碍作用的一种器件。在电子设备中占元件总数的30%以上,在电路中,电阻的作用主要是稳定和调节电路中的电流和电压, 即起降压、分压、限流、分流、隔离、过滤(与电容...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 3,317
精华内容 1,326
关键字:

常用分类器