retroc2/.ipynb_checkpoints/run-checkpoint.ipynb
2022-05-18 00:12:24 +02:00

9.7 KiB
Raw Blame History

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
train = pd.read_csv('train/train.tsv', header=None, sep='\t', error_bad_lines=False)
print(len(train))
train = train.head(2000)
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')
vectorizer = TfidfVectorizer()
x_train = vectorizer.fit_transform(x_train)
x_dev = vectorizer.transform(x_dev)
model = LinearRegression()
model.fit(x_train.toarray(), y_train)
LinearRegression()
dev_predicted =  model.predict(x_dev.toarray())

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')
---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
C:\Users\SEBAST~1\AppData\Local\Temp/ipykernel_17784/3948937349.py in <module>
----> 1 dev_predicted =  model.predict(x_dev.toarray())
      2 
      3 with open('dev-0/out.tsv', 'wt') as f:
      4     for i in dev_predicted:
      5         f.write(str(i)+'\n')

D:\Programy\anaconda3\lib\site-packages\scipy\sparse\compressed.py in toarray(self, order, out)
   1029         if out is None and order is None:
   1030             order = self._swap('cf')[0]
-> 1031         out = self._process_toarray_args(order, out)
   1032         if not (out.flags.c_contiguous or out.flags.f_contiguous):
   1033             raise ValueError('Output array must be C or F contiguous')

D:\Programy\anaconda3\lib\site-packages\scipy\sparse\base.py in _process_toarray_args(self, order, out)
   1200             return out
   1201         else:
-> 1202             return np.zeros(self.shape, dtype=self.dtype, order=order)
   1203 
   1204 

MemoryError: Unable to allocate 32.2 GiB for an array with shape (20000, 216394) and data type float64
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.toarray())

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