Skip to content

[🚧] Use NN with TFIDF #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions model/ensemble_classification.py
Original file line number Diff line number Diff line change
@@ -14,24 +14,39 @@

class EnsembleClassification:
def __init__(self):
self.tfidf_vectorizer = TfidfVectorizer(tokenizer=lambda x: x, lowercase=False)
self.classifier = ExtraTreesClassifier()
self.tfidf_vectorizer = TfidfVectorizer(tokenizer=lambda x: x, lowercase=False, max_features=10000)
# self.classifier = ExtraTreesClassifier()

model = Sequential()
model.add(Dense(512, activation='relu', input_dim=10000))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(7, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
self.classifier = model

def train(self, X, Y):
x_tfidf = self.tfidf_vectorizer.fit_transform(X)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(x_tfidf, Y,
test_size=0.2,
random_state=42,
stratify=Y)
# one-hot encoding
num_classes = len(np.unique(Y))
Y = tf.keras.utils.to_categorical(Y, num_classes=num_classes)
print(Y)

self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
self.X_train = self.tfidf_vectorizer.fit_transform(self.X_train).toarray()
self.X_test = self.tfidf_vectorizer.transform(self.X_test).toarray()

self.classifier.fit(self.X_train, self.y_train)
self.classifier.fit(self.X_train, self.y_train, epochs=50, batch_size=32,
validation_data=(self.X_test, self.y_test))

def predict(self, text):
X = self.tfidf_vectorizer.transform(text)
X = self.tfidf_vectorizer.transform(text).toarray()
return self.classifier.predict(X)

def predict_with_probability(self, text):
X = self.tfidf_vectorizer.transform(text)
X = self.tfidf_vectorizer.transform(text).toarray()
return self.classifier.predict_proba(X)

def classification_report(self):
12 changes: 7 additions & 5 deletions research/classifier_runner.py
Original file line number Diff line number Diff line change
@@ -24,25 +24,27 @@

for i in range(7):
print(f'Label {i}: {len(X[Y == i])}')
all_words = [text for subtext in X[Y == i] for text in subtext]
word_cloud_generator.generate(' '.join(all_words), f'label_{label_names[i]}.png')
# all_words = [text for subtext in X[Y == i] for text in subtext]
# word_cloud_generator.generate(' '.join(all_words), f'label_{label_names[i]}.png')

model = EnsembleClassification()
model.train(X, Y)

FileIo.save_obj('model', model)
FileIo.save_obj('tokenizer', tokenizer)

print(model.classification_report())
print(model.confusion_matrix(labels=label_names))
# print(model.classification_report())
# print(model.confusion_matrix(labels=label_names))

loaded_model = FileIo.load_obj('model')
loaded_tokenizer = FileIo.load_obj('tokenizer')
cached_tokens = FileIo.cached('test_nltk_tokens.pkl', lambda x: loaded_tokenizer.tokenize(x))
test_csv = pd.read_csv(config.input_file('toxic_comment_verify.csv'))

X = cached_tokens(test_csv['comment_text'].to_numpy())
Y = loaded_model.predict_with_probability(X)
# Y = loaded_model.predict_with_probability(X)
Y = loaded_model.predict(X)
print(Y)
print(np.argmax(Y, axis=1))

ids = test_csv['id']