<jupyter_start><jupyter_text>Time series forecasting using LSTMs  Prediction of temperature in New Delhi with time series forecasting using RNN method.  Installing Dependencies<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler<jupyter_output><empty_output><jupyter_text>Loading, analysing and pre-processing data<jupyter_code>data = pd.read_csv("C:/Users/anmol/OneDrive/Desktop/testset.csv")
data.columns
data.head()
data['datetime_utc'] = pd.to_datetime(data['datetime_utc'])
data.set_index('datetime_utc', inplace = True)
data = data.resample('D').mean()
df = data[[' _tempm',' _hum']]
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 7480 entries, 1996-11-01 to 2017-04-24
Freq: D
Data columns (total 2 columns):
 _tempm    7336 non-null float64
 _hum      7336 non-null float64
dtypes: float64(2)
memory usage: 175.3 KB<jupyter_text>Replacing NA values<jupyter_code>tmean = df[' _tempm'].mean()
df[' _tempm'].fillna(tmean,inplace = True)
hmean = df[' _hum'].mean()
df[' _hum'].fillna(hmean,inplace = True)
df.info()
df.head()<jupyter_output><empty_output><jupyter_text>Plotting temp v/s Humidity Curve<jupyter_code>plt.figure(figsize=(20,8))
plt.plot(df[' _tempm'], label ="Temperature")
plt.plot(df[' _hum'], label ="Humidity")
plt.title('Temp v/s Humidity in Delhi')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend(loc="upper left")
plt.show()<jupyter_output><empty_output><jupyter_text>Temperature Curve<jupyter_code>plt.figure(figsize=(20,8))
plt.plot(df[' _tempm'], label ="Temperature")
plt.title('Temp Variation in Delhi')
plt.xlabel('Year')
plt.ylabel('Temperature')
plt.legend(loc="upper left")
plt.show()<jupyter_output><empty_output><jupyter_text>Selecting timestep and splitting data into train and test split<jupyter_code>df1 = df[[' _tempm']]
df1.head(10)
scaler = MinMaxScaler(feature_range = (-1,1))
sc = scaler.fit_transform(df1)
sc
timestep = 40
X = []
Y = []
for i in range(len(sc) - timestep):
    X.append(sc[i:i+timestep])
    Y.append(sc[i+timestep])
X = np.asanyarray(X)
Y = np.asanyarray(Y)
no_of_ex = 7200
X_train = X[:no_of_ex,:,:]
X_test = X[no_of_ex:,:,:]
Y_train = Y[:no_of_ex]
Y_test = Y[no_of_ex:]
print(X_train.shape)
print(Y_train.shape)
print(X_test.shape)
print(Y_test.shape)<jupyter_output>(7200, 40, 1)
(7200, 1)
(240, 40, 1)
(240, 1)<jupyter_text>LSTM Model<jupyter_code>from keras.models import Sequential 
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import RepeatVector
from keras.layers import TimeDistributed
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
model = Sequential()
model.add(Conv1D(filters = 256, kernel_size = 2 , activation = "relu", input_shape = (40,1)))
model.add(Conv1D(filters = 128, kernel_size = 2 , activation = "relu"))
model.add(MaxPooling1D(pool_size = 2))
#prepares a vector for a fully conected layer
model.add(Flatten())
model.add(RepeatVector(30))
model.add(LSTM(128, activation = 'relu'))
model.add(Dense(100, activation = 'relu'))
model.add(Dense(1))
model.compile(loss = 'mse', optimizer = 'adam')
model.fit(X_train,Y_train,epochs = 300, verbose = 0)
from sklearn.metrics import mean_squared_error<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>preds_cnn1 = model.predict(X_test)
preds_cnn1 = scaler.inverse_transform(preds_cnn1)


Y_test=np.asanyarray(Y_test)  
Y_test=Y_test.reshape(-1,1) 
Y_test = scaler.inverse_transform(Y_test)


Y_train=np.asanyarray(Y_train)  
Y_train=Y_train.reshape(-1,1) 
Y_train = scaler.inverse_transform(Y_train)

mean_squared_error(Y_test,preds_cnn1)
plt.figure(figsize=(20,9))
plt.plot(Y_test , 'blue', linewidth=5)
plt.plot(preds_cnn1,'r' , linewidth=4)
plt.legend(('Test','Predicted'))
plt.show()<jupyter_output><empty_output><jupyter_text>Forecasting on Test Set<jupyter_code>def insert_end(Xi, new_example):
    for i in range(timestep-1):
        Xi[:,i,:]= Xi[:,i+1,:]
    Xi[:,timestep-1,:] = new_example
    return Xi
first = 0
future = 300
forecast_cnn = []
Xi = X_test[first:first+1,:,:]
for i in range(future):
    out = model.predict(Xi, batch_size=1)
    forecast_cnn.append(out[0,0])
    Xi = insert_end(Xi,out[0,0])
forecasted_output_cnn=np.asanyarray(forecast_cnn)   
forecasted_output_cnn=forecasted_output_cnn.reshape(-1,1) 
forecasted_output_cnn = scaler.inverse_transform(forecasted_output_cnn)
plt.figure(figsize=(16,9))
plt.plot(Y_test , 'black', linewidth=4)
plt.plot(forecasted_output_cnn,'r' , linewidth=4)
plt.legend(('test','Forecasted'))
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14759676733304977, 'fraction_numerical': 0.030837941301573798, 'mean_word_length': 7.150779896013865, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '486198', 'license': 'MIT', 'n_tokens_mistral': 1996, 'n_tokens_neox': 1854, 'n_words': 336, 'path': 'weather prediction using time series forecasting/Delhi Weather Prediction.ipynb', 'repo_name': 'gauraverma33/Data-is-Beautiful', 'size': '4702'}