Anniek commited on
Commit
ac04146
·
1 Parent(s): f834d90

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def predictAirPassengers(df, split):
2
+ ts= pd.read_csv('AirPassengers.csv')
3
+ df2 =ts.copy()
4
+ ttSplit=split/100
5
+ ts['Month']=pd.to_datetime(ts['Month'])
6
+ ts.rename(columns={'#Passengers':'Passengers'},inplace=True)
7
+ ts=ts.set_index(['Month'])
8
+ ts['months'] = [x.month for x in ts.index]
9
+ ts['years'] = [x.year for x in ts.index]
10
+ ts.reset_index(drop=True, inplace=True)
11
+
12
+ # Split Data
13
+ X=ts.drop("Passengers",axis=1)
14
+ Y= ts["Passengers"]
15
+ X_train=X[:int (len(Y)*ttSplit)]
16
+ X_test=X[int(len(Y)*ttSplit):]
17
+ Y_train=Y[:int (len(Y)*ttSplit)]
18
+ Y_test=Y[int(len(Y)*ttSplit):]
19
+
20
+ # fit the model
21
+ rf = RandomForestRegressor()
22
+ rf.fit(X_train, Y_train)
23
+
24
+ df1=df2.set_index(['Month'])
25
+ df1.rename(columns={'#Passengers':'Passengers'},inplace=True)
26
+ train=df1.Passengers[:int (len(ts.Passengers)*ttSplit)]
27
+ test=df1.Passengers[int(len(ts.Passengers)*ttSplit):]
28
+ preds=rf.predict(X_test).astype(int)
29
+ predictions=pd.DataFrame(preds,columns=['Passengers'])
30
+ predictions.index=test.index
31
+ predictions.reset_index(inplace=True)
32
+ predictions['Month']=pd.to_datetime(predictions['Month'])
33
+ print(predictions)
34
+
35
+ #combine all into one table
36
+ ts_df=df.copy()
37
+ ts_df.rename(columns={'#Passengers':'Passengers'},inplace=True)
38
+ train= ts_df[:int (len(ts_df)*ttSplit)]
39
+ test= ts_df[int(len(ts_df)*ttSplit):]
40
+
41
+ df2['Month']=pd.to_datetime(df2['Month'])
42
+ df2.rename(columns={'#Passengers':'Passengers'},inplace=True)
43
+ df3= predictions
44
+ df2['origin']='ground truth'
45
+ df3['origin']='prediction'
46
+ df4=pd.concat([df2, df3])
47
+ print(df4)
48
+ return df4
49
+
50
+ demo = gr.Interface(
51
+ fn =predictAirPassengers,
52
+ inputs = [
53
+ gr.Timeseries(label="Input for the timeseries", max_rows=1, interactive=False),
54
+ gr.Slider(1, 100, value=75, step=1, label="Train test split percentage"),
55
+ ],
56
+ outputs= [
57
+ gr.LinePlot(x='Month', y='Passengers', color='origin', width=200)
58
+ #gr.Timeseries(x='Month')
59
+
60
+ ],
61
+ examples=[
62
+ [os.path.join(os.path.abspath(''), "AirPassengers_dt.csv"), 75],
63
+ ]
64
+ )
65
+
66
+ demo.launch()