nalinrajendran commited on
Commit
135a621
·
1 Parent(s): f001b09

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import pandas as pd
3
+ import math
4
+ import matplotlib.pyplot as plt
5
+ import numpy as np
6
+ import os
7
+ import keras
8
+ import tensorflow as tf
9
+ from sklearn.preprocessing import MinMaxScaler
10
+ from tensorflow.keras.models import Sequential
11
+ from tensorflow.keras.layers import Dense
12
+ from tensorflow.keras.layers import LSTM
13
+ from sklearn.metrics import mean_squared_error
14
+ from sklearn.metrics import mean_absolute_error as MAE
15
+
16
+
17
+ pwd = os.getcwd()
18
+ filepath = os.path.join(pwd,r"DATA/try_data.csv")
19
+
20
+ df = pd.read_csv(filepath)
21
+
22
+ df1 = df.reset_index()['cases']
23
+
24
+ scaler = MinMaxScaler(feature_range=(0,1))
25
+ df1 = scaler.fit_transform(np.array(df1).reshape(-1,1))
26
+
27
+ training_size = int(len(df1)*0.8)
28
+ test_size= len(df1)-training_size
29
+
30
+ train_data,test_data = df1[0:training_size,:],df1[training_size:len(df1),:1]
31
+
32
+ def create_dataset (dataset,time_step=1):
33
+ dataX, dataY = [], []
34
+ for i in range(len(dataset)-time_step-1):
35
+ a = dataset[i:(i+time_step),0]
36
+ dataX.append(a)
37
+ dataY.append(dataset[i+time_step, 0])
38
+ return np.array(dataX), np.array(dataY)
39
+
40
+
41
+ time_step = 2
42
+ X_train, y_train = create_dataset(train_data, time_step)
43
+ X_test, y_test = create_dataset(test_data, time_step)
44
+
45
+
46
+ X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
47
+ X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)
48
+
49
+
50
+ model = Sequential()
51
+ model.add(LSTM(11,return_sequences=True, input_shape=(2,1)))
52
+ model.add(LSTM(11,return_sequences=True))
53
+ model.add(LSTM(11))
54
+ model.add(Dense(1))
55
+ model.compile(loss='mean_squared_error', optimizer = "adam")
56
+
57
+ lin_model=model.fit(X_train, y_train, validation_data=(X_test,y_test), epochs=400, batch_size=38, verbose=1)
58
+
59
+ train_predict=model.predict(X_train)
60
+ test_predict=model.predict(X_test)
61
+
62
+
63
+
64
+ look_back=2
65
+ trainPredictPlot = np.empty_like(df1)
66
+ trainPredictPlot[:, :] = np.nan
67
+ trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict
68
+
69
+
70
+ # shift test predictions for plotting
71
+ testPredictPlot = np.empty_like(df1)
72
+ testPredictPlot[:, :] = np.nan
73
+ testPredictPlot[len(train_predict)+(look_back*2)+1:len(df1)-1, :] = test_predict
74
+
75
+
76
+ # plot baseline and predictions
77
+ plt.figure(figsize = (15,6))
78
+ plt.plot(scaler.inverse_transform(df1),'r',label='Original Data')
79
+ plt.plot(trainPredictPlot,'g',label='Train Data')
80
+ plt.plot(testPredictPlot,'b',label='Test Data')
81
+ plt.xlabel('Months')
82
+ plt.ylabel('Cases')
83
+ plt.show()
84
+
85
+
86
+
87
+ import gradio as gr
88
+ import pandas as pd
89
+ import seaborn as sns
90
+ import matplotlib.pyplot as plt
91
+
92
+ def plot(df):
93
+
94
+
95
+ plt.plot(scaler.inverse_transform(df1),'r',label='Original Data')
96
+ plt.title("Original data that is used for analysis")
97
+ plt.savefig("original_data.png")
98
+ plt.plot(trainPredictPlot,'g',label='Train Data')
99
+ plt.title("Predicting the Trained data from the original data")
100
+ plt.savefig("train_prediction.png")
101
+ plt.plot(testPredictPlot,'b',label='Test Data')
102
+ plt.title("Predicting the case coutn of the year 2020 and 2021")
103
+ plt.savefig("test_prediction.png")
104
+
105
+
106
+ plots = ["Case_count.png","Rainfall_pattern.png","original_data.png","train_prediction.png", "test_prediction.png"]
107
+ return plots
108
+
109
+ inputs = [gr.Dataframe(label="Dengue Data")]
110
+ outputs = [gr.Gallery(label="Inference from the data").style(grid=(5,1))]
111
+ description = "We are given with the dataset of dengue cases with respect to the state Kerala with a span ranging from the year 2011 to 2021.The goal is to predict the cases in a monthly fashion, for the upcoming years.The bar graph indicated the year-wise dengue case count.The Time series plot indicates the rainfall pattern across the period 2011-2021.The red part of the graph is the input data that we have .The green part of the graph is the data with which we trained our model.The blue part of the graph indicates the predicted output(test data) against the red part which is the original data"
112
+
113
+ gr.Interface(plot, inputs=inputs, outputs=outputs, title="Dengue Cases Prediction Dashboard",description=description,allow_flagging="never").launch()
114
+
115
+