sport-text-classification-ball/run.ipynb
JulianZablonski 87a9c7ca30 task
2022-05-17 22:20:36 +02:00

3.9 KiB

import pandas as pd
import numpy as np

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import make_pipeline
data = pd.read_csv('train/train.tsv', sep='\t', header=None, error_bad_lines=False)
c:\Users\User\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3444: FutureWarning: The error_bad_lines argument has been deprecated and will be removed in a future version.


  exec(code_obj, self.user_global_ns, self.user_ns)
b'Skipping line 3249: expected 2 fields, saw 3\nSkipping line 66393: expected 2 fields, saw 3\nSkipping line 76415: expected 2 fields, saw 3\n'
X = data[1]

with open('dev-0/in.tsv', 'r', encoding='utf8') as f:
    Xdev = f.readlines()
Xdev = pd.Series(Xdev)

with open('test-A/in.tsv', 'r', encoding='utf8') as f:
    Xtest = f.readlines()
Xtest = pd.Series(Xtest)
y = data[0].astype('string')

ydev = pd.read_csv('dev-0/expected.tsv', sep='\t', header=None)
ydev = ydev.squeeze()
model = make_pipeline(TfidfVectorizer(), MultinomialNB())
model.fit(X, y)
Pipeline(steps=[('tfidfvectorizer', TfidfVectorizer()),
                ('multinomialnb', MultinomialNB())])
predictions_dev0 = model.predict(Xdev)
predictions_dev0 = pd.Series(predictions_dev0)
predictions_dev0 = predictions_dev0
with open('dev-0/out.tsv', 'wt') as f:
    for pred in predictions_dev0:
        f.write(str(pred)+'\n')
predictions_testA = model.predict(Xtest)
predictions_testA = pd.Series(predictions_testA)
predictions_testA = predictions_testA
with open('test-A/out.tsv', 'wt') as f:
    for pred in predictions_testA:
        f.write(str(pred)+'\n')