AMKhakbaz commited on
Commit
9e4c0ea
·
verified ·
1 Parent(s): 335d6f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -1
app.py CHANGED
@@ -6,6 +6,7 @@ import plotly.graph_objects as go
6
  from scipy.stats import norm, t
7
  from scipy.cluster.hierarchy import linkage, dendrogram, fcluster
8
  import plotly.figure_factory as ff
 
9
 
10
  def sorting(df):
11
  df.index = list(map(float, df.index))
@@ -499,6 +500,18 @@ def hierarchical_clustering_with_plotly(df, linkage_method):
499
 
500
  return df
501
 
 
 
 
 
 
 
 
 
 
 
 
 
502
 
503
  def upload_and_select_dataframe():
504
  st.sidebar.title("File Upload")
@@ -766,7 +779,6 @@ try:
766
 
767
  funnel_percentage_cleaned = funnel_percentage.dropna(axis=0, how='all')
768
 
769
- # اضافه کردن انتخاب ترتیب مرتب‌سازی
770
  columns = st.sidebar.multiselect('Sort by which questions?', sorted(funnel_percentage_cleaned.columns))
771
  sort_order = st.sidebar.radio('Sort Order', ['Ascending', 'Descending'])
772
 
@@ -826,6 +838,46 @@ try:
826
 
827
  st.plotly_chart(fig)
828
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
829
 
830
  elif main_option == "Hypothesis test":
831
  st.header("Hypothesis Testing")
 
6
  from scipy.stats import norm, t
7
  from scipy.cluster.hierarchy import linkage, dendrogram, fcluster
8
  import plotly.figure_factory as ff
9
+ from sklearn.cluster import KMeans
10
 
11
  def sorting(df):
12
  df.index = list(map(float, df.index))
 
500
 
501
  return df
502
 
503
+ from sklearn.cluster import KMeans
504
+
505
+ def kmeans_clustering(df, k):
506
+
507
+ numeric_df = df.select_dtypes(include=['number'])
508
+
509
+ if numeric_df.empty:
510
+ raise ValueError("DataFrame does not contain any numeric columns for clustering.")
511
+
512
+ kmeans = KMeans(n_clusters=k, random_state=0) # You can modify random_state
513
+ df['cluster'] = kmeans.fit_predict(numeric_df)
514
+ return df
515
 
516
  def upload_and_select_dataframe():
517
  st.sidebar.title("File Upload")
 
779
 
780
  funnel_percentage_cleaned = funnel_percentage.dropna(axis=0, how='all')
781
 
 
782
  columns = st.sidebar.multiselect('Sort by which questions?', sorted(funnel_percentage_cleaned.columns))
783
  sort_order = st.sidebar.radio('Sort Order', ['Ascending', 'Descending'])
784
 
 
838
 
839
  st.plotly_chart(fig)
840
 
841
+ elif main_option == "Segmentation Analysis":
842
+ st.header("Segmentation Analysis")
843
+
844
+ cols = edit_strings(df.columns)
845
+ cols = sorted(list(set(cols)))
846
+
847
+ st.sidebar.header("Selection of questions")
848
+ single_list = st.sidebar.multiselect(
849
+ 'Single answer questions',
850
+ cols,
851
+ default=[]
852
+ )
853
+
854
+ multi_list = st.sidebar.multiselect(
855
+ 'Multi answer questions',
856
+ cols,
857
+ default=[]
858
+ )
859
+
860
+ score_list = st.sidebar.multiselect(
861
+ 'Score answer questions',
862
+ cols,
863
+ default=[]
864
+ )
865
+
866
+ matching_cols1 = []
867
+ for i in multi_list:
868
+ matching_cols1 += [col for col in df.columns if is_matching_pattern(col, i)]
869
+
870
+ df_clean = process_dataframe(df[single_list + matching_cols1])
871
+ st.subheader("Selected Table")
872
+ st.dataframe(df_clean)
873
+
874
+ linkage_method = st.sidebar.selectbox("Select the Linkage Method of Segmentation Analysis:", ["Hierarchical Clustering"])
875
+
876
+ df_cluster = hierarchical_clustering_with_plotly(df_clean, linkage_method)
877
+
878
+ st.subheader("Cluster Table")
879
+ st.dataframe(df_clean)
880
+
881
 
882
  elif main_option == "Hypothesis test":
883
  st.header("Hypothesis Testing")