from sklearn.feature_extraction.text import TfidfVectorizer import pandas as pd from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB 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}/out.tsv') if __name__ == '__main__': main()