3.1 KiB
3.1 KiB
import sklearn
from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
from sklearn.naive_bayes import MultinomialNB
from sklearn.preprocessing import LabelEncoder
def getInput(path):
with open(path,encoding='utf-8') as f:
return f.readlines()
!pwd
/c/Users/mkoci/Desktop/naiwny_bayes
train_in=getInput('./train/in.tsv')
train_expected=getInput('./train/expected.tsv')
test_in=getInput('./test-A/in.tsv')
dev_in=getInput('./dev-0/in.tsv')
dev_expected=getInput('./dev-0/expected.tsv')
pipeline = make_pipeline(TfidfVectorizer(),MultinomialNB())
encTransform = LabelEncoder().fit_transform(train_expected)
model = pipeline.fit(train_in, encTransform)
dev_predicted = model.predict(dev_in)
test_predicted = model.predict(test_in)
with open('./dev-0/out.tsv', "w") as result:
for out in dev_predicted:
result.write(str(out) + '\n')
with open('./test-A/out.tsv', "w") as result:
for out in test_predicted:
result.write(str(out) + '\n')
!jupyter nbconvert --to script Naiwny_bayes.ipynb
[NbConvertApp] Converting notebook Naiwny_bayes.ipynb to script [NbConvertApp] Writing 1337 bytes to Naiwny_bayes.py