ciasimbaya/TimeSeries
0
1from datetime import datetime2import numpy as np3import pandas as pd4from sklearn.ensemble import RandomForestRegressor5import gradio as gr6import plotly.graph_objects as go7from huggingface_hub import from_pretrained_keras8import os9 10 11def predictAirPassengers(df, split):12 ts= pd.read_csv('DemandaQuito.csv')13 df2 =ts.copy()14 ttSplit=split/10015 ts['Month']=pd.to_datetime(ts['Month'])16 ts.rename(columns={'#Valor':'Valor'},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 Data23 X=ts.drop("Valor",axis=1)24 Y= ts["Valor"]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 model31 rf = RandomForestRegressor()32 rf.fit(X_train, Y_train)33 34 df1=df2.set_index(['Month'])35 df1.rename(columns={'#Valor':'Valor'},inplace=True)36 train=df1.Valor[:int (len(ts.Valor)*ttSplit)]37 test=df1.Valor[int(len(ts.Valor)*ttSplit):]38 preds=rf.predict(X_test).astype(int) 39 predictions=pd.DataFrame(preds,columns=['Valor'])40 predictions.index=test.index41 predictions.reset_index(inplace=True)42 predictions['Month']=pd.to_datetime(predictions['Month'])43 print(predictions)44 45 #combine all into one table46 ts_df=df.copy()47 ts_df.rename(columns={'#Valor':'Valor'},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={'#Valor':'Valor'},inplace=True)53 df3= predictions54 df2['origin']='ground truth'55 df3['origin']='prediction'56 df4=pd.concat([df2, df3])57 print(df4)58 return df459 60demo = 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='Valor', color='origin')68 #gr.Timeseries(x='Month')69 70 ],71 examples=[72 [os.path.join(os.path.abspath(''), "DemandaQuito_dt.csv"), 75],73 ]74)75 76demo.launch() 