zoukankan      html  css  js  c++  java
  • python进行机器学习(三)之模型选择与构建

    Scikit-Learn库已经实现了所有基本机器学习的算法,可以直接调用里面库进行模型构建

    一、逻辑回归

     

    大多数情况下被用来解决分类问题(二元分类),但多类的分类(所谓的一对多方法)也适用。这个算法的优点是对于每一个输出的对象都有一个对应类别的概率。

     

     

    from sklearn import metrics
    from sklearn.linear_model import LogisticRegression
    model = LogisticRegression()
    model.fit(X, y)
    print(model)
    # make predictions
    expected = y
    predicted = model.predict(X)
    # summarize the fit of the model
    print(metrics.classification_report(expected, predicted))
    print(metrics.confusion_matrix(expected, predicted))

     

    二、朴素贝叶斯

     

    它也是最有名的机器学习的算法之一,它的主要任务是恢复训练样本的数据分布密度。这个方法通常在多类的分类问题上表现的很好。

     

    from sklearn import metrics

    from sklearn.naive_bayes import GaussianNB

    model = GaussianNB()

    model.fit(X, y)

    print(model)

    # make predictions

    expected = y

    predicted = model.predict(X)

    # summarize the fit of the model

    print(metrics.classification_report(expected, predicted))

    print(metrics.confusion_matrix(expected, predicted))

     

    三、k-最近邻

     

    kNN(k-最近邻)方法通常用于一个更复杂分类算法的一部分。例如,我们可以用它的估计值做为一个对象的特征。有时候,一个简单的kNN算法在良好选择的特征上会有很出色的表现。当参数(主要是metrics)被设置得当,这个算法在回归问题中通常表现出最好的质量。

     

    from sklearn import metrics

    from sklearn.neighbors import KNeighborsClassifier

    # fit a k-nearest neighbor model to the data

    model = KNeighborsClassifier()

    model.fit(X, y)

    print(model)

    # make predictions

    expected = y

    predicted = model.predict(X)

    # summarize the fit of the model

    print(metrics.classification_report(expected, predicted))

    print(metrics.confusion_matrix(expected, predicted))

     

    四、决策树

     

    分类和回归树(CART)经常被用于这么一类问题,在这类问题中对象有可分类的特征且被用于回归和分类问题。决策树很适用于多类分类。

     

    from sklearn import metrics

    from sklearn.tree import DecisionTreeClassifier

    # fit a CART model to the data

    model = DecisionTreeClassifier()

    model.fit(X, y)

    print(model)

    # make predictions

    expected = y

    predicted = model.predict(X)

    # summarize the fit of the model

    print(metrics.classification_report(expected, predicted))

    print(metrics.confusion_matrix(expected, predicted))

     

    五、支持向量机

     

    SVM(支持向量机)是最流行的机器学习算法之一,它主要用于分类问题。同样也用于逻辑回归,SVM在一对多方法的帮助下可以实现多类分类。

     

    from sklearn import metrics

    from sklearn.svm import SVC

    # fit a SVM model to the data

    model = SVC()

    model.fit(X, y)

    print(model)

    # make predictions

    expected = y

    predicted = model.predict(X)

    # summarize the fit of the model

    print(metrics.classification_report(expected, predicted))

    print(metrics.confusion_matrix(expected, predicted))

     

    除了分类和回归问题,Scikit-Learn还有海量的更复杂的算法,包括了聚类, 以及建立混合算法的实现技术,如Bagging和Boosting。

     

  • 相关阅读:
    简单的排序算法总结
    Android Stuido 方法参数 p0,p1
    年与酒
    Android-自定义View前传-View的三大流程-Layout
    Android自定义View前传-View的三大流程-Measure
    寻找积极
    括号配对问题
    Wan Android 项目总结
    涂鸦之作WanAndroid第三方APP
    CSS-定位(Position)
  • 原文地址:https://www.cnblogs.com/gczr/p/6829068.html
Copyright © 2011-2022 走看看