precipitation-pl/.ipynb_checkpoints/Untitled-checkpoint.ipynb
2022-05-21 18:47:07 +02:00

422 lines
15 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>id_stacji</th>\n",
" <th>rok</th>\n",
" <th>miesiąc</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>249180010</td>\n",
" <td>1981</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>249180010</td>\n",
" <td>1981</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>249180010</td>\n",
" <td>1981</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>249180010</td>\n",
" <td>1981</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>249180010</td>\n",
" <td>1981</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8755</th>\n",
" <td>252230120</td>\n",
" <td>2010</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8756</th>\n",
" <td>252230120</td>\n",
" <td>2010</td>\n",
" <td>9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8757</th>\n",
" <td>252230120</td>\n",
" <td>2010</td>\n",
" <td>10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8758</th>\n",
" <td>252230120</td>\n",
" <td>2010</td>\n",
" <td>11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8759</th>\n",
" <td>252230120</td>\n",
" <td>2010</td>\n",
" <td>12</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>8760 rows × 3 columns</p>\n",
"</div>"
],
"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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>rainfall</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>19.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>43.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>72.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>25.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>89.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8755</th>\n",
" <td>114.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8756</th>\n",
" <td>101.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8757</th>\n",
" <td>20.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8758</th>\n",
" <td>93.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8759</th>\n",
" <td>46.9</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>8760 rows × 1 columns</p>\n",
"</div>"
],
"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<module>\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) <dtype: 'float32'>\n",
"(None, 1) <dtype: 'float32'>\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<module>\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
}