125 lines
2.3 KiB
Python
125 lines
2.3 KiB
Python
# %%
|
|
import lzma
|
|
import sys
|
|
from io import StringIO
|
|
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
import pandas as pd
|
|
import numpy
|
|
|
|
pathX = "./train/train.tsv.xz"
|
|
# pathX = "./train/in.tsv"
|
|
# pathY = "./train/meta.tsv.xz"
|
|
nrows = 100000
|
|
|
|
# %%
|
|
# data = lzma.open(pathX, mode='rt', encoding='utf-8').read()
|
|
# stringIO = StringIO(data)
|
|
# df = pd.read_csv(stringIO, sep="\t", header=None)
|
|
df = pd.read_csv(pathX, sep='\t', nrows=nrows, header=None)
|
|
# df = df.drop(df.columns, axis=1)
|
|
# topics = pd.read_csv(pathY, sep='\t', nrows=nrows, header=None)
|
|
|
|
# %%
|
|
print(len(df.index))
|
|
|
|
# print(len(topics.index))
|
|
|
|
|
|
# %%
|
|
def mergeTexts(a, b, c):
|
|
return str(a) + " " + str(b) + " " + str(c)
|
|
|
|
# %%
|
|
def getMean(a, b):
|
|
return ((a + b)/2)
|
|
|
|
# %%
|
|
df["year"] = df.apply(lambda x: getMean(x[0], x[1]), axis = 1)
|
|
df["text"] = df.apply(lambda x: x[4], axis = 1)
|
|
|
|
# %%
|
|
df = df.drop(columns = [0,1,2,3,4], axis=1)
|
|
df.sample(5)
|
|
|
|
# %%
|
|
topics = df.pop('year')
|
|
df.sample(5)
|
|
|
|
# %%
|
|
topics.sample(5)
|
|
|
|
# %%
|
|
vectorizer = TfidfVectorizer(lowercase=True, stop_words=['polish'])
|
|
X = vectorizer.fit_transform(df.to_numpy().ravel())
|
|
vectorizer.get_feature_names_out()
|
|
|
|
|
|
# %%
|
|
# vectorizer.transform("Ala ma kotka".lower().split())
|
|
|
|
# %%
|
|
# df = df.reset_index()
|
|
|
|
# %%
|
|
tfidfVector = vectorizer.transform(df["text"])
|
|
|
|
|
|
|
|
# %%
|
|
# from sklearn.model_selection import train_test_split
|
|
# from sklearn.naive_bayes import GaussianNB
|
|
#
|
|
# gnb = GaussianNB()
|
|
# gnb.fit(tfidfVector.todense(), topics)
|
|
|
|
# %%
|
|
from sklearn.linear_model import LinearRegression
|
|
|
|
reg = LinearRegression().fit(tfidfVector, topics)
|
|
|
|
|
|
# %%
|
|
testXPath = "./dev-0/in.tsv"
|
|
testYPath = "./dev-0/expected.tsv"
|
|
|
|
testX = pd.read_csv(testXPath, sep='\t', nrows=19998, header=None)
|
|
|
|
testY = pd.read_csv(testYPath, sep='\t', nrows=19998, header=None)
|
|
|
|
|
|
# %%
|
|
testX.sample(5)
|
|
|
|
# %%
|
|
testY.sample()
|
|
|
|
|
|
# %%
|
|
testXtfidfVector = vectorizer.transform(testX[0])
|
|
|
|
|
|
# %%
|
|
reg.score(testXtfidfVector, testY[0])
|
|
|
|
|
|
# %%
|
|
testXPath = "./dev-1/in.tsv"
|
|
testYPath = "./dev-1/out.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 = reg.predict(testXtfidfVector)
|
|
print(pred)
|
|
|
|
import csv
|
|
with open(testYPath, 'w', newline='') as f_output:
|
|
tsv_output = csv.writer(f_output, delimiter='\n')
|
|
tsv_output.writerow(pred)
|
|
|
|
|