标签: text-classification

如何绘制混淆矩阵?

我正在使用scikit-learn将文本文档(22000)分类为100个类.我使用scikit-learn的混淆矩阵方法来计算混淆矩阵.

model1 = LogisticRegression()
model1 = model1.fit(matrix, labels)
pred = model1.predict(test_matrix)
cm=metrics.confusion_matrix(test_labels,pred)
print(cm)
plt.imshow(cm, cmap='binary')
Run Code Online (Sandbox Code Playgroud)

这就是我的混淆矩阵的样子:

[[3962  325    0 ...,    0    0    0]
 [ 250 2765    0 ...,    0    0    0]
 [   2    8   17 ...,    0    0    0]
 ..., 
 [   1    6    0 ...,    5    0    0]
 [   1    1    0 ...,    0    0    0]
 [   9    0    0 ...,    0    0    9]]
Run Code Online (Sandbox Code Playgroud)

但是,我没有收到明确或清晰的情节.有一个更好的方法吗?

python matplotlib matrix scikit-learn text-classification

51
推荐指数
3
解决办法
11万
查看次数

conv1D中的形状尺寸

我试图用一层构建一个CNN,但我有一些问题.确实,编译器说我

ValueError:检查模型输入时出错:预期conv1d_1_input具有3个维度,但是具有形状的数组(569,30)

这是代码

import numpy
from keras.models import Sequential
from keras.layers.convolutional import Conv1D
numpy.random.seed(7)
datasetTraining = numpy.loadtxt("CancerAdapter.csv",delimiter=",")
X = datasetTraining[:,1:31]
Y = datasetTraining[:,0]
datasetTesting = numpy.loadtxt("CancereEvaluation.csv",delimiter=",")
X_test = datasetTraining[:,1:31]
Y_test = datasetTraining[:,0]
model = Sequential()
model.add(Conv1D(2,2,activation='relu',input_shape=X.shape))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, Y, epochs=150, batch_size=5)
scores = model.evaluate(X_test, Y_test)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
Run Code Online (Sandbox Code Playgroud)

python text-classification keras keras-layer

51
推荐指数
2
解决办法
4万
查看次数

如何使用Bert进行长文本分类?

我们知道BERT有tokens的最大长度限制=512,那么如果一篇文章的长度远远大于512,比如文本中有10000个tokens,BERT怎么用呢?

nlp text-classification bert-language-model

39
推荐指数
5
解决办法
3万
查看次数

使用TensorFlow进行多标签文本分类

文本数据被组织为具有20,000个元素的向量,如[2,1,0,0,5,....,0].第i个元素表示文本中第i个单词的频率.

地面实况标签数据也表示为具有4,000个元素的向量,如[0,0,1,0,1,......,0].第i个元素指示第i个标签是否是文本的肯定标签.文本的标签数量因文本而异.

我有一个单标签文本分类的代码.

如何编辑以下代码进行多标签文本分类?

特别是,我想知道以下几点.

  • 如何使用TensorFlow计算精度.
  • 如何设置判断标签是正面还是负面的阈值.例如,如果输出为[0.80,0.43,0.21,0.01,0.32]且基本事实为[1,1,0,0,1],则得分超过0.25的标签应判断为正数.

谢谢.

import tensorflow as tf

# hidden Layer
class HiddenLayer(object):
    def __init__(self, input, n_in, n_out):
        self.input = input

        w_h = tf.Variable(tf.random_normal([n_in, n_out],mean = 0.0,stddev = 0.05))
        b_h = tf.Variable(tf.zeros([n_out]))

        self.w = w_h
        self.b = b_h
        self.params = [self.w, self.b]

    def output(self):
        linarg = tf.matmul(self.input, self.w) + self.b
        self.output = tf.nn.relu(linarg)

        return self.output

