使用scikit-learn训练NER的NLP日志线性模型

时间:2015-10-20 23:59:15

标签: nlp scikit-learn

我想知道如何使用sklearn.linear_model.LogisticRegression来训练命名实体识别(NER)的NLP日志线性模型。

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

enter image description here

使用:

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

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

问题在于功能取决于课程。

1 个答案:

答案 0 :(得分:7)

在scikit-learn 0.16及更高版本中,您可以使用multinomial sklearn.linear_model.LogisticRegression选项来训练对数线性模型(a.k.a. MaxEnt分类器,多类逻辑回归)。目前,'{lbfgs'和'newton-cg'解算器multinomial选项为supported only

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)))

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

  
      
  • 添加multi_class="multinomial"选项    :class:linear_model.LogisticRegression实现Logistic    回归求解器可最大限度地减少交叉熵或多项式损失    而不是默认的One-vs-Rest设置。支持lbfgs和    newton-cg求解器。按Lars Buitinck _和Manoj Kumar _。求解器选项    Simon Wu newton-cg
  •