Skip to content

Instantly share code, notes, and snippets.

@cindyangelira
cindyangelira / pandas_profiling_streamlit.py
Created January 23, 2023 15:49
Pandas Profiling Streamlit
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)
@cindyangelira
cindyangelira / config_streamlit.py
Created January 23, 2023 15:29
Configuration Streamlit
# 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")
@cindyangelira
cindyangelira / multipage_streamlit.py
Created January 23, 2023 15:24
Multipage Streamlit
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:
@cindyangelira
cindyangelira / marketbasket_streamlit.py
Created January 23, 2023 15:15
Market Basket Analysis Streamlit
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)
@cindyangelira
cindyangelira / rfm_streamlit.py
Created January 23, 2023 14:59
RFM Streamlit
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,
@cindyangelira
cindyangelira / markov_steady_state.py
Created January 23, 2023 14:43
Markov Chain Steady State
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])
@cindyangelira
cindyangelira / markov_predict.py
Created January 23, 2023 14:37
Markov Chain Prediction
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.
@cindyangelira
cindyangelira / setup_pyenv_R.R
Created January 9, 2023 13:39
Set up miniconda env in R
# Set up python env in R----
library(survivalmodels)
install_pycox(pip = TRUE, install_torch = TRUE)
install_keras(pip = TRUE, install_tensorflow = TRUE)
# 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)
@cindyangelira
cindyangelira / surv_pred.R
Created January 9, 2023 12:27
survival_prediction
coxtime = aggr$resample_result[[1]]
coxtime$prediction(predict_sets = "test")