reddit p|s s478874

This commit is contained in:
Jakub Eichner 2022-06-07 16:43:05 +02:00
parent ecfafbf86c
commit b2466f93da
4 changed files with 11604 additions and 1089 deletions

File diff suppressed because it is too large Load Diff

5272
dev-0/expected1.tsv Normal file

File diff suppressed because it is too large Load Diff

91
run.py Normal file
View File

@ -0,0 +1,91 @@
import lzma
import sys
from io import StringIO
from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd
import csv
import numpy
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
pathX = "./train/in.tsv.xz"
pathY = "./train/expected.tsv"
nrows = 10000
def load_data(path):
in_df = pd.read_csv(f'{path}/in.tsv.xz', sep='\t', nrows=nrows, header=None)
try:
exp_df = pd.read_csv(f'{path}/expected.tsv', sep='\t', nrows=nrows, header=None)
except:
exp_df = None
return in_df, exp_df
def write_res(data, path):
with open(path, 'w') as f:
for line in data:
f.write(f'{line}\n')
print(f"Data written {path}/out.tsv")
def main():
in_df, exp_df = load_data('train')
in_df = in_df.drop(in_df.columns[1], axis=1)
vectorizer = TfidfVectorizer(lowercase=True, stop_words=['english'])
X = vectorizer.fit_transform(in_df.to_numpy().ravel())
vectorizer.get_feature_names_out()
# in_df = in_df.reset_index()
tfidfVector = vectorizer.transform(in_df[0])
gnb = GaussianNB()
gnb.fit(tfidfVector.todense(), exp_df)
paths = ['dev-0', 'test-A']
for path in paths:
x = load_data(path)
vec = vectorizer.transform(x[0][0])
result = gnb.predict(vec.todense())
write_res(result, f'{path}/expected.tsv')
if __name__ == '__main__':
main()
# testXPath = "./dev-0/in.tsv.xz"
# testYPath = "./dev-0/expected.tsv"
# testX = pd.read_csv(testXPath, sep='\t', nrows=nrows, header=None)
# testY = pd.read_csv(testYPath, sep='\t', nrows=nrows, header=None)
# testXtfidfVector = vectorizer.transform(testX[0])
# testXPath = "./test-A/in.tsv.xz"
# testYPath = "./test-A/expected.tsv"
# testX = pd.read_csv(testXPath, sep='\t', nrows=nrows, header=None)
# # testY = pd.read_csv(testYPath, sep='\t', nrows=nrows, header=None)
# testXtfidfVector = vectorizer.transform(testX[0])
# pred = gnb.predict(testXtfidfVector.todense())
# print(pred)
# with open(testYPath, 'w', newline='') as f_output:
# tsv_output = csv.writer(f_output, delimiter='\n')
# tsv_output.writerow(pred)

5152
test-A/expected.tsv Normal file

File diff suppressed because it is too large Load Diff