{ "cells": [ { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import math\n", "\n", "from sklearn.feature_extraction.text import TfidfVectorizer\n", "from sklearn.linear_model import LinearRegression\n", "from sklearn.pipeline import make_pipeline\n", "from sklearn.metrics import mean_squared_error" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "data = pd.read_csv('train/train.tsv', sep='\\t', names=['Begin', 'End', 'Title', 'Publisher', 'Text'])" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "data['Year'] = data.apply(lambda row: ((row['Begin'] + row['End'])/2), axis=1)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
TextYear
0nowią część kultury. U nas już nikt ich nie ch...1985.494521
1hlstorja znana w okresie piramid, jak wlaśclcl...1926.475342
2działek. Idąc dalej w swych hipotetycznych roz...2013.963014
3w Warszawie o stosunkach domowych dziatwy szko...1925.500000
4\\\\'iykład: \"Cywilizacyjna Koncepcja dziejów ¥e...1981.500000
.........
107458M. (2) na rzecz powoda M. S. kwotę 5003,66 zł ...2013.058904
107459Zintegrowanego Systemu Informatycznego (ZSI), ...2013.023288
107460prokurator. Wyrokowi temu powołując się na prz...2013.921918
10746107 lipca 2010 r. świadczą o tym, że nie wszyst...2013.083562
107462zatem niezdolności do pracy było schorzenie sa...2013.100000
\n", "

107463 rows × 2 columns

\n", "
" ], "text/plain": [ " Text Year\n", "0 nowią część kultury. U nas już nikt ich nie ch... 1985.494521\n", "1 hlstorja znana w okresie piramid, jak wlaśclcl... 1926.475342\n", "2 działek. Idąc dalej w swych hipotetycznych roz... 2013.963014\n", "3 w Warszawie o stosunkach domowych dziatwy szko... 1925.500000\n", "4 \\\\'iykład: \"Cywilizacyjna Koncepcja dziejów ¥e... 1981.500000\n", "... ... ...\n", "107458 M. (2) na rzecz powoda M. S. kwotę 5003,66 zł ... 2013.058904\n", "107459 Zintegrowanego Systemu Informatycznego (ZSI), ... 2013.023288\n", "107460 prokurator. Wyrokowi temu powołując się na prz... 2013.921918\n", "107461 07 lipca 2010 r. świadczą o tym, że nie wszyst... 2013.083562\n", "107462 zatem niezdolności do pracy było schorzenie sa... 2013.100000\n", "\n", "[107463 rows x 2 columns]" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = data[['Text', 'Year']]\n", "data" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "X = data['Text']\n", "y = data['Year']" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "model = make_pipeline(TfidfVectorizer(), LinearRegression())" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Pipeline(steps=[('tfidfvectorizer', TfidfVectorizer()),\n", " ('linearregression', LinearRegression())])" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(X, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dev0" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20000\n" ] } ], "source": [ "with open('dev-0/in.tsv', 'r', encoding='utf8') as f:\n", " X_dev0 = f.readlines()" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "with open('dev-0/expected.tsv', 'r', encoding='utf8') as f:\n", " y_dev0 = f.readlines()\n", "y_dev0 = pd.Series(y_dev0)\n", "y_dev0 = y_dev0.apply(lambda row: row.replace('\\n', ''))" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [], "source": [ "predictions_dev0 = model.predict(X_dev0)" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21.66807634196494" ] }, "execution_count": 160, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.sqrt(mean_squared_error(y_dev0, predictions_dev0))" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [], "source": [ "with open('dev-0/out.tsv', 'wt') as f:\n", " for pred in predictions_dev0:\n", " f.write(str(pred)+'\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dev1" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [], "source": [ "with open('dev-1/in.tsv', 'r', encoding='utf8') as f:\n", " X_dev1 = f.readlines()" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [], "source": [ "with open('dev-1/expected.tsv', 'r', encoding='utf8') as f:\n", " y_dev1 = f.readlines()\n", "y_dev1 = pd.Series(y_dev1)\n", "y_dev1 = y_dev1.apply(lambda row: row.replace('\\n', ''))" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [], "source": [ "predictions_dev1 = model.predict(X_dev1)" ] }, { "cell_type": "code", "execution_count": 164, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21.943703116726265" ] }, "execution_count": 164, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.sqrt(mean_squared_error(y_dev1, predictions_dev1))" ] }, { "cell_type": "code", "execution_count": 168, "metadata": {}, "outputs": [], "source": [ "with open('dev-1/out.tsv', 'wt') as f:\n", " for pred in predictions_dev1:\n", " f.write(str(pred)+'\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Test" ] }, { "cell_type": "code", "execution_count": 165, "metadata": {}, "outputs": [], "source": [ "with open('test-A/in.tsv', 'r', encoding='utf8') as f:\n", " X_test = f.readlines()" ] }, { "cell_type": "code", "execution_count": 166, "metadata": {}, "outputs": [], "source": [ "predictions_test = model.predict(X_test)" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [], "source": [ "with open('test-A/out.tsv', 'wt') as f:\n", " for pred in predictions_test:\n", " f.write(str(pred)+'\\n')" ] } ], "metadata": { "interpreter": { "hash": "3ecbe772e0e869a386d256c10cc6d948e50cd4df13a3f02e58ab4f2a666d7bf0" }, "kernelspec": { "display_name": "Python 3.8.13 ('eks')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.13" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }