【问题标题】:Using scikit-learn to training an NLP log linear model for NER使用 scikit-learn 为 NER 训练 NLP 对数线性模型
【发布时间】:2016-01-19 19:54:02
【问题描述】:

我想知道如何使用sklearn.linear_model.LogisticRegression 来训练 NLP 对数线性模型以进行命名实体识别 (NER)。

一个典型的对数线性模型定义条件概率如下:

与:

  • x:当前单词
  • y:正在考虑的单词类别
  • f:特征向量函数,将单词 x 和类 y 映射到标量向量。
  • v:特征权重向量

sklearn.linear_model.LogisticRegression可以训练出这样的模型吗?

问题在于功能取决于类。

【问题讨论】:

    标签: nlp scikit-learn


    【解决方案1】:

    在 scikit-learn 0.16 及更高版本中,您可以使用 sklearn.linear_model.LogisticRegressionmultinomial 选项来训练对数线性模型(又名 MaxEnt 分类器,多类逻辑回归)。目前 multinomial 选项是 supported only 由“lbfgs”和“newton-cg”求解器提供。

    以 Iris 数据集为例(4 个特征,3 个类别,150 个样本):

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    from __future__ import print_function
    from __future__ import division
    
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn import linear_model, datasets
    from sklearn.metrics import confusion_matrix
    from sklearn.metrics import classification_report
    
    # Import data 
    iris = datasets.load_iris()
    X = iris.data # features
    y_true = iris.target # labels
    
    # Look at the size of the feature matrix and the label vector:
    print('iris.data.shape: {0}'.format(iris.data.shape))
    print('iris.target.shape: {0}\n'.format(iris.target.shape))
    
    #  Instantiate a MaxEnt model
    logreg = linear_model.LogisticRegression(C=1e5, multi_class='multinomial', solver='lbfgs')
    
    # Train the model
    logreg.fit(X, y_true)
    print('logreg.coef_: \n{0}\n'.format(logreg.coef_))
    print('logreg.intercept_: \n{0}'.format(logreg.intercept_))
    
    # Use the model to make predictions
    y_pred = logreg.predict(X)
    print('\ny_pred: \n{0}'.format(y_pred))
    
    # Assess the quality of the predictions
    print('\nconfusion_matrix(y_true, y_pred):\n{0}\n'.format(confusion_matrix(y_true, y_pred)))
    print('classification_report(y_true, y_pred): \n{0}'.format(classification_report(y_true, y_pred)))
    

    sklearn.linear_model.LogisticRegression was introduced in version 0.16multinomial 选项:

    • 添加multi_class="multinomial"选项 :class:linear_model.LogisticRegression 实现一个物流 最小化交叉熵或多项损失的回归求解器 而不是默认的 One-vs-Rest 设置。支持lbfgsnewton-cg 求解器。通过Lars Buitinck_ 和Manoj Kumar_。求解器选项 newton-cg 由 Simon Wu。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2013-05-05
      • 2023-03-22
      • 2014-09-17
      • 2019-09-09
      相关资源
      最近更新 更多