<jupyter_start><jupyter_text>Develop LSTM Models For Univariate Time Series Forecasting<jupyter_code># univariate lstm example
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten

# preparing independent and dependent features
def prepare_data(timeseries_data, n_features):
	X, y =[],[]
	for i in range(len(timeseries_data)):
		# find the end of this pattern
		end_ix = i + n_features
		# check if we are beyond the sequence
		if end_ix > len(timeseries_data)-1:
			break
		# gather input and output parts of the pattern
		seq_x, seq_y = timeseries_data[i:end_ix], timeseries_data[end_ix]
		X.append(seq_x)
		y.append(seq_y)
	return np.array(X), np.array(y)
# define input sequence
timeseries_data = [110, 125, 133, 146, 158, 172, 187, 196, 210]
# choose a number of time steps
n_steps = 3
# split into samples
X, y = prepare_data(timeseries_data, n_steps)
print(X),print(y)
X.shape
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))<jupyter_output><empty_output><jupyter_text>Building LSTM Model<jupyter_code># define model
model = Sequential()
model.add(LSTM(50, activation='relu', return_sequences=True, input_shape=(n_steps, n_features)))
model.add(LSTM(50, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=300, verbose=1)<jupyter_output>Epoch 1/300
1/1 [==============================] - 0s 2ms/step - loss: 32925.4258
Epoch 2/300
1/1 [==============================] - 0s 2ms/step - loss: 32372.4355
Epoch 3/300
1/1 [==============================] - 0s 999us/step - loss: 31777.8184
Epoch 4/300
1/1 [==============================] - 0s 998us/step - loss: 31119.6719
Epoch 5/300
1/1 [==============================] - 0s 999us/step - loss: 30473.8418
Epoch 6/300
1/1 [==============================] - 0s 2ms/step - loss: 29775.2031
Epoch 7/300
1/1 [==============================] - 0s 3ms/step - loss: 29044.7324
Epoch 8/300
1/1 [==============================] - 0s 2ms/step - loss: 28373.2578
Epoch 9/300
1/1 [==============================] - 0s 2ms/step - loss: 27737.3574
Epoch 10/300
1/1 [==============================] - 0s 999us/step - loss: 27057.5215
Epoch 11/300
1/1 [==============================] - 0s 2ms/step - loss: 26266.7969
Epoch 12/300
1/1 [==============================] - 0s 2ms/step - loss: 25363.7910
Epoch[...]<jupyter_text>Predicting For the next 10 data<jupyter_code># demonstrate prediction for next 10 days
x_input = np.array([187, 196, 210])
temp_input=list(x_input)
lst_output=[]
i=0
while(i<10):
    
    if(len(temp_input)>3):
        x_input=np.array(temp_input[1:])
        print("{} day input {}".format(i,x_input))
        #print(x_input)
        x_input = x_input.reshape((1, n_steps, n_features))
        #print(x_input)
        yhat = model.predict(x_input, verbose=0)
        print("{} day output {}".format(i,yhat))
        temp_input.append(yhat[0][0])
        temp_input=temp_input[1:]
        #print(temp_input)
        lst_output.append(yhat[0][0])
        i=i+1
    else:
        x_input = x_input.reshape((1, n_steps, n_features))
        yhat = model.predict(x_input, verbose=0)
        print(yhat[0])
        temp_input.append(yhat[0][0])
        lst_output.append(yhat[0][0])
        i=i+1
    

print(lst_output)
timeseries_data
len(timeseries_data)
lst_output
lst<jupyter_output><empty_output><jupyter_text>Visualizaing The Output<jupyter_code>import matplotlib.pyplot as plt
day_new=np.arange(1,10)
day_pred=np.arange(10,20)
plt.plot(day_new,timeseries_data)
plt.plot(day_pred,lst_output)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21728786677240286, 'fraction_numerical': 0.07850911974623315, 'mean_word_length': 4.716012084592145, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '231272', 'license': 'MIT', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1359, 'n_words': 301, 'path': '4. Time Series/AZ/Misc/UnivariateTimeSeries.ipynb', 'repo_name': 'AmirRazaMBA/TensorFlow-Certification', 'size': '3783'}