|
import joblib |
|
import pandas as pd |
|
import matplotlib.pyplot as plt |
|
import streamlit as st |
|
from sklearn.preprocessing import StandardScaler |
|
from sklearn.decomposition import PCA |
|
from scipy.cluster.hierarchy import fcluster |
|
|
|
|
|
scaler = joblib.load('scaler.sav') |
|
pca = joblib.load('pca_model.sav') |
|
kmeans = joblib.load('kmeans_model.sav') |
|
linked = joblib.load('hierarchical_model.sav') |
|
dbscan = joblib.load('dbscan_model.sav') |
|
|
|
|
|
def plot_clusters(data, labels, title): |
|
plt.figure(figsize=(8, 6)) |
|
plt.scatter(data['PC1'], data['PC2'], c=labels, cmap='viridis', s=50) |
|
plt.title(title) |
|
plt.xlabel('Principal Component 1 (PC1)') |
|
plt.ylabel('Principal Component 2 (PC2)') |
|
plt.colorbar() |
|
plt.savefig('plot.png') |
|
plt.close() |
|
return 'plot.png' |
|
|
|
|
|
def process_data(file): |
|
|
|
new_data = pd.read_csv(file) |
|
|
|
new_numerical_data = new_data.drop(columns=['Time']) |
|
|
|
|
|
scaled_new_data = scaler.transform(new_numerical_data) |
|
pca_new_data = pca.transform(scaled_new_data) |
|
|
|
|
|
pca_new_df = pd.DataFrame(pca_new_data, columns=['PC1', 'PC2']) |
|
|
|
|
|
kmeans_new_labels = kmeans.predict(pca_new_df) |
|
hclust_new_labels = fcluster(linked, 3, criterion='maxclust') |
|
dbscan_new_labels = dbscan.fit_predict(pca_new_df) |
|
|
|
|
|
kmeans_plot = plot_clusters(pca_new_df, kmeans_new_labels, 'K-means Clustering') |
|
hclust_plot = plot_clusters(pca_new_df, hclust_new_labels, 'Hierarchical Clustering') |
|
dbscan_plot = plot_clusters(pca_new_df, dbscan_new_labels, 'DBSCAN Clustering') |
|
|
|
return kmeans_new_labels, hclust_new_labels, dbscan_new_labels, kmeans_plot, hclust_plot, dbscan_plot |
|
|
|
|
|
st.title("聚類模型應用") |
|
|
|
|
|
uploaded_file = st.file_uploader("上傳 CSV 檔案", type=["csv"]) |
|
|
|
if uploaded_file is not None: |
|
kmeans_labels, hclust_labels, dbscan_labels, kmeans_plot, hclust_plot, dbscan_plot = process_data(uploaded_file) |
|
|
|
|
|
st.subheader("K-means Labels") |
|
st.text(kmeans_labels) |
|
|
|
|
|
st.subheader("Hierarchical Clustering Labels") |
|
st.text(hclust_labels) |
|
|
|
|
|
st.subheader("DBSCAN Labels") |
|
st.text(dbscan_labels) |
|
|
|
|
|
st.subheader("K-means Clustering Plot") |
|
st.image(kmeans_plot) |
|
|
|
st.subheader("Hierarchical Clustering Plot") |
|
st.image(hclust_plot) |
|
|
|
st.subheader("DBSCAN Clustering Plot") |
|
st.image(dbscan_plot) |
|
|