Compare commits

..

4 Commits

Author SHA1 Message Date
Maciej Sobkowiak
7203bb3594 Added naive bayes 2021-05-12 20:37:58 +02:00
Maciej Sobkowiak
d4b396f3be Working on bayes2 2021-05-12 20:29:37 +02:00
Maciej Sobkowiak
7ea61e7e1f Read data from files 2021-05-12 20:18:46 +02:00
Maciej Sobkowiak
9ccc657af6 Working on bayes2 2021-05-12 19:58:28 +02:00
3 changed files with 10942 additions and 0 deletions

45
bayes2.py Normal file
View File

@ -0,0 +1,45 @@
import pandas as pd
import numpy as np
import gzip
from sklearn.pipeline import make_pipeline
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import metrics
# Read data
dev = pd.read_table('dev-0/in.tsv', error_bad_lines=False, header=None)
test = pd.read_table('test-A/in.tsv', error_bad_lines=False, header=None)
test_expected = pd.read_table(
'dev-0/expected.tsv', error_bad_lines=False, header=None)
X_train = []
y_train = []
with gzip.open('train/train.tsv.gz', 'r') as f:
for l in f:
line = l.decode('UTF-8').replace("\n", "").split("\t")
y_train.append(int(line[0]))
X_train.append(str(line[1:]))
# Convert to unified types
X_train = np.asarray(X_train)
y_train = np.asarray(y_train)
X_dev = dev[0].values
X_test = test[0].values
# Create model
model = make_pipeline(TfidfVectorizer(), MultinomialNB())
model.fit(X_train, y_train)
# Predict
dev_p = model.predict(X_dev)
test_p = model.predict(X_test)
# Accuracy
score = metrics.accuracy_score(test_expected, dev_p)
print("Accuracy: %0.3f" % score)
# Save to files
dev_p.tofile('./dev-0/out.tsv', sep='\n')
test_p.tofile('./test-A/out.tsv', sep='\n')

5452
dev-0/out.tsv Normal file

File diff suppressed because it is too large Load Diff

5445
test-A/out.tsv Normal file

File diff suppressed because it is too large Load Diff