BlendMMM/Simulator-UOPX
0
1import streamlit as st
2import pandas as pd
3import plotly.express as px
4import plotly.graph_objects as go
5from Eda_functions import format_numbers,line_plot,summary
6import numpy as np
7import re
8
9def sanitize_key(key, prefix=""):
10 # Use regular expressions to remove non-alphanumeric characters and spaces
11 key = re.sub(r'[^a-zA-Z0-9]', '', key)
12 return f"{prefix}{key}"
13
14
15def check_box(options, ad_stock_value,lag_value,num_columns=4, prefix=""):
16 num_rows = -(-len(options) // num_columns) # Ceiling division to calculate rows
17
18 selected_options = []
19 adstock_info = {} # Store adstock and lag info for each selected option
20 if ad_stock_value!=0:
21 for row in range(num_rows):
22 cols = st.columns(num_columns)
23 for col in cols:
24 if options:
25 option = options.pop(0)
26 key = sanitize_key(f"{option}_{row}", prefix=prefix)
27 selected = col.checkbox(option, key=key)
28 if selected:
29 selected_options.append(option)
30
31 # Input minimum and maximum adstock values
32 adstock = col.slider('Select Adstock Range', 0.0, 1.0, ad_stock_value, step=0.05, format="%.2f",key= f"adstock_{key}" )
33
34 # Input minimum and maximum lag values
35 lag = col.slider('Select Lag Range', 0, 7, lag_value, step=1,key=f"lag_{key}" )
36
37 # Create a dictionary to store adstock and lag info for the option
38 option_info = {
39 'adstock': adstock,
40 'lag': lag}
41 # Append the dictionary to the adstock_info list
42 adstock_info[option]=option_info
43
44 else:adstock_info[option]={
45 'adstock': ad_stock_value,
46 'lag': lag_value}
47
48 return selected_options, adstock_info
49 else:
50 for row in range(num_rows):
51 cols = st.columns(num_columns)
52 for col in cols:
53 if options:
54 option = options.pop(0)
55 key = sanitize_key(f"{option}_{row}", prefix=prefix)
56 selected = col.checkbox(option, key=key)
57 if selected:
58 selected_options.append(option)
59
60 # Input minimum and maximum lag values
61 lag = col.slider('Select Lag Range', 0, 7, lag_value, step=1,key=f"lag_{key}" )
62
63 # dictionary to store adstock and lag info for the option
64 option_info = {
65 'lag': lag}
66 # Append the dictionary to the adstock_info list
67 adstock_info[option]=option_info
68
69 else:adstock_info[option]={
70 'lag': lag_value}
71
72 return selected_options, adstock_info
73
74def apply_lag(X, features,lag_dict):
75 #lag_data=pd.DataFrame()
76 for col in features:
77 for lag in range(lag_dict[col]['lag'][0], lag_dict[col]['lag'][1] + 1):
78 if lag>0:
79 X[f'{col}_lag{lag}'] = X[col].shift(periods=lag, fill_value=0)
80 return X
81
82def apply_adstock(X, variable_name, decay):
83 values = X[variable_name].values
84 adstock = np.zeros(len(values))
85
86 for row in range(len(values)):
87 if row == 0:
88 adstock[row] = values[row]
89 else:
90 adstock[row] = values[row] + adstock[row - 1] * decay
91
92 return adstock
93
94def top_correlated_features(df,target,media_data):
95 corr_df=df.drop(target,axis=1)
96 #corr_df[target]=df[target]
97 #st.dataframe(corr_df)
98 for i in media_data:
99 #st.write(media_data[2])
100 #st.dataframe(corr_df.filter(like=media_data[2]))
101 d=(pd.concat([corr_df.filter(like=i),df[target]],axis=1)).corr()[target]
102 d=d.sort_values(ascending=False)
103 d=d.drop(target,axis=0)
104 corr=pd.DataFrame({'Feature_name':d.index,"Correlation":d.values})
105 corr.columns = pd.MultiIndex.from_product([[i], ['Feature_name', 'Correlation']])
106
107 return corr
108
109def top_correlated_features(df,variables,target):
110 correlation_df=pd.DataFrame()
111 for col in variables:
112 d=pd.concat([df.filter(like=col),df[target]],axis=1).corr()[target]
113 #st.dataframe(d)
114 d=d.sort_values(ascending=False).iloc[1:]
115 corr_df=pd.DataFrame({'Media_channel':d.index,'Correlation':d.values})
116 corr_df.columns=pd.MultiIndex.from_tuples([(col, 'Variable'), (col, 'Correlation')])
117 correlation_df=pd.concat([corr_df,correlation_df],axis=1)
118 return correlation_df
119
120def top_correlated_feature(df,variable,target):
121 d=pd.concat([df.filter(like=variable),df[target]],axis=1).corr()[target]
122 # st.dataframe(d)
123 d=d.sort_values(ascending=False).iloc[1:]
124 # st.dataframe(d)
125 corr_df=pd.DataFrame({'Media_channel':d.index,'Correlation':d.values})
126 corr_df['Adstock']=corr_df['Media_channel'].map(lambda x:x.split('_adst')[1] if len(x.split('_adst'))>1 else '-')
127 corr_df['Lag']=corr_df['Media_channel'].map(lambda x:x.split('_lag')[1][0] if len(x.split('_lag'))>1 else '-' )
128 corr_df.drop(['Correlation'],axis=1,inplace=True)
129 corr_df['Correlation']=np.round(d.values,2)
130 sorted_corr_df= corr_df.loc[corr_df['Correlation'].abs().sort_values(ascending=False).index]
131 #corr_df.columns=pd.MultiIndex.from_tuples([(variable, 'Variable'), (variable, 'Correlation')])
132 #correlation_df=pd.concat([corr_df,correlation_df],axis=1)
133 return sorted_corr_df