File size: 2,397 Bytes
9183c57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import streamlit as st
from sklearn.cluster import KMeans, DBSCAN, AgglomerativeClustering, SpectralClustering
from sklearn.mixture import GaussianMixture

@st.cache_data
def train_select_cluster_model(X_train, n, model_type, model_params=None):
    """
    Trains a clustering model based on the specified model type and parameters.

    Parameters:
    - X_train (array-like): The training data set.
    - n (int): The number of clusters to form or the number of components for the Gaussian Mixture model.
    - model_type (int): An integer representing the type of model to train. 
        1 for KMeans, 2 for DBSCAN, 3 for GaussianMixture, 4 for Hierarchical clustering, and 5 for Spectral clustering.
    - model_params (dict, optional): A dictionary of model-specific parameters. Default is None.

    Returns:
    - The trained clustering model object based on the specified model type.
    """
    if model_type == 1:
        return KMeans_train(X_train, n, model_params)
    elif model_type == 2:
        return DBSCAN_train(X_train, model_params)
    elif model_type == 3:
        return GaussianMixture_train(X_train, n, model_params)
    elif model_type == 4:
        return Hierarchical_train(X_train, n, model_params)
    elif model_type == 5:
        return Spectral_train(X_train, n, model_params)

def KMeans_train(X_train, n_clusters=3, model_params=None):
    if model_params is None: model_params = {}
    kmeans = KMeans(n_clusters=n_clusters, **model_params)
    kmeans.fit(X_train)
    return kmeans

def DBSCAN_train(X_train, model_params=None):
    if model_params is None: model_params = {}
    dbscan = DBSCAN(**model_params)
    dbscan.fit(X_train)
    return dbscan

def GaussianMixture_train(X_train, n_components=1, model_params=None):
    if model_params is None: model_params = {}
    gmm = GaussianMixture(n_components=n_components, **model_params)
    gmm.fit(X_train)
    return gmm

def Hierarchical_train(X_train, n_clusters=3, model_params=None):
    if model_params is None: model_params = {}
    hierarchical = AgglomerativeClustering(n_clusters=n_clusters, **model_params)
    hierarchical.fit(X_train)
    return hierarchical

def Spectral_train(X_train, n_clusters=3, model_params=None):
    if model_params is None: model_params = {}
    spectral = SpectralClustering(n_clusters=n_clusters, **model_params)
    spectral.fit(X_train)
    return spectral