-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
1458 lines (1373 loc) · 67.6 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import flask
import pandas as pd
import numpy as np
import scipy
from scipy.cluster import hierarchy as hc
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash_table.Format import Format, Group, Scheme
from dash.dependencies import Input, Output
import dash_daq as daq
from sklearn.svm import LinearSVC
from sklearn.inspection import partial_dependence
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, label_binarize, RobustScaler
from sklearn.metrics import auc, accuracy_score, confusion_matrix, mean_squared_error
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
import statsmodels.api as sm
import xgboost as xgb
from xgboost import XGBRegressor
import gc
import plotly.express as px
import plotly.figure_factory as ff
import plotly.graph_objects as go
from datetime import timedelta
external_stylesheets = [dbc.themes.LITERA]
server = flask.Flask(__name__)
app = dash.Dash(__name__, external_stylesheets=external_stylesheets, server=server)
colors = {'background': '#F1EEE6'}
#Load data
def load_csv(path):
return pd.read_csv(path,
engine='c',
parse_dates=True,
infer_datetime_format=True,
low_memory=False)
def min_max(df):
min_max_scaler = preprocessing.MinMaxScaler(feature_range=(0,1))
df_scaled = min_max_scaler.fit_transform(df[[item for item in df.drop(columns='date').columns]])
df_scaled= pd.DataFrame(df_scaled)
df_scaled.columns = df.drop(columns=['date']).columns
df = df_scaled.merge(df['date'], left_index=True, right_index=True)
df = df.mask(df==0).fillna(method='backfill')
return df
def fix_missing(df):
df = df.fillna(method='ffill')
df = df.fillna(method='backfill')
return df
path='gimnasio.csv'
# we store original data set as df_original
df_original = load_csv(path)
df_original = df_original.mask(df_original==0).fillna(method='backfill')
df_original['date']=pd.to_datetime(df_original['date'])
df_original = df_original.iloc[:2451]
df = df_original
# preparing dataframe for classification
df = fix_missing(df)
df = min_max(df)
df['date']=pd.to_datetime(df['date'])
col = df.drop(columns='date').columns
for i in col:
df[i] = df[i].map('{:,.3f}'.format)
df
df[col] = df[col].astype('float64')
source =pd.DataFrame(columns=['Features', 'Explanation', 'Range'])
source['Features']=df.columns
source['Explanation'][0]='Twitter Intensity analysys in word "economia" (daily data). Source: Twitter'
source['Explanation'][1]='Twitter Polarization analysys in word "economia" (daily data). Source: Twitter'
source['Explanation'][2]='Twitter Subjectivity analysys in word "economia"(daily data). Source: Twitter'
source['Explanation'][3]='Twitter Intensity analysys in word "gimnasio"(daily data). Source: Twitter'
source['Explanation'][4]='Twitter Polarization analysys in word "gimnasio" (daily data). Source: Twitter'
source['Explanation'][5]='Google trends word "gimnasio". Source: Google (monthly data)'
source['Explanation'][6]='Google trends word "sanitas". Source: Google (monthly data)'
source['Explanation'][7]='Google trends word "adeslas". Source: Google (monthly data)'
source['Explanation'][8]='Google trends word "decathlon". Source: Google (monthly data)'
source['Explanation'][9]='IBEX35 close. Source: Yahoo Finance (daily data)'
source['Explanation'][10]='Growth of fitness industry in spain (year data). Source: Statista 2020'
source['Explanation'][11]='date'
source['Range'][0]='-1 to +1'
source['Range'][1]='-1 to +1'
source['Range'][2]='-1 to +1'
source['Range'][3]='-1 to +1'
source['Range'][4]='-1 to +1'
source['Range'][5]='0 - 100%'
source['Range'][6]='0 - 100%'
source['Range'][7]='0 - 100%'
source['Range'][8]='0 - 100%'
source['Range'][9]='thousands'
source['Range'][10]='0 to 1'
source['Range'][11]='Date Y/M/D'
available_indicators = df.columns.unique()
variable_list = df.drop(columns=['date']).columns.unique().tolist()
dfa = {'Logistic Regression':
[LogisticRegression(fit_intercept=True,max_iter=1000, random_state=0)],
'Linear Support Vector':
[LinearSVC(random_state=0, tol=1e-5)],
#'Random Forest Classifier':
# [RandomForestClassifier(n_estimators=300,
# max_depth=10,
# criterion='entropy',
# bootstrap=True,
# n_jobs=-1,
# random_state=0,
# oob_score=True)],
#'Gradient Boosting Classifier':
# [GradientBoostingClassifier(random_state=0)]
}
dfa = pd.DataFrame(dfa, columns = ['Logistic Regression',
'Linear Support Vector',
#'Random Forest Classifier',
#'Gradient Boosting Classifier'
])
algorithm = dfa.columns.unique().tolist()
#dash/html code
app.layout = html.Div([
dcc.Tabs(id="tabs-with-classes",
parent_className='custom-tabs',
className='custom-tabs-container',
children=[
dcc.Tab(label='DATA SOURCE',
value='tab-1',
className='custom-tab',
selected_className='custom-tab--selected',
children=[
html.H3('Fitness industry in Spain',
style={
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '600',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'5%',
}),
html.P('Copyright Javier Marín (2021). MIT license. ' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'3%',
}),
html.P('We have used open data sources to describe the fitness industry in Spain in 2020. We want to know more about the growing perspectives of this industry according users insights about gyms. We have selected only a few features related with consumers attitudes and motivations regarding this industry. Below you can see each feature explanation and source. ' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'3%',
}),
dash_table.DataTable(id='table1',
columns=[{"name": i, "id": i} for i in source.columns],
data=source.to_dict('records'),
fixed_rows={'headers': True},
style_table={'width':'70%',
'margin-left':'15%',
'margin-bottom':'3%'},
style_cell={'textAlign': 'center',
'font-family': 'Open Sans',
'height':'auto',
'font_size': '15px'
},
),
dash_table.DataTable( id='table2',
columns=[{"name": i, "id": i, 'format': Format(precision=2)} for i in df.columns],
data=df.to_dict('dict'),
page_size=10,
fixed_rows={'headers': True},
style_table={'width':'90%',
'margin-left':'5%'},
style_cell={ 'minWidth': 105,
'maxWidth': 200,
'height':'auto',
'width': 200,
'overflow': 'hidden',
'margin-left': '1%',
'font_size': '15px',
'font-family': 'Open Sans',
},
)
]),
dcc.Tab(label='DATA CLASSIFICATION',
value='tab-2',
className='custom-tab',
selected_className='custom-tab--selected',
children=[
html.Div([
html.H3('Introduction to classification algorithms',
style={
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '600',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'5%',
}),
html.P('We start with a dataset that includes several time-series of different "variables" or "features". With this data set we ara going to do two analysis: "predict" the value of this features in the future (look at the time series tab) or, know more about how this features are correlated between them. The idea is we have a special interest feature and we wonder if we could predict this feature (we will call "target" feature) as a combination of the rest of features. , so we could predict this "target" by knowing the rest of features evolution. This problem becomes a classification problem and gives us lots of insights from our data' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}),
html.Img(src='assets/Introduction.png', style={
'display': 'block',
'margin-left': '11%',
'margin-right': 'auto',
'width': '75%',
'margin-bottom':'1%',
}),
html.P('To make it simplier, we will divide the target feature in two parts: high values (values over average value) and low values (values below average value). So we have a binary classification problem.' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}),
html.H3('1. General overview ',
style={
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '600',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'2%',
}),
html.P('First of all, we need to have a general view of variables in dataset. Following you can see a plot with the comparion between variables pairs to start having an overview. As all this vars are time dependent, you will also see time-dependency plots of the choosen variables. ' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}),
html.Div([ html.Div([
html.Div([
html.P('Select variable for axis x in the plot:' ,
style={
'width': '125%',
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'5px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'30%',
}),
], className="two columns"),
html.Div([
html.P('Select variable for axis y in the plot:' ,
style={
'width': '125%',
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'5px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'135%',
})
], className="two columns")
], className="row")
]),
html.Div([
dcc.Dropdown(
id='xaxis-column',
options=[{'label': i, 'value': i} for i in available_indicators],
value=df.columns[1]
)],
style={
'width': '25%',
'display': 'inline-block',
'font-family': 'Open Sans',
'padding': '25px',
'background-color': '#F1EEE6',
'margin-left': '50px',
}),
html.Div([
dcc.Dropdown(
id='yaxis-column',
options=[{'label': i, 'value': i} for i in available_indicators],
value=df.columns[2]
)],
style={
'width': '25%',
'float': 'right',
'display': 'inline-block',
'font-family': 'Open Sans',
'padding': '25px',
'background-color': '#F1EEE6',
'margin-right': '100px' })
]),
html.Div(children=[
dcc.Graph(id='indicator-graphic')
],
style={
'width':'84%',
'height': '60%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6' ,
'margin-left': '75px',
'margin-bottom': '70px'
}),
html.P('We have represented pairs of variables in a contour plot. We have seen how variables are correlated by pairs. All conclusions from this visual exploration are needed to understant a bit more the dataset and the relation betwen variables. We can include in this comparison the feature we are going to select as "target" and plot aginst other features to see partial dependences in the data set. Rememember that or problem is that we want to know the dependece of all features together with the target variable. If we plot the relation of a feature with our target its a partial dependence because give us only partial information. ' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}),
html.H3('2. Data correlation' ,
style={
'font-family': 'Open Sans',
'padding':'15px',
'font-weight': '600',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'5%',
}),
html.P('Choose the features you consider are the most interesting, or remove the ones you think are not going to be useful. We will start plotting its correlation' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'15px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'3%',
}
),
html.Div([
dcc.Dropdown(
id="variable_list",
options=[{"label": i, "value": i} for i in variable_list],
value=variable_list[:],
multi=True,
)],
style={
'width': '30%',
'display': 'inline-block',
'font-family': 'Open Sans',
'padding': '25px',
'background-color': '#F1EEE6',
'margin-left': '1%'
}
),
html.Div([
dcc.Graph(id='correlation'
)],
style={
'width': '60%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left': '25px',
'right': '50px',
'margin-bottom': '1%'
}
),
html.H3('3. Features importance ',
style={
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '600',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'3%',
}
),
html.P('To predict a target feature as a combination of the rest of features, select target and we will calculate the general variables importance to predict this target in a general classification.' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}
),
html.P('First of all, select the target feature: ' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'6%',
'margin-top':'1%',
}
),
html.Div([
dcc.Dropdown(
id='target',
options=[{'label': i, 'value': i} for i in available_indicators],
value=df.columns[1]
)],
style={
'width': '40%',
'display': 'inline-block',
'font-family': 'Open Sans',
'background-color': '#F1EEE6',
'padding': '25px',
'margin-left': '50px'
}
),
html.P('We have a N-dimensional space (we have 11 features including target). A 1-dimensional space is a line, 2-dimensional space is a plane (for example a 2D plot) and a 3-dimensional space is similar to a cube (a 3D plot). But for N > 3, we do not have direct techniques to visually explore this data. And this is very important for us because would give us an idea if data can be easily divided in two parts for our data visualization (remember, target-High and target-Low) ' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'6%',
'margin-top':'1%',
}
),
html.Img(src='assets/Manifold.png', style={
'display': 'block',
'margin-left': 'auto',
'margin-right': 'auto',
'width': '80%',
'margin-bottom':'5%',
}),
html.P('There are special algorithms that performs what is called "dimensionality reduction". In our case we would like to have all data from our 11 dimensional space embeded into a 2 dimensional space. So we could visualize interesting information from data as for example if is separable (classificable). For doing that we are going to use a very interesting algorithm: "t-DISTRIBUTED STOCHASTIC NEIGHBOR EMBEDDING" that takes a set of points in a high-dimensional space and find a "faithful" representation of these points in a lower-dimensional space. t-sne algorithm is very useful in Machine Learning and also for quantum physics, genetics, thermodynamics, and even gambling. How does it work? Maths behind are hard, but intuitively we can say we create a new low dimensionality representation of this high-dimensionality data by calculating distances betwen points and measuring how much information we learn with our new representation having already known about the data. Algortihm minimizes this amount of information difference in order to be minimum. Ideally the information amount should be the same, but in practice the excercise of creating the new representation forces us to make several assumptions. The idea is that we do not have to gain information from original data and new representation has to be very close in information value, otherwise we are creating a new dataset. Alorithms task is to get another representation having very similar information than original dataset. ' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'6%',
'margin-top':'1%',
'margin-bottom':'2%',
}
),
html.Div([
dcc.Graph(id='tsne'
)],
style={
'width': '80%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left': '10%',
'margin-bottom': '1%'
}
),
html.P('Now you have a dataset visual representation for a binary classification on your target variable in HIGH and LOW values. If the points are not clearly separated, binary classification accuracy is not going to be interesting. If you see t-sne algorithm separates HIGH points from LOW points quite clearly, you will perform this classification with success. Also youcan see how algorithm sometimes tends to group points into clusters and separate them. These are hidden patterns in your data set ¡ ',
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}
),
html.P('All features in dataset has not the same importance when doing a binary classification on your target. To know wich features has more importance is interesting because it can tell you from wich variables your target depends more. But also having this information is imoportant cause alows us eliminate variables having residual importance when performing the classification algorithms. We can save computational resources ¡. Let check in our dataset wich variables are more relevant to perform our classification task:',
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}
),
html.Div([
dcc.Graph(id='feature_importance'
)],
style={
'width': '80%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left': '10%',
'margin-bottom': '1%'
}
),
html.P('We can see now wich variable influences more the classification. We can decide now remove some of these variables. But, ¿how can be sure we are removing not too much variables to keep the classification accuracy? If we delete too much variables, we are reducing original information in our dataset. But there is an optimal number variables we can reduce to keep all information dataset. Lets check ',
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}
),
html.P('In the vertical axis we can see the percentage from the original data and in the horizantal axis we see the number of features. With all features (100%) we have of course the original data (we keel 100% infomration) . But we can see that removing some features we still keep more than 90% of our original data information. The ideal minimum features number would have a correspodence betwen 90 and 95% of original data.',
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}
),
html.Div([
dcc.Graph(id='variance'
)],
style={
'width': '80%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left': '10%',
'margin-bottom': '2%'
}
),
html.P('Another interesting calculation to do in our data is what we have done with features but grouping features in clusters. The dataset information is build by grouping these clusters. Inside a cluster, we can see features close to each other. So we use this property when we remove features (the idea is not remove all features from the same cluster ¡)',
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}
),
html.Img(src='assets/Dendogram.png', style={
'display': 'block',
'margin-left': 'auto',
'margin-right': 'auto',
'width': '70%',
'margin-bottom':'2%',
}),
html.Div([
dcc.Graph(id='dendogram'
)],
style={
'width': '80%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left': '10%',
'margin-bottom': '2%'
}
),
html.Div([ html.H3('4. Classification' ,
style={
'display': 'inline-block',
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '600' ,
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'5%',
}
),
html.P(' We want to answer the following question: With selected target, ¿wich accuracy could we get predicting the target value combining the rest of variables? But first we have to choose the variables you want to work with, together with the target variable. Remember we have shown above wich features are more important for classification, and also wich ones offers similar information to classification algorithms' ,
style={
'display': 'inline-block',
'font-size':'20px',
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'0.5%',
}
),
html.P('With all informtion you have from above, now select the variables you want to use for running classification (you have to select as well the target feature ¡): ' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'4%',
'margin-top':'1%',
}
),
html.Div([
dcc.Dropdown(
id="features_list",
options=[{"label": i, "value": i} for i in variable_list],
value=variable_list[:],
multi=True,
)],
style={
'width': '95%',
#'display': 'inline-block',
'font-family': 'Open Sans',
'padding': '25px',
'background-color': '#F1EEE6',
'margin-left': '2%'
}
),
html.P('Now we will split target values in only two: from targets minimum value to mean value - we will label it as LOW-, and from mean value to maximum - label as HIGH -. Our classification problem will classify betwen this two sets (LOW and HIGH). We want to check if the rest of the features can tell us if target will belong to set LOW or to set HIGH. Also we want to know the accuracy of this classification.' ,
style={
'display': 'inline-block',
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'0.5%',
}
),
html.P('We will start with some basic linear algorithms. Choose an algorithm from the list : ' ,
style={
'display': 'inline-block',
'font-family': 'Open Sans',
'font-size':'20px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'4%',
'margin-bottom':'3%',
}
)
]),
html.Div([
dcc.Dropdown(
id='algorithm',
options=[{'label': i, 'value': i} for i in algorithm],
value='Logistic Regression'
)
],
style={
'width': '40%',
'display': 'inline-block',
'font-family': 'Open Sans',
'background-color': '#F1EEE6',
'margin-left': '30%',
'margin-bottom' : '5%'
}
),
html.Div([
dcc.Graph(id='classification'
)],
style={
'width': '88%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left': '6%',
'margin-bottom':'3%'
}
),
html.P('We are going to look why and how the algorithm has concluded this. First we look at the features that have been more important for each algorithm decission. ' ,
style={
'display': 'inline-block',
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}
),
html.Div([dcc.Graph(id='coefficients'
)],
style={
'width': '88%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left': '6%',
'margin-bottom':'3%'
}
),
html.P('We have seen how much classification depends on each feature. If we want to know how each individual feature affects the result, we need to know how "partially" depends the clasification from this feature (why is called "partial dependence"). In the following plot we show this partial dependence. You have to select a feature that you have choosen for target classification and you will se how how target classification depends on this single feature.' ,
style={
'display': 'inline-block',
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-bottom':'2%',
}
),
html.Div([
dcc.Dropdown(
id='variable',
options=[{'label': i, 'value': i} for i in available_indicators],
value=df.columns[2]
)],
style={
'width': '40%',
'display': 'inline-block',
'font-family': 'Open Sans',
'background-color': '#F1EEE6',
'margin-left': '50px',
'margin-bottom': '34%'
}
),
html.Div([
dcc.Graph(id='partial_dependence'
)],
style={
'width': '47%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left': '2%',
#'margin-top':'15%',
'margin-bottom':'2%'
}
),
]),
dcc.Tab(label='TIME SERIES ANALISYS',
value='tab-3',
className='custom-tab',
selected_className='custom-tab--selected',
children=[
html.H3('Decoding timeseries',
style={
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '600',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'5%',
}),
html.P('Time series forecasting have become the "holy grail" for most Machine Learning scientists. Trends estimation demand is pulling scientists to deal with more and more complex models. Uncertainty, in its metaforic meaning, have become one of the most used words in business strategy. Uncertainty, in its mathematical definition, have allways been there, but assuming high levels of it forces us to use more sophisticated models to forecast time series. Apart from forecasting as output, time series "encodes" a lot of information that can be very useful.' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'3%',
}),
html.P('Select a feature to start visualizing time series: ' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'2%',
}),
html.Div([
dcc.Dropdown(
id='yaxis-column2',
options=[{'label': i, 'value': i} for i in available_indicators],
value=df.columns[6]
)],
style={
'width': '30%',
'display': 'inline-block',
'font-family': 'Open Sans',
'background-color': '#F1EEE6',
'margin-left': '35%',
'margin-bottom':'1%'
}),
html.Div([
dcc.Graph(id='y-time-series2'
)],
style={
'width': '80%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left':'10%'
}),
html.Div([
dcc.Graph(id='y-time-series21'
)],
style={
'width': '40%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left':'10%'
}),
html.Div([
dcc.Graph(id='y-time-series22'
)],
style={
'width': '40%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-right':'5%'
}),
html.P('One of the most important aspects of time series is its stationarity. Time series that shows similar patterns over the time are called stationaries. In Business Intelligence applications the most of time series are stationary. For example, product sales has similar trends every week, month or year. A given product sales can increase during the weekend (from Friday to Monday) because people has more free time and go shoping massively. The same happens every month (people spend more money at the begining of the month for example). An also happens during the year, depending on products, where some seasons concetrate a high amount of sales. And this is happening over and over again. We call this starionarity. In the plots below we can see the same feture you have selected for analysis but with weekly and monthly values. All three plots shows differente stationary behaviours. ' ,
style={
'display': 'inline-block',
'font-size':'20px',
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'0.5%',
}
),
html.Div([
dcc.Graph(id='autocorrelation'
)],
style={
'width': '80%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left':'10%'
}),
html.P('We can plot variable autocorrelation with different time lags or time steps. Plot starts in autocorrelation value of 1, corresponding to time a lag of 0. Then drops down to 0 as time lags are growing. In a feature with no autocorrelation, plot will be oscilating around 0 with very small peaks (with a peak value very near 0). If this peaks are high or the plot does not rapidally drops to near zero, time series has autocorrelation. Where peaks are very high will show that in this time lag there is a high autocorrelation. High peaks can be found at any point, but usually in time series like product sales, this peaks or high values appears in time lags of 7 (week), 30 (month), 120 (quarter) or 360 (year). It means every 7-30-120-360 the same patterns appears over and over again. ',
style={
'display': 'inline-block',
'font-size':'20px',
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'0.5%',
}
),
html.P('¿Why is important autocorrelation? Because when is high, is not possible to predict feature values with ordinary statistical methods. If we do, we can get important errors forecasting features. Autocorrelation is also useful because its presence tells you important things about the variable and potential problems with your model. The autocorrelation function can be used basically to detect non-randomness in data and identify cyclical patterns if present. The good thing is that we can filter this patterns to get an "adjusted" timeseries and to perform better forecasting. ',
style={
'display': 'inline-block',
'font-size':'20px',
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
}
),
html.H5('Can we predict all time series?',
style={
'font-family': 'Open Sans',
'padding':'25px',
'font-weight': '600',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}),
html.P(' We could make predictions in all time series but the error we could get in some of them makes prediction unpractical. Stationary series (with high autocorrelation) needs important adjustmanets to get a simple result. Non-stationary series (with no autocorrelation) can be random, with high noise or chaotic (from chaos theory). Random series has some hints, as well as very noisy series. Chaotic time series are my favourites. This series has "long term memory", encoding very interesting information inside. Generally, chaotic time series shows a great dependence on the initial conditions. Systems very sensitive to initial conditions tends to amplify any small change all over the time, so will make its forecast less predictable. This is an essential aspect of deterministic chaos, small differences in the initial values of a systems can grow into very large differences with time. The reason is that these differences has an exponential growth with time.' ,
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}),
html.P(' There are two very powerful technologies that performs the task of time series forecasting with outstaning results: Probabilistoc Programming and Neural Networs. Probabilistic Programming models are a higher abstract of human thought in contrast to human brain neuronal structure modeled by Neural Networks. Probabilistic Programming therefore models human intuition in terms of incorporating what we are certain or uncertain about. It models how we go about looking for correlation in data and explore what we do not know and refine details in an areas of interest. They are a more intuitive approach to usage in data analysis. Neural Networks are a system made up of a number of simple, highly interconnected processing elements, which process information by their dynamic state response to external inputs. Maths behind both are difficult and computational resources needed are high. In this app we are going to use a simpler but very promising algorithm call XGBoost. It is easier to explain and requires less computational resources.',
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}),
html.P('To understant XGBoost we have to understand decission trees (DT). As we can see in the figure below, DT are a very interesting algortithms for classification. But, can we use then for time series regression (or forecast)? The response is yes. In the same figure we can see how a DT can be applied to a time series regression. We have as well splits, leafs and nodes. ',
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}),
html.Img(src='assets/Trees.png', style={
'display': 'block',
'margin-left': '11%',
'margin-right': 'auto',
'width': '75%',
'margin-bottom':'1%',
}),
html.P(' XGBoost belongs to a class of machine learning models called ensemble methods. It creates a sequence of models such that these models attempt to correct the mistakes of the models before them in the sequence. The first model is built on training data or the original dataset, the second model improves the first model, the third model improves the second, and so on. Now imagine our forcasting problem and how Decission Trees works. Model draws a first DT regressor (splits the series for a certain point as we have seen in the picture above). This regressor is able to make a prediction for some points but not for others. Then, algorithm performs another DT regressor focused on the points that have not been predicted in the first regressor. Then a third RT regressor performs another calculation with points that have not been predicted in in the second regressor, and so on. This process continues and we have a combined final regressor which predicts all the data points correctly.',
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}),
html.Img(src='assets/Split.png', style={
'display': 'block',
'margin-left': '11%',
'margin-right': 'auto',
'width': '75%',
'margin-bottom':'1%',
}),
html.Div([dcc.Graph(id='xgboost'
)],
style={
'width': '80%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left':'10%'
}),
html.P(' In the above graph we see how XGBoost regressor have fitted our feature points. In order to use less computational resources, we have used monthly data. We also have splitted the series in two sections: 70% of points for training the algortihm, and 30% for testing it. In the graph we heve plotted training points section. As accuracy metric, we use Mean Squared Error. Basically it means: when MSE is close to 0, we have done a good regression; higher MSE values, less accurate regression. ',
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',
'background-color': '#F1EEE6',
'margin-left':'2%',
'margin-top':'1%',
}),
html.Div([ dcc.Graph(id='split'
)],
style={
'width': '80%',
'display': 'inline-block',
'box-shadow': '10px 5px 12px #DBCBBE',
'border-radius': '5px',
'background-color': '#F1EEE6',
'margin-left':'10%'
}),
html.P(' You may wonder ¿at wich point algorithm decides first split?, ¿and rest of splits?. XGBoost uses a computing objective (also called regularized objective) consisting in measuring the difference between the prediction y<sup>t</sup> and target y<sup>p</sup> values, and penalizing the complexity of the model by selecting a model employing simple and predictive functions. In above graph we see wich split has XGBoost algorithm choosen first. ¿Is this information useful? Could be, as it means this point in the series (or split) is the most important point for performing the regression, so we should take care what happened in this point (something important happened here). Algorithm wants this objective having a value smaller as possible. ',
style={
'font-family': 'Open Sans',
'font-size':'20px',
'padding':'25px',
'font-weight': '400',