paranormal-or-skeptic-ISI-p.../run.py
2022-05-30 09:12:24 +02:00

32 lines
1.4 KiB
Python

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
with open('train/in.tsv', 'r', encoding='utf-8') as f:
x_train = pd.DataFrame([line.strip().split('\t') for line in f.readlines()], columns=['text', 'text_id'])
with open('dev-0/in.tsv', 'r', encoding='utf-8') as f:
x_dev = pd.DataFrame([line.strip().split('\t') for line in f.readlines()], columns=['text', 'text_id'])
with open('train/in.tsv', 'r', encoding='utf-8') as f:
x_test = pd.DataFrame([line.strip().split('\t') for line in f.readlines()], columns=['text', 'text_id'])
y_train = pd.read_csv('train/expected.tsv', sep='\t', names=['paranormal'], encoding='utf-8')
tfidf_vectorizer = TfidfVectorizer(max_df=0.95, max_features=500)
x_train_vectorized = tfidf_vectorizer.fit_transform(x_train['text'].values)
mnb_model = MultinomialNB().fit(x_train_vectorized, y_train.values.ravel())
# Dev data
x_dev_prepared = tfidf_vectorizer.transform(x_dev['text'].values)
predictions = mnb_model.predict(x_dev_prepared)
with open('dev-0/out.tsv', 'w') as f:
for pred in predictions:
f.write(f'{pred}\n')
# Test data
x_test_vectorized = tfidf_vectorizer.transform(x_test['text'].values)
predictions = mnb_model.predict(x_test_vectorized)
with open('test-A/out.tsv', 'w') as f:
for pred in predictions:
f.write(f'{pred}\n')