{ "cells": [ { "cell_type": "code", "execution_count": 5, "id": "99f56426", "metadata": {}, "outputs": [], "source": [ "# Import required libraries\n", "import pandas as pd\n", "import numpy as np \n", "import matplotlib.pyplot as plt\n", "import sklearn\n", "\n", "# Import necessary modules\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import mean_squared_error\n", "from math import sqrt\n", "\n", "# Keras specific\n", "import keras\n", "from keras.models import Sequential\n", "from keras.layers import Dense" ] }, { "cell_type": "code", "execution_count": 13, "id": "10b8d271", "metadata": {}, "outputs": [], "source": [ "in_columns = ['id_stacji', 'nazwa_stacji', 'typ_zbioru', 'rok', 'miesiąc']\n", "\n", "df = pd.read_csv('train/in.tsv', names=in_columns, sep='\\t')" ] }, { "cell_type": "code", "execution_count": 18, "id": "0cd1b792", "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
id_stacjirokmiesiąc
024918001019811
124918001019812
224918001019813
324918001019814
424918001019815
............
875525223012020108
875625223012020109
8757252230120201010
8758252230120201011
8759252230120201012
\n", "

8760 rows × 3 columns

\n", "
" ], "text/plain": [ " id_stacji rok miesiąc\n", "0 249180010 1981 1\n", "1 249180010 1981 2\n", "2 249180010 1981 3\n", "3 249180010 1981 4\n", "4 249180010 1981 5\n", "... ... ... ...\n", "8755 252230120 2010 8\n", "8756 252230120 2010 9\n", "8757 252230120 2010 10\n", "8758 252230120 2010 11\n", "8759 252230120 2010 12\n", "\n", "[8760 rows x 3 columns]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.drop(['nazwa_stacji','typ_zbioru'], axis=1)" ] }, { "cell_type": "code", "execution_count": 23, "id": "d4fbb03a", "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", "
rainfall
019.4
143.2
272.2
325.3
489.3
......
8755114.9
8756101.2
875720.4
875893.2
875946.9
\n", "

8760 rows × 1 columns

\n", "
" ], "text/plain": [ " rainfall\n", "0 19.4\n", "1 43.2\n", "2 72.2\n", "3 25.3\n", "4 89.3\n", "... ...\n", "8755 114.9\n", "8756 101.2\n", "8757 20.4\n", "8758 93.2\n", "8759 46.9\n", "\n", "[8760 rows x 1 columns]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = pd.read_csv('train/expected.tsv', sep='\\t', names=['rainfall'])\n", "#y = np.array(y).reshape(1,-1)\n", "y" ] }, { "cell_type": "code", "execution_count": 10, "id": "fdb79c48", "metadata": {}, "outputs": [], "source": [ "# Define model\n", "model = Sequential()\n", "model.add(Dense(500, input_dim=4, activation= \"relu\"))\n", "model.add(Dense(100, activation= \"relu\"))\n", "model.add(Dense(50, activation= \"relu\"))\n", "model.add(Dense(1))\n", "#model.summary() #Print model Summary" ] }, { "cell_type": "code", "execution_count": 28, "id": "483a10d4", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "could not convert string to float: 'PSZCZYNA'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_9948/1827701549.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0masarray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'float32'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: could not convert string to float: 'PSZCZYNA'" ] } ], "source": [ "df['id_stacji'] = np.asarray(df['id_stacji']).astype('float32')" ] }, { "cell_type": "code", "execution_count": 26, "id": "9d7f9ad2", "metadata": {}, "outputs": [], "source": [ "y = np.asarray(y).astype('float32')" ] }, { "cell_type": "code", "execution_count": 25, "id": "10e71137", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(None, 4) \n", "(None, 1) \n", "dense (None, 4) float32\n", "dense_1 (None, 500) float32\n", "dense_2 (None, 100) float32\n", "dense_3 (None, 50) float32\n" ] }, { "data": { "text/plain": [ "[None, None, None, None]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[print(i.shape, i.dtype) for i in model.inputs]\n", "[print(o.shape, o.dtype) for o in model.outputs]\n", "[print(l.name, l.input_shape, l.dtype) for l in model.layers]" ] }, { "cell_type": "code", "execution_count": 27, "id": "827c4d74", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "Failed to convert a NumPy array to a Tensor (Unsupported object type int).", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_9948/1196997991.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcompile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mloss\u001b[0m\u001b[1;33m=\u001b[0m \u001b[1;34m\"mean_squared_error\"\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0moptimizer\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"adam\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmetrics\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"mean_squared_error\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mmodel\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mepochs\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\keras\\utils\\traceback_utils.py\u001b[0m in \u001b[0;36merror_handler\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 65\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# pylint: disable=broad-except\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 66\u001b[0m \u001b[0mfiltered_tb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_process_traceback_frames\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__traceback__\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 67\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwith_traceback\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfiltered_tb\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 68\u001b[0m \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 69\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mfiltered_tb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\tensorflow\\python\\framework\\constant_op.py\u001b[0m in \u001b[0;36mconvert_to_eager_tensor\u001b[1;34m(value, ctx, dtype)\u001b[0m\n\u001b[0;32m 100\u001b[0m \u001b[0mdtype\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdtypes\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_dtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_datatype_enum\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 101\u001b[0m \u001b[0mctx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mensure_initialized\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 102\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mops\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mEagerTensor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mctx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdevice_name\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 103\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 104\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mValueError\u001b[0m: Failed to convert a NumPy array to a Tensor (Unsupported object type int)." ] } ], "source": [ "model.compile(loss= \"mean_squared_error\" , optimizer=\"adam\", metrics=[\"mean_squared_error\"])\n", "model.fit(df, y, epochs=20)" ] }, { "cell_type": "code", "execution_count": null, "id": "f53bf8ef", "metadata": {}, "outputs": [], "source": [ "pred_train= model.predict(X_train)\n", "print(np.sqrt(mean_squared_error(y_train,pred_train)))\n", "\n", "pred= model.predict(X_test)\n", "print(np.sqrt(mean_squared_error(y_test,pred))) " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }