text_plain_output_1.png
from keras.callbacks import ModelCheckpoint

from keras.layers import Input,Dense

from keras.models import Model

from keras.optimizers import Nadam

from sklearn.preprocessing import MinMaxScaler

import math

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

df = pd.read_csv('../input/renfe.csv')

df.isna().sum()

df.dropna(inplace=True)

df.drop(['Unnamed: 0'], axis=1, inplace=True)
df.drop(['insert_date'], axis=1, inplace=True)

k = df['train_type'].unique()
l = [x for x in range(len(k))]
df['train_type'].replace(k, l, inplace=True)

k = df['train_class'].unique()
l = [x for x in range(len(k))]
df['train_class'].replace(k, l, inplace=True)

k = df['fare'].unique()
l = [x for x in range(len(k))]
df['fare'].replace(k, l, inplace=True)

f,ax = plt.subplots(figsize=(6, 6))
sns.heatmap(df.corr(), annot=True, cmap = "Blues", linewidths=.5, fmt= '.2f',ax = ax)
plt.show()

hr_cos = []
hr_sin = []
min_cos = []
min_sin = []
data = df['start_date'].values
for i in range(len(data)):
    time_obj = dt.datetime.strptime(data[i], '%Y-%m-%d %H:%M:%S')
    hr = time_obj.hour
    minute = time_obj.minute
    sample_hr_sin = math.sin(hr * (2.0 * math.pi / 24))
    sample_hr_cos = math.cos(hr * (2.0 * math.pi / 24))
    sample_min_sin = math.sin(minute * (2.0 * math.pi / 60))
    sample_min_cos = math.cos(minute * (2.0 * math.pi / 60))
    hr_cos.append(sample_hr_cos)
    hr_sin.append(sample_hr_sin)
    min_cos.append(sample_min_cos)
    min_sin.append(sample_min_sin)
df['depart_time_hr_sin'] = hr_sin
df['depart_time_hr_cos'] = hr_cos
df['depart_time_min_sin'] = min_sin
df['depart_time_min_cos'] = min_cos
hr_cos = []
hr_sin = []
min_cos = []
min_sin = []
data = df['end_date'].values
for i in range(len(data)):
    time_obj = dt.datetime.strptime(data[i], '%Y-%m-%d %H:%M:%S')
    hr = time_obj.hour
    minute = time_obj.minute
    sample_hr_sin = math.sin(hr * (2.0 * math.pi / 24))
    sample_hr_cos = math.cos(hr * (2.0 * math.pi / 24))
    sample_min_sin = math.sin(minute * (2.0 * math.pi / 60))
    sample_min_cos = math.cos(minute * (2.0 * math.pi / 60))
    hr_cos.append(sample_hr_cos)
    hr_sin.append(sample_hr_sin)
    min_cos.append(sample_min_cos)
    min_sin.append(sample_min_sin)
df['arrival_time_hr_sin'] = hr_sin
df['arrival_time_hr_cos'] = hr_cos
df['arrival_time_min_sin'] = min_sin
df['arrival_time_min_cos'] = min_cos

df.drop(['start_date'], axis=1, inplace=True)
df.drop(['end_date'], axis=1, inplace=True)

f,ax = plt.subplots(figsize=(20, 20))
sns.heatmap(df.corr(), annot=True, cmap = "Greens", linewidths=.5, fmt= '.2f',ax = ax)
plt.show()

places_sc = MinMaxScaler(copy=False)
train_type_sc = MinMaxScaler(copy=False)
train_class_sc = MinMaxScaler(copy=False)
fare_sc = MinMaxScaler(copy=False)
weekday_sc = MinMaxScaler(copy=False)
duration_sc = MinMaxScaler(copy=False)
price_sc = MinMaxScaler(copy=False)
df['origin'] = places_sc.fit_transform(df['origin'].values.reshape(-1, 1))
df['destination'] = places_sc.fit_transform(df['destination'].values.reshape(-1, 1))
df['train_type'] = train_type_sc.fit_transform(df['train_type'].values.reshape(-1, 1))
df['train_class'] = train_class_sc.fit_transform(df['train_class'].values.reshape(-1, 1))
df['fare'] = fare_sc.fit_transform(df['fare'].values.reshape(-1, 1))
df['start_weekday'] = weekday_sc.fit_transform(df['start_weekday'].values.reshape(-1, 1))
df['end_weekday'] = weekday_sc.fit_transform(df['end_weekday'].values.reshape(-1, 1))
df['duration'] = duration_sc.fit_transform(df['duration'].values.reshape(-1, 1))
df['price'] = price_sc.fit_transform(df['price'].values.reshape(-1, 1))

data = df.values
Y = data[:, 3]
X = np.delete(data, 3, 1)
x_train = X[:2223708]
y_train = Y[:2223708]
x_validation = X[2223708:2246398]
y_validation = Y[2223708:2246398]
x_test = X[2246398:]
y_test = Y[2246398:]

input_layer = Input((X.shape[1],))
y = Dense(64, kernel_initializer='he_normal', activation='tanh')(input_layer)
y = Dense(8, kernel_initializer='he_normal', activation='sigmoid')(y)
y = Dense(1, kernel_initializer='he_normal', activation='sigmoid')(y)
y = Dense(1, kernel_initializer='he_normal', activation='tanh')(y)
model = Model(inputs=input_layer, outputs=y)
model.compile(Nadam(), loss='mse')
model.summary()

history = model.fit(x_train, y_train, validation_data=(x_validation, y_validation), epochs=100, batch_size=2048, callbacks=[ModelCheckpoint('best_model.hdf5', monitor='val_loss', mode='min')])

model.load_weights('best_model.hdf5')
scores = model.evaluate(x_test, y_test)
print('Test Set  RMSE(before scaling ):', scores)
pred = model.predict(x_test)
y_test = y_test.reshape(22692, 1)
k = y_test - pred
k = price_sc.inverse_transform(k)
rmse = np.sqrt(np.mean(np.square(k)))
print('Test Set RMSE(after scaling) :', rmse)