retroc2/run.ipynb
2022-05-18 00:59:01 +02:00

5.2 KiB

import os
import pandas as pd
import numpy as np
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.pipeline import make_pipeline
train = pd.read_csv('train/train.tsv', header=None, sep='\t', error_bad_lines=False)
print(len(train))
train = train[:10000]
D:\Programy\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3444: FutureWarning: The error_bad_lines argument has been deprecated and will be removed in a future version.


  exec(code_obj, self.user_global_ns, self.user_ns)
107463
x_train = train[4]
y_train = train[0]
x_dev_data = pd.read_csv('dev-0/in.tsv', header=None, sep='\t')
x_dev = x_dev_data[0]
x_dev[19999] = "to jest tekst testowy"
x_dev[20000] = "a ten tekst jest najbardziej testowy"
y_dev = pd.read_csv('dev-0/expected.tsv', header=None, sep='\t')
model = make_pipeline(TfidfVectorizer(), LinearRegression())
model.fit(x_train, y_train)
Pipeline(steps=[('tfidfvectorizer', TfidfVectorizer()),
                ('linearregression', LinearRegression())])
dev_predicted =  model.predict(x_dev)

with open('dev-0/out.tsv', 'wt') as f:
    for i in dev_predicted:
        f.write(str(i)+'\n')

dev_out = pd.read_csv('dev-0/out.tsv', header=None, sep='\t')
dev_expected = pd.read_csv('dev-0/expected.tsv', header=None, sep='\t')
def readFile(filename):
    result = []
    with open(filename, 'r', encoding="utf-8") as file:
        for line in file:
            text = line.split("\t")[0].strip()
            result.append(text)
    return result
x_dev = readFile('dev-0/in.tsv')
dev_predicted =  model.predict(x_dev)
with open('dev-0/out.tsv', 'wt') as f:
    for i in dev_predicted:
        f.write(str(i)+'\n')
print(mean_squared_error(dev_out, dev_expected))
with open('test-A/in.tsv', 'r', encoding = 'utf-8') as f:
       x_test = f.readlines()
        
# x_test = pd.Series(x_test)
# x_test = vectorizer.transform(x_test)

test_predicted = model.predict(x_test)

with open('test-A/out.tsv', 'wt') as f:
    for i in test_predicted:
        f.write(str(i)+'\n')
!jupyter nbconvert --to script run.ipynb