XPMaster commited on
Commit
c142685
·
1 Parent(s): 042c335

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -184,28 +184,25 @@ with tab1:
184
  st.write("""
185
  ##### 🧠 PCA (Principal Component Analysis) is like looking at a messy room from the best angle to see the most mess. It helps us see our data more clearly!
186
  """)
187
-
188
  # Apply PCA for dimensionality reduction
189
  pca = PCA(n_components=2)
190
  X_transformed = pca.fit_transform(X)
191
  user_features_transformed = pca.transform([user_features])[0]
192
- st.write("Using PCA for visualization.") # Debugging print statement
193
  else:
194
  X_transformed = X[:, :2] # Just use the first two features for visualization
195
  user_features_transformed = user_features[:2]
196
- st.write("Using the first two features for visualization.") # Debugging print statement
 
 
197
 
198
  # K-Means Algorithm
199
  kmeans = KMeans(n_clusters=n_clusters_advanced)
200
  y_kmeans = kmeans.fit_predict(X_transformed)
 
201
 
202
  # Predict the cluster for the user input in the transformed space
203
  predicted_cluster = kmeans.predict([user_features_transformed])
204
 
205
- # Create a DataFrame for easier plotting with plotly
206
- df_transformed = pd.DataFrame(X_transformed, columns=['Feature1', 'Feature2'])
207
- df_transformed['cluster'] = y_kmeans
208
-
209
  # For tab1
210
  fig = go.Figure()
211
 
@@ -218,8 +215,11 @@ with tab1:
218
  hull = ConvexHull(cluster_data[['Feature1', 'Feature2']])
219
  fig.add_trace(go.Scatter(x=x_data[hull.vertices], y=y_data[hull.vertices], fill='toself', fillcolor=px.colors.qualitative.Set1[cluster], opacity=0.5, line=dict(width=0), showlegend=False))
220
 
221
- # Add scatter plot
222
- fig.add_trace(go.Scatter(x=df_transformed['Feature1'], y=df_transformed['Feature2'], mode='markers', marker=dict(color=y_kmeans, colorscale=px.colors.qualitative.Set1), showlegend=False))
 
 
 
223
 
224
  # Add user input as a star marker
225
  fig.add_trace(go.Scatter(x=[user_features_transformed[0]], y=[user_features_transformed[1]], mode='markers', marker=dict(symbol='star', size=30, color='white')))
@@ -236,7 +236,7 @@ with tab1:
236
 
237
  # Update layout
238
  fig.update_layout(width=1200, height=500)
239
-
240
  st.write("""
241
  ### Visualizing Groups
242
  ##### Here are the groups from our tidying method. Each color has a number at its center, representing its group.
 
184
  st.write("""
185
  ##### 🧠 PCA (Principal Component Analysis) is like looking at a messy room from the best angle to see the most mess. It helps us see our data more clearly!
186
  """)
 
187
  # Apply PCA for dimensionality reduction
188
  pca = PCA(n_components=2)
189
  X_transformed = pca.fit_transform(X)
190
  user_features_transformed = pca.transform([user_features])[0]
 
191
  else:
192
  X_transformed = X[:, :2] # Just use the first two features for visualization
193
  user_features_transformed = user_features[:2]
194
+
195
+ # Create a DataFrame for easier plotting with plotly
196
+ df_transformed = pd.DataFrame(X_transformed, columns=['Feature1', 'Feature2'])
197
 
198
  # K-Means Algorithm
199
  kmeans = KMeans(n_clusters=n_clusters_advanced)
200
  y_kmeans = kmeans.fit_predict(X_transformed)
201
+ df_transformed['cluster'] = y_kmeans
202
 
203
  # Predict the cluster for the user input in the transformed space
204
  predicted_cluster = kmeans.predict([user_features_transformed])
205
 
 
 
 
 
206
  # For tab1
207
  fig = go.Figure()
208
 
 
215
  hull = ConvexHull(cluster_data[['Feature1', 'Feature2']])
216
  fig.add_trace(go.Scatter(x=x_data[hull.vertices], y=y_data[hull.vertices], fill='toself', fillcolor=px.colors.qualitative.Set1[cluster], opacity=0.5, line=dict(width=0), showlegend=False))
217
 
218
+ # Add scatter plot based on PCA toggle
219
+ if use_pca:
220
+ fig.add_trace(go.Scatter(x=df_transformed['Feature1'], y=df_transformed['Feature2'], mode='markers', marker=dict(color=y_kmeans, colorscale=px.colors.qualitative.Set1), showlegend=False))
221
+ else:
222
+ fig.add_trace(go.Scatter(x=df_transformed['Feature1'], y=df_transformed['Feature2'], mode='markers', marker=dict(color=y_kmeans, colorscale=px.colors.qualitative.Set1, symbol='square'), showlegend=False))
223
 
224
  # Add user input as a star marker
225
  fig.add_trace(go.Scatter(x=[user_features_transformed[0]], y=[user_features_transformed[1]], mode='markers', marker=dict(symbol='star', size=30, color='white')))
 
236
 
237
  # Update layout
238
  fig.update_layout(width=1200, height=500)
239
+
240
  st.write("""
241
  ### Visualizing Groups
242
  ##### Here are the groups from our tidying method. Each color has a number at its center, representing its group.