我正在使用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)
但是,我没有收到明确或清晰的情节.有一个更好的方法吗?
我试图用一层构建一个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) 我们知道BERT有tokens的最大长度限制=512,那么如果一篇文章的长度远远大于512,比如文本中有10000个tokens,BERT怎么用呢?
文本数据被组织为具有20,000个元素的向量,如[2,1,0,0,5,....,0].第i个元素表示文本中第i个单词的频率.
地面实况标签数据也表示为具有4,000个元素的向量,如[0,0,1,0,1,......,0].第i个元素指示第i个标签是否是文本的肯定标签.文本的标签数量因文本而异.
我有一个单标签文本分类的代码.
如何编辑以下代码进行多标签文本分类?
特别是,我想知道以下几点.
谢谢.
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
我正在使用Scikit-learn进行文本分类.我想针对(稀疏)文档 - 术语矩阵中的类计算每个属性的信息增益.信息增益定义为H(类) - H(类|属性),其中H是熵.
使用weka,可以使用InfoGainAttribute完成.但我还没有在scikit-learn中找到这个措施.
但是,有人建议上面的信息增益公式与互信息相同.这也与维基百科中的定义相匹配.
是否可以在scikit中使用特定设置来交互信息 - 学习完成此任务?
python machine-learning feature-selection scikit-learn text-classification
我们可以从 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上搜索过,但不清楚)
我正在用文字袋来分类文字.它运作良好,但我想知道如何添加一个不是一个单词的功能.
这是我的示例代码.
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
我熟悉使用BOW功能进行文本分类,其中我们首先找到语料库词汇表的大小,这些词汇表的大小就是我们的特征向量.对于每个句子/文件及其所有组成单词,我们然后根据该单词/文档中该单词的缺席/存在而放置0/1.
但是,既然我正在尝试使用每个单词的向量表示,那么创建全局词汇是必不可少的吗?
尝试使用 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'
你能帮我吗


我有一个csv,struct is
CAT1,CAT2,TITLE,URL,CONTENT,CAT1,CAT2,TITLE,CONTENT都是中文的.
我想要火车LinearSVC或MultinomialNBX(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 ×7
scikit-learn ×4
nlp ×3
keras ×1
keras-layer ×1
matplotlib ×1
matrix ×1
mlp ×1
tensor ×1
tensorflow ×1
text ×1
vector ×1
word2vec ×1