You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.5 KiB
45 lines
1.5 KiB
import streamlit as st
|
|
import matplotlib.pyplot as plt
|
|
from clusters import KMeans_cluster
|
|
|
|
st.header("Clustering: kmeans")
|
|
|
|
if "data" in st.session_state:
|
|
data = st.session_state.data
|
|
|
|
with st.form("my_form"):
|
|
row1 = st.columns([1,1,1])
|
|
n_clusters = row1[0].selectbox("Number of clusters", range(1,data.shape[0]))
|
|
data_name = row1[1].multiselect("Data Name",data.select_dtypes(include="number").columns, max_selections=3)
|
|
n_init = row1[2].number_input("n_init",step=1,min_value=1)
|
|
|
|
row2 = st.columns([1,1])
|
|
max_iter = row1[0].number_input("max_iter",step=1,min_value=1)
|
|
|
|
|
|
st.form_submit_button("launch")
|
|
|
|
if len(data_name) >= 2 and len(data_name) <=3:
|
|
x = data[data_name].to_numpy()
|
|
|
|
kmeans = KMeans_cluster(n_clusters, n_init, max_iter, x)
|
|
y_kmeans = kmeans.run()
|
|
|
|
st.table(kmeans.get_stats())
|
|
|
|
centers = kmeans.centers
|
|
fig = plt.figure()
|
|
if len(data_name) == 2:
|
|
ax = fig.add_subplot(projection='rectilinear')
|
|
plt.scatter(x[:, 0], x[:, 1], c=y_kmeans, s=50, cmap="viridis")
|
|
plt.scatter(centers[:, 0], centers[:, 1], c="black", s=200, marker="X")
|
|
else:
|
|
ax = fig.add_subplot(projection='3d')
|
|
|
|
ax.scatter(x[:, 0], x[:, 1],x[:, 2], c=y_kmeans, s=50, cmap="viridis")
|
|
ax.scatter(centers[:, 0], centers[:, 1], centers[:, 2], c="black", s=200, marker="X")
|
|
st.pyplot(fig)
|
|
|
|
else:
|
|
st.error("file not loaded")
|