如何在使用 Tensorflow 将文本数据集输入模型之前对其进行特征选择
How to perform feature selection on text dataset before input them to model using Tensorflow
我目前正在研究用于对 Twitter 数据进行情感分析的深度学习模型。我正在关注 Here
中的这个示例
为了提高准确性我想在将 Twitter 文本数据作为输入输入到神经网络模型之前对它们执行特征选择。我正在研究 tensorflow 和 keras。
如何使用张量流实现这一点?
data = tweets[['text','airline_sentiment']]
data = data[data.airline_sentiment != "neutral"]
data['text'] = data['text'].apply(lambda x: x.lower())
data['text'] = data['text'].apply((lambda x: re.sub('[^a-zA-z0-9\s]','',x)))
max_fatures = 2000
tokenizer = Tokenizer(num_words=max_fatures, split=' ')
tokenizer.fit_on_texts(data['text'].values)
X = tokenizer.texts_to_sequences(data['text'].values)
X = pad_sequences(X)
embed_dim = 128
lstm_out = 196
model = Sequential()
model.add(Embedding(max_fatures, embed_dim,input_length = X.shape[1]))
model.add(Dropout(0.5))
model.add(LSTM(lstm_out, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(2,activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
print(model.summary())
Y = pd.get_dummies(data['airline_sentiment']).values
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size = 0.33, random_state = 42)
X_val = X_train[:500]
Y_val = Y_train[:500]
partial_X_train = X_train[500:]
partial_Y_train = Y_train[500:]
batch_size = 512
history = model.fit(partial_X_train,
partial_Y_train,
epochs = 10,
batch_size=batch_size,
validation_data=(X_val, Y_val))
有几种方法可以做到这一点。一个简单的想法是使用 TF-IDF 作为特征重要性的度量。您可以在调用 fit_on_texts
.
后使用 Keras Tokenizer 访问 TF 和 IDF
您可以简单地过滤掉低于某个阈值的值:看看 Text Preprocessing
无论如何,我不建议走这条路,因为你正在处理深度学习,你的模型应该自动学习这个重要性。此外,您正在使用词嵌入,减少出现的词将影响这些嵌入。
你的字典(max_fatures)很低,我会先开始增加这个数字。 Keras 丢弃所有其他标记:
num_words: None or int. Maximum number of words to work with (if set,
tokenization will be restricted to the top num_words most common words
in the dataset)
我目前正在研究用于对 Twitter 数据进行情感分析的深度学习模型。我正在关注 Here
中的这个示例为了提高准确性我想在将 Twitter 文本数据作为输入输入到神经网络模型之前对它们执行特征选择。我正在研究 tensorflow 和 keras。
如何使用张量流实现这一点?
data = tweets[['text','airline_sentiment']]
data = data[data.airline_sentiment != "neutral"]
data['text'] = data['text'].apply(lambda x: x.lower())
data['text'] = data['text'].apply((lambda x: re.sub('[^a-zA-z0-9\s]','',x)))
max_fatures = 2000
tokenizer = Tokenizer(num_words=max_fatures, split=' ')
tokenizer.fit_on_texts(data['text'].values)
X = tokenizer.texts_to_sequences(data['text'].values)
X = pad_sequences(X)
embed_dim = 128
lstm_out = 196
model = Sequential()
model.add(Embedding(max_fatures, embed_dim,input_length = X.shape[1]))
model.add(Dropout(0.5))
model.add(LSTM(lstm_out, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(2,activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
print(model.summary())
Y = pd.get_dummies(data['airline_sentiment']).values
X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size = 0.33, random_state = 42)
X_val = X_train[:500]
Y_val = Y_train[:500]
partial_X_train = X_train[500:]
partial_Y_train = Y_train[500:]
batch_size = 512
history = model.fit(partial_X_train,
partial_Y_train,
epochs = 10,
batch_size=batch_size,
validation_data=(X_val, Y_val))
有几种方法可以做到这一点。一个简单的想法是使用 TF-IDF 作为特征重要性的度量。您可以在调用 fit_on_texts
.
您可以简单地过滤掉低于某个阈值的值:看看 Text Preprocessing
无论如何,我不建议走这条路,因为你正在处理深度学习,你的模型应该自动学习这个重要性。此外,您正在使用词嵌入,减少出现的词将影响这些嵌入。
你的字典(max_fatures)很低,我会先开始增加这个数字。 Keras 丢弃所有其他标记:
num_words: None or int. Maximum number of words to work with (if set, tokenization will be restricted to the top num_words most common words in the dataset)