end clustering

pull/4/head
Bastien OLLIER 11 months ago
parent 4ae8512dcb
commit 5bf5f507a5

@ -2,47 +2,34 @@ import streamlit as st
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
st.header("clustering et Prediction")
st.header("Clustering")
if "data" in st.session_state:
data = st.session_state.data
with st.form("my_form"):
header = st.columns([2,1,2])
header[0].subheader("Dispersion")
header[1].subheader("Number of clusters")
header[2].subheader("Data Name")
row1 = st.columns([1,1,1])
n_clusters = row1[0].selectbox("Number of clusters", range(1, 10))
data_name = row1[1].multiselect("Data Name",data.select_dtypes(include="number").columns, max_selections=2)
n_init = row1[2].number_input("n_init",step=1,min_value=1)
row1 = st.columns([2,1,2])
cluster_std = row1[0].slider("", 0.2, 3.0, 0.2, 0.2)
n_clusters = row1[1].selectbox("", range(1, 10))
data_name = row1[2].selectbox("", data.columns)
row2 = st.columns([1,1])
max_iter = row1[0].number_input("max_iter",step=1,min_value=1)
st.form_submit_button('launch')
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import streamlit as st
import random
# Points generator
x, _ = make_blobs(n_samples=200, n_features=2, centers=5, cluster_std=cluster_std, shuffle=True, random_state=10)
st.form_submit_button('launch')
x = data[["Unit Price","Unit Cost"]].to_numpy()
if len(data_name) == 2:
x = data[data_name].to_numpy()
# k-means algorithm
kmeans = KMeans(n_clusters=n_clusters, init='random', n_init=10, max_iter=300, random_state=111)
y_kmeans = kmeans.fit_predict(x)
kmeans = KMeans(n_clusters=n_clusters, init='random', n_init=n_init, max_iter=max_iter, random_state=111)
y_kmeans = kmeans.fit_predict(x)
# Plotting colored clusters
fig, ax = plt.subplots(figsize=(12,8))
plt.scatter(x[:, 0], x[:, 1], s=100, c=kmeans.labels_, cmap='Set1')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=400, marker='*', color='k')
st.pyplot(fig)
fig, ax = plt.subplots(figsize=(12,8))
plt.scatter(x[:, 0], x[:, 1], s=100, c=kmeans.labels_, cmap='Set1')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=400, marker='*', color='k')
st.pyplot(fig)
else:
st.error("file not loaded")
# Cached function that returns a mutable object with a random number in the range 0-10
Loading…
Cancel
Save