Skip to content
Snippets Groups Projects
KMEANS_.py 1.25 KiB
Newer Older
from Packages import *
class Sk_Kmeans:
    """K-Means clustering for Samples selection.

    Returns:
        inertia_ (pd.DataFrame): DataFrame with ...
        x (pd.DataFrame): Initial data
        clu (pd.DataFrame): Cluster name for each sample
        model.cluster_centers_ (pd.DataFrame): Coordinates of the center of each cluster
    """
    def __init__(self, x, max_clusters):
        """Initiate the KMeans class.

        Args:
            x (pd.DataFrame): the original reduced data to cluster
            max_cluster (Int): the max number of desired clusters.
        """
        self.x = x
        self.max_clusters = max_clusters

        self.inertia = pd.DataFrame()
        for i in range(1, max_clusters+1):
            model = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)
            model.fit(x)
            self.inertia[f'{i}_clust']= [model.inertia_]
        self.inertia.index = ['inertia']

    @property
    def inertia_(self):
        return self.inertia

    def fit_optimal(self, nclusters):
        model = KMeans(n_clusters = nclusters, init = 'k-means++', random_state = 42)
        model.fit(self.x)
        yp = model.predict(self.x)+1
        clu = [f'cluster#{i}' for i in yp]

DIANE's avatar
DIANE committed
        return self.x, clu, model.cluster_centers_