naive bayes
This commit is contained in:
parent
9cb2fb2612
commit
64d685de67
30
bayes.py
Normal file
30
bayes.py
Normal file
@ -0,0 +1,30 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import gzip
|
||||
from sklearn.naive_bayes import MultinomialNB
|
||||
from sklearn.feature_extraction.text import CountVectorizer
|
||||
|
||||
bayes = MultinomialNB()
|
||||
vec_q = CountVectorizer()
|
||||
|
||||
with gzip.open('./train/train.tsv.gz', 'rb') as f:
|
||||
train = pd.read_csv(f, error_bad_lines=False, header=None, sep="\t")
|
||||
dev = pd.read_csv("./dev-0/in.tsv", error_bad_lines=False, header=None, sep="\t")
|
||||
test = pd.read_csv("./test-A/in.tsv", error_bad_lines=False, header=None, sep="\t")
|
||||
|
||||
# model
|
||||
X_train, y_train = train[0].astype(str).tolist(), train[1].astype(str).tolist()
|
||||
y_train=vec_q.fit_transform(y_train)
|
||||
bayes.fit(y_train, X_train)
|
||||
|
||||
# dev
|
||||
X_dev = dev[0].astype(str).tolist()
|
||||
y_dev = vec_q.transform(X_dev)
|
||||
dev_pred = bayes.predict(y_dev)
|
||||
pd.DataFrame(dev_pred).to_csv('./dev-0/out.tsv', sep='\t', index=False, header=False)
|
||||
|
||||
# test
|
||||
X_test = train[0].astype(str).tolist()
|
||||
y_test = vec_q.transform(X_test)
|
||||
test_pred = bayes.predict(y_test)
|
||||
pd.DataFrame(test_pred).to_csv('./test-A/out.tsv', sep='\t', index=False, header=False)
|
5452
dev-0/out.tsv
Normal file
5452
dev-0/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
98129
test-A/out.tsv
Normal file
98129
test-A/out.tsv
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user