This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def data_exploration(): | |
df = df1.copy() | |
st.title("Data Exploration via Pandas Profiling", anchor="data-exploration") | |
st.caption("Please wait for a few seconds for the data exploration to be completed") | |
df_profile = ProfileReport(df, explorative=True) | |
st_profile_report(df_profile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set page config | |
st.set_page_config( | |
page_title="Customer Behavior Analysis", | |
page_icon="📊", | |
layout="wide" | |
) | |
st.title("Customer Behavior Analysis", anchor="customer-behavior-analysis") | |
# Upload file | |
uploaded_file = st.file_uploader("Please Import Your Transaction Data", type="csv") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
side = st.sidebar.selectbox("Select your page", ["Home", "Data Exploration", "RFM Analysis", "Market Basket Analysis", "Customer Lifetimes Value"]) | |
if side == "Home": | |
home() | |
elif side == "Data Exploration": | |
data_exploration() | |
elif side == "RFM Analysis": | |
RFM() | |
elif side == "Market Basket Analysis": | |
market_basket() | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def encode_units(x): | |
if x <= 0: | |
return 0 | |
else: | |
return 1 | |
def mba(df, support): | |
df1 = df.copy() | |
df2 = df1[df1.sum(axis=1) > 1] | |
frequent_itemsets = apriori(df2.astype('bool'), min_support=support, use_colnames=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rfm_segmentation(df, technique:int, num_segments:int): | |
if technique == 1: | |
data = df.copy() | |
# K-means clustering | |
X_ = data[['recency', 'frequency', 'monetary']] | |
X = StandardScaler().fit_transform(X_) | |
kms = KMeansInterp( | |
n_clusters=num_segments, random_state=42, | |
ordered_feature_names=X_.columns.tolist(), | |
n_init = 'auto', max_iter = 1000, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
steps = 10**6 | |
transition = model_try.prob_matrix | |
transition_n = transition | |
for i in range(steps): | |
transition_n=np.matmul(transition_n,transition) | |
print("pi = ",transition_n[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MarkovChainPred(object): | |
def __init__(self, transition_matrix, states): | |
""" | |
Initialize the MarkovChain instance. | |
Parameters | |
---------- | |
transition_matrix: 2-D array | |
A 2-D array representing the probabilities of change of | |
state in the Markov Chain. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set up python env in R---- | |
library(survivalmodels) | |
install_pycox(pip = TRUE, install_torch = TRUE) | |
install_keras(pip = TRUE, install_tensorflow = TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Plot all survival curves---- | |
plot(coxtime$prediction(predict_sets = "test")$distr, ind = 1, fun = "survival") | |
# Plot first two survival curves---- | |
plot(coxtime$prediction(predict_sets = "test")$distr[1], "survival", main = "First 2 Survival Curves") | |
lines(coxtime$prediction(predict_sets = "test")$distr[2], "survival", col = 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
coxtime = aggr$resample_result[[1]] | |
coxtime$prediction(predict_sets = "test") |
NewerOlder