# output Layer
class OutputLayer(object):
    def __init__(self, input, n_in, n_out):
        self.input = input

        w_o = tf.Variable(tf.random_normal([n_in, n_out], mean = 0.0, stddev …
Run Code Online (Sandbox Code Playgroud)

python text-classification multilabel-classification tensorflow

34
推荐指数
2
解决办法
2万
查看次数

信息使用Scikit-learn计算收益

我正在使用Scikit-learn进行文本分类.我想针对(稀疏)文档 - 术语矩阵中的类计算每个属性的信息增益.信息增益定义为H(类) - H(类|属性),其中H是熵.

使用weka,可以使用InfoGainAttribute完成.但我还没有在scikit-learn中找到这个措施.

但是,有人建议上面的信息增益公式与互信息相同.这也与维基百科中的定义相匹配.

是否可以在scikit中使用特定设置来交互信息 - 学习完成此任务?

python machine-learning feature-selection scikit-learn text-classification

19
推荐指数
1
解决办法
2万
查看次数

AutoModelForSequenceClassification 与 AutoModel 之间有什么区别

我们可以从 AutoModel(TFAutoModel) 函数创建一个模型:

from transformers import AutoModel 
model = AutoModel.from_pretrained('distilbert-base-uncase')
Run Code Online (Sandbox Code Playgroud)

另一方面,模型是由 AutoModelForSequenceClassification(TFAutoModelForSequenceClassification) 创建的:

from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification('distilbert-base-uncase')
Run Code Online (Sandbox Code Playgroud)

据我所知,这两个模型都使用 distilbert-base-uncase 库来创建模型。从方法名称来看,第二个类(AutoModelForSequenceClassification)是为序列分类创建的。

但是两个类别的真正区别是什么?以及如何正确使用它们?

(我在huggingface上搜索过,但不清楚)

nlp text-classification huggingface-transformers

19
推荐指数
2
解决办法
1万
查看次数

如何添加另一个功能(文本的长度)到当前的单词分类?Scikit学习

我正在用文字袋来分类文字.它运作良好,但我想知道如何添加一个不是一个单词的功能.

这是我的示例代码.

import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsRestClassifier

X_train = np.array(["new york is a hell of a town",
                    "new york was originally dutch",
                    "new york is also called the big apple",
                    "nyc is nice",
                    "the capital of great britain is london. london is a huge metropolis which has a great many number of people living in it. london is also a very …
Run Code Online (Sandbox Code Playgroud)

python classification machine-learning scikit-learn text-classification

16
推荐指数
1
解决办法
3852
查看次数

如何使用单词的向量表示(从Word2Vec等获得)作为分类器的特征?

我熟悉使用BOW功能进行文本分类,其中我们首先找到语料库词汇表的大小,这些词汇表的大小就是我们的特征向量.对于每个句子/文件及其所有组成单词,我们然后根据该单词/文档中该单词的缺席/存在而放置0/1.

但是,既然我正在尝试使用每个单词的向量表示,那么创建全局词汇是必不可少的吗?

text nlp vector text-classification word2vec

15
推荐指数
1
解决办法
3272
查看次数

使用 BERT 文本分类时,出现 ValueError: Too muchDimensions 'str' 错误

尝试使用 BERT 模型制作文本情感分类器,但得到ValueError : too many dimensions 'str'

这是列车数据值的 DataFrame;所以它们是train_labels

0   notr
1   notr
2   notr
3   negative
4   notr
... ...
854 positive
855 notr
856 notr
857 notr
858 positive
Run Code Online (Sandbox Code Playgroud)

并且有代码产生错误

train_seq = torch.tensor(tokens_train['input_ids'])
train_mask = torch.tensor(tokens_train['attention_mask'])
train_y = torch.tensor(train_labels.tolist())
Run Code Online (Sandbox Code Playgroud)

At train_y = torch.tensor(train_labels.tolist());出现错误: ValueError: too many dimensions 'str'

你能帮我吗

在此输入图像描述

在此输入图像描述

python text-classification tensor mlp bert-language-model

15
推荐指数
2
解决办法
4万
查看次数

sklearn分类器得到ValueError:输入形状不好

我有一个csv,struct is CAT1,CAT2,TITLE,URL,CONTENT,CAT1,CAT2,TITLE,CONTENT都是中文的.

我想要火车LinearSVCMultinomialNBX(TITLE)和功能(CAT1,CAT2),都会得到这个错误.下面是我的代码:

PS:我通过这个例子scikit-learn text_analytics在下面写代码

import numpy as np
import csv
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
from sklearn.pipeline import Pipeline

label_list = []

def label_map_target(label):
    ''' map chinese feature name to integer  '''
    try:
        idx = label_list.index(label)
    except ValueError:
        idx = len(label_list)
        label_list.append(label)

    return idx


c1_list = []
c2_list = []
title_list = []
with open(csv_file, 'r') as f:
    # row_from_csv is for shorting this example
    for row in …
Run Code Online (Sandbox Code Playgroud)

python classification scikit-learn text-classification

14
推荐指数
2
解决办法
6万
查看次数