create a app with following descriptions: 3D Data Clustering Visualizer Description: Visualize clustering algorithms (like k-means) on 3D data using interactive Plotly 3D scatter plots. Features: Allow users to set the number of clusters, see cluster centroids, and color-code points based on cluster assignment.
To upload files, please first save the app
import streamlit as st
import plotly.express as px
import numpy as np
from sklearn.cluster import KMeans
st.title("3D Data Clustering Visualizer")
# Generate synthetic 3D data
n_samples = st.slider("Number of Samples", 100, 1000, 300)
n_clusters = st.slider("Number of Clusters", 2, 10, 3)
np.random.seed(42)
data = np.random.rand(n_samples, 3)
# Apply k-means clustering
kmeans = KMeans(n_clusters=n_clusters)
kmeans.fit(data)
labels = kmeans.labels_
# Plot 3D clusters
fig = px.scatter_3d(
x=data[:, 0], y=data[:, 1], z=data[:, 2],
color=labels.astype(str),
title="3D K-Means Clustering",
labels={'x': 'X', 'y': 'Y', 'z': 'Z'}
)
st.plotly_chart(fig)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?