df_extensions/space.py
def find_k(self, start: int = 15, stop: Optional[int] = None, step: int = 1):
        """
        Graph the elbow and Silhouette method for finding the optimal k.
        High silhouette value good.
        Parameters are the search space.
        """
        if start and not stop:
            stop = start
            start = 2

        def s_score(kmeans):
            return silhouette_score(self.df, kmeans.labels_, metric="euclidean")

        df = pd.DataFrame({"n": range(start, stop, step)})
        df["k_means"] = df["n"].apply(self.get_clusters)
        df["sum_squares"] = df["k_means"].apply(lambda x: x.inertia_)
        df["silhouette"] = df["k_means"].apply(s_score)

        plt.rcParams["figure.figsize"] = (10, 5)
        plt.subplot(1, 2, 1)
        plt.plot(df["n"], df["sum_squares"], "bx-")
        plt.xlabel("k")
        plt.ylabel("Sum of squared distances")
        plt.title("Elbow Method For Optimal k")

        plt.subplot(1, 2, 2)
        plt.plot(df["n"], df["silhouette"], "bx-")
        plt.xlabel("k")
        plt.ylabel("Silhouette score")
        plt.title("Silhouette Method For Optimal k")
        plt.show()

        Graph the elbow and Silhouette method for finding the optimal k.
        High silhouette value good.
        Parameters are the search space.
        
Graph the elbow and Silhouette method for finding the optimal k.
High silhouette value good.
Parameters are the search space.
Graph the elbow and Silhouette method for finding the optimal k . High silhouette value good . Parameters are the search space .
def find_k(self, start: int = 15, stop: Optional[int] = None, step: int = 1):
        if start and not stop:
            stop = start
            start = 2
        def s_score(kmeans):
            return silhouette_score(self.df, kmeans.labels_, metric="euclidean")
        df = pd.DataFrame({"n": range(start, stop, step)})
        df["k_means"] = df["n"].apply(self.get_clusters)
        df["sum_squares"] = df["k_means"].apply(lambda x: x.inertia_)
        df["silhouette"] = df["k_means"].apply(s_score)
        plt.rcParams["figure.figsize"] = (10, 5)
        plt.subplot(1, 2, 1)
        plt.plot(df["n"], df["sum_squares"], "bx-")
        plt.xlabel("k")
        plt.ylabel("Sum of squared distances")
        plt.title("Elbow Method For Optimal k")
        plt.subplot(1, 2, 2)
        plt.plot(df["n"], df["silhouette"], "bx-")
        plt.xlabel("k")
        plt.ylabel("Silhouette score")
        plt.title("Silhouette Method For Optimal k")
        plt.show()
def find_k ( self , start : int = 15 , stop : Optional [ int ] = None , step : int = 1 ) : if start and not stop : stop = start start = 2 def s_score ( kmeans ) : return silhouette_score ( self . df , kmeans . labels_ , metric = "euclidean" ) df = pd . DataFrame ( { "n" : range ( start , stop , step ) } ) df [ "k_means" ] = df [ "n" ] . apply ( self . get_clusters ) df [ "sum_squares" ] = df [ "k_means" ] . apply ( lambda x : x . inertia_ ) df [ "silhouette" ] = df [ "k_means" ] . apply ( s_score ) plt . rcParams [ "figure.figsize" ] = ( 10 , 5 ) plt . subplot ( 1 , 2 , 1 ) plt . plot ( df [ "n" ] , df [ "sum_squares" ] , "bx-" ) plt . xlabel ( "k" ) plt . ylabel ( "Sum of squared distances" ) plt . title ( "Elbow Method For Optimal k" ) plt . subplot ( 1 , 2 , 2 ) plt . plot ( df [ "n" ] , df [ "silhouette" ] , "bx-" ) plt . xlabel ( "k" ) plt . ylabel ( "Silhouette score" ) plt . title ( "Silhouette Method For Optimal k" ) plt . show ( )
Graph the elbow and Silhouette method for finding the optimal k.
High silhouette value good.
Graph the elbow and Silhouette method for finding the optimal k . High silhouette value good .
"""
        Graph the elbow and Silhouette method for finding the optimal k.
        High silhouette value good.
        Parameters are the search space.
        """
self
start
int
stop
Optional[int]
step
int