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

15 KiB
Raw Blame History

# 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')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9948/1827701549.py in <module>
----> 1 df = np.asarray(df).astype('float32')

ValueError: 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)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_9948/1196997991.py in <module>
      1 model.compile(loss= "mean_squared_error" , optimizer="adam", metrics=["mean_squared_error"])
----> 2 model.fit(df, y, epochs=20)

~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

~\anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
    100       dtype = dtypes.as_dtype(dtype).as_datatype_enum
    101   ctx.ensure_initialized()
--> 102   return ops.EagerTensor(value, ctx.device_name, dtype)
    103 
    104 

ValueError: 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)))