![](/img/trans.png)
[英]Sklearn+Gensim: How to use Gensim's Word2Vec embedding for Sklearn text classification
[英]How to use word embedding and feature for text classification
我有一堆句子要分类。 对于每个句子,我使用 word2vec 生成了一个词嵌入。 我还进行了聚类分析,将句子聚类为 3 个单独的聚类。
我想要做的是使用集群 ID (1-3) 作为我的 model 的功能。 但是,我只是不完全确定如何做到这一点? 我似乎找不到一篇清楚说明如何做到这一点的好文章。
我在想我可以为集群 ID 创建一个热嵌入,然后以某种方式将一个热嵌入到单词嵌入中? 我真的不确定在这里做什么。
我已经有一个 model 将接受词嵌入并对句子进行分类:
X=Data[word_embedding].values
y=Data[category].values
indices = filtered_products.index.values
X_train, X_test, y_train, y_test, indices_train, indices_test, = train_test_split(X, y, indices, test_size=0.3, random_state=428)
clf = svm.SVC(C=1.0, kernel='linear', degree=3, gamma='auto')
DSVM = clf.fit(X_train,y_train)
prediction = DSVM.predict(X_test)
print(metrics.classification_report(y_test, prediction))
其中X
是词嵌入, y
是类别。 只是不确定如何将集群 ID 添加为功能
假设,您想使用 Tensorflow。 您可以使用Embedding
层将 id 或 map 编码为 n 维随机向量。 这是一个带有Embedding
层的示例,其中我将每个 id 映射到一个 10 维向量,然后将该向量重复 50 次以对应于句子的最大长度(因此,每个单词具有相同的 10 维向量给定的输入)。 之后,我只是连接:
import tensorflow as tf
word_embedding_dim = 300
max_sentence_length = 50
word_embedding_input = tf.keras.layers.Input((max_sentence_length, word_embedding_dim))
id_input = tf.keras.layers.Input((1, ))
embedding_layer = tf.keras.layers.Embedding(1, 10) # or one-hot encode
x = embedding_layer(id_input)
x = tf.keras.layers.RepeatVector(max_sentence_length)(x[:, 0, :])
output = tf.keras.layers.Concatenate()([word_embedding_input, x])
model = tf.keras.Model([word_embedding_input, id_input], output)
print(model.summary())
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_17 (InputLayer) [(None, 1)] 0 []
embedding_3 (Embedding) (None, 1, 10) 10 ['input_17[0][0]']
tf.__operators__.getitem (Slic (None, 10) 0 ['embedding_3[0][0]']
ingOpLambda)
input_16 (InputLayer) [(None, 50, 300)] 0 []
repeat_vector_1 (RepeatVector) (None, 50, 10) 0 ['tf.__operators__.getitem[0][0]'
]
concatenate (Concatenate) (None, 50, 310) 0 ['input_16[0][0]',
'repeat_vector_1[0][0]']
==================================================================================================
Total params: 10
Trainable params: 10
Non-trainable params: 0
__________________________________________________________________________________________________
None
如果您没有 2D 输入,但实际上是句子嵌入,则更容易:
import tensorflow as tf
sentence_embedding_dim = 300
sentence_embedding_input = tf.keras.layers.Input((sentence_embedding_dim,))
id_input = tf.keras.layers.Input((1, ))
embedding_layer = tf.keras.layers.Embedding(1, 10) # or one-hot encode
x = embedding_layer(id_input)
output = tf.keras.layers.Concatenate()([sentence_embedding_input, x[:, 0, :]])
model = tf.keras.Model([sentence_embedding_input, id_input], output)
以下是numpy
和sklearn
的解决方案供参考:
import numpy as np
from sklearn.preprocessing import OneHotEncoder
samples = 10
word_embedding_dim = 300
max_sentence_length = 50
ids = np.random.randint(low=1, high=4, size=(10,)).reshape(-1, 1)
enc = OneHotEncoder(handle_unknown='ignore')
ids = enc.fit_transform(ids).toarray()[:, None, :]
X_train = np.random.random((samples, max_sentence_length, word_embedding_dim))
ids = np.repeat(ids, max_sentence_length, axis=1)
X_train = np.concatenate([X_train, ids], axis=-1)
print(X_train.shape)
# (10, 50, 303)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.