15 KiB
15 KiB
# Import required libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn
# Import necessary modules
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from math import sqrt
# Keras specific
import keras
from keras.models import Sequential
from keras.layers import Dense
in_columns = ['id_stacji', 'nazwa_stacji', 'typ_zbioru', 'rok', 'miesiąc']
df = pd.read_csv('train/in.tsv', names=in_columns, sep='\t')
df.drop(['nazwa_stacji','typ_zbioru'], axis=1)
id_stacji | rok | miesiąc | |
---|---|---|---|
0 | 249180010 | 1981 | 1 |
1 | 249180010 | 1981 | 2 |
2 | 249180010 | 1981 | 3 |
3 | 249180010 | 1981 | 4 |
4 | 249180010 | 1981 | 5 |
... | ... | ... | ... |
8755 | 252230120 | 2010 | 8 |
8756 | 252230120 | 2010 | 9 |
8757 | 252230120 | 2010 | 10 |
8758 | 252230120 | 2010 | 11 |
8759 | 252230120 | 2010 | 12 |
8760 rows × 3 columns
y = pd.read_csv('train/expected.tsv', sep='\t', names=['rainfall'])
#y = np.array(y).reshape(1,-1)
y
rainfall | |
---|---|
0 | 19.4 |
1 | 43.2 |
2 | 72.2 |
3 | 25.3 |
4 | 89.3 |
... | ... |
8755 | 114.9 |
8756 | 101.2 |
8757 | 20.4 |
8758 | 93.2 |
8759 | 46.9 |
8760 rows × 1 columns
# Define model
model = Sequential()
model.add(Dense(500, input_dim=4, activation= "relu"))
model.add(Dense(100, activation= "relu"))
model.add(Dense(50, activation= "relu"))
model.add(Dense(1))
#model.summary() #Print model Summary
df['id_stacji'] = np.asarray(df['id_stacji']).astype('float32')
[1;31m---------------------------------------------------------------------------[0m [1;31mValueError[0m Traceback (most recent call last) [1;32m~\AppData\Local\Temp/ipykernel_9948/1827701549.py[0m in [0;36m<module>[1;34m[0m [1;32m----> 1[1;33m [0mdf[0m [1;33m=[0m [0mnp[0m[1;33m.[0m[0masarray[0m[1;33m([0m[0mdf[0m[1;33m)[0m[1;33m.[0m[0mastype[0m[1;33m([0m[1;34m'float32'[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m [1;31mValueError[0m: could not convert string to float: 'PSZCZYNA'
y = np.asarray(y).astype('float32')
[print(i.shape, i.dtype) for i in model.inputs]
[print(o.shape, o.dtype) for o in model.outputs]
[print(l.name, l.input_shape, l.dtype) for l in model.layers]
(None, 4) <dtype: 'float32'> (None, 1) <dtype: 'float32'> dense (None, 4) float32 dense_1 (None, 500) float32 dense_2 (None, 100) float32 dense_3 (None, 50) float32
[None, None, None, None]
model.compile(loss= "mean_squared_error" , optimizer="adam", metrics=["mean_squared_error"])
model.fit(df, y, epochs=20)
[1;31m---------------------------------------------------------------------------[0m [1;31mValueError[0m Traceback (most recent call last) [1;32m~\AppData\Local\Temp/ipykernel_9948/1196997991.py[0m in [0;36m<module>[1;34m[0m [0;32m 1[0m [0mmodel[0m[1;33m.[0m[0mcompile[0m[1;33m([0m[0mloss[0m[1;33m=[0m [1;34m"mean_squared_error"[0m [1;33m,[0m [0moptimizer[0m[1;33m=[0m[1;34m"adam"[0m[1;33m,[0m [0mmetrics[0m[1;33m=[0m[1;33m[[0m[1;34m"mean_squared_error"[0m[1;33m][0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [1;32m----> 2[1;33m [0mmodel[0m[1;33m.[0m[0mfit[0m[1;33m([0m[0mdf[0m[1;33m,[0m [0my[0m[1;33m,[0m [0mepochs[0m[1;33m=[0m[1;36m20[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m [1;32m~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py[0m in [0;36merror_handler[1;34m(*args, **kwargs)[0m [0;32m 65[0m [1;32mexcept[0m [0mException[0m [1;32mas[0m [0me[0m[1;33m:[0m [1;31m# pylint: disable=broad-except[0m[1;33m[0m[1;33m[0m[0m [0;32m 66[0m [0mfiltered_tb[0m [1;33m=[0m [0m_process_traceback_frames[0m[1;33m([0m[0me[0m[1;33m.[0m[0m__traceback__[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [1;32m---> 67[1;33m [1;32mraise[0m [0me[0m[1;33m.[0m[0mwith_traceback[0m[1;33m([0m[0mfiltered_tb[0m[1;33m)[0m [1;32mfrom[0m [1;32mNone[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 68[0m [1;32mfinally[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [0;32m 69[0m [1;32mdel[0m [0mfiltered_tb[0m[1;33m[0m[1;33m[0m[0m [1;32m~\anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py[0m in [0;36mconvert_to_eager_tensor[1;34m(value, ctx, dtype)[0m [0;32m 100[0m [0mdtype[0m [1;33m=[0m [0mdtypes[0m[1;33m.[0m[0mas_dtype[0m[1;33m([0m[0mdtype[0m[1;33m)[0m[1;33m.[0m[0mas_datatype_enum[0m[1;33m[0m[1;33m[0m[0m [0;32m 101[0m [0mctx[0m[1;33m.[0m[0mensure_initialized[0m[1;33m([0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [1;32m--> 102[1;33m [1;32mreturn[0m [0mops[0m[1;33m.[0m[0mEagerTensor[0m[1;33m([0m[0mvalue[0m[1;33m,[0m [0mctx[0m[1;33m.[0m[0mdevice_name[0m[1;33m,[0m [0mdtype[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 103[0m [1;33m[0m[0m [0;32m 104[0m [1;33m[0m[0m [1;31mValueError[0m: Failed to convert a NumPy array to a Tensor (Unsupported object type int).
pred_train= model.predict(X_train)
print(np.sqrt(mean_squared_error(y_train,pred_train)))
pred= model.predict(X_test)
print(np.sqrt(mean_squared_error(y_test,pred)))