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