Created
January 23, 2023 14:59
-
-
Save cindyangelira/b2a7c0e398ef67d9def400df1c70dd7c to your computer and use it in GitHub Desktop.
RFM Streamlit
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, | |
feature_importance_method='wcss_min', # or 'unsup2sup' | |
).fit(X) | |
#kmeans = KMeans(n_clusters=num_segments, random_state=42, n_init='auto', max_iter=1000).fit(X) | |
segment = kms.labels_ + 1 | |
data['RFM_Segment'] = segment | |
data['RFM_Segment'] = data['RFM_Segment'].astype(str) | |
feature_importance = kms.feature_importances_ | |
return data, feature_importance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment