コード例 #1
0
 def plot_kelbow_visualizer(self,
                            scorer='distortion',
                            axis=None,
                            plt_show=True):
     return kelbow_visualizer(KMeans(),
                              self.data,
                              k=self.n_clusters,
                              metric=scorer,
                              ax=axis,
                              show=plt_show)
コード例 #2
0
 def test_quick_method_params(self):
     """
     Test the quick method correctly consumes the user-provided parameters
     """
     X, y = make_blobs(centers=3)
     custom_title = "My custom title"
     model = KMeans(3, random_state=13)
     oz = kelbow_visualizer(model,
                            X,
                            sample_weight=np.ones(X.shape[0]),
                            title=custom_title)
     assert oz.title == custom_title
コード例 #3
0
    def test_quick_method(self):
        """
        Test the quick method producing a valid visualization
        """
        X, y = make_blobs(n_samples=1000,
                          n_features=12,
                          centers=8,
                          shuffle=False,
                          random_state=2)

        model = MiniBatchKMeans(3, random_state=43)
        oz = kelbow_visualizer(model, X, random_state=13, legend=False)
        assert isinstance(oz, KElbowVisualizer)

        self.assert_images_similar(oz)
コード例 #4
0
    def __print_elbow(self, k, visualizer=None):
        self.k = k

        self.visualizer = kelbow_visualizer(KMeans(), self, self.k)
        self.visualizer.show()
              col=3)
fig.update_layout(yaxis_title='Value')
plot(fig)

# -------------------------------
# 2. Normalization
customers_data_norm = normalizer(df=customers_data_cleaned,
                                 norm_type='StandardScaler')

# ***************************************************
# ******            Modelling                  ******
# ***************************************************

# -------------------------------
# 1. Extract optimize number of clusters based on the Elbow Method
kelbow_visualizer(KMeans(random_state=4), customers_data_norm, k=(2, 10))

# -------------------------------
# 2. Apply clustering model
clustering_model = KMeans(n_clusters=5, random_state=42)
clustering_model.fit(customers_data_norm)

customers_data_norm['cluster'] = clustering_model.labels_
customers_data_norm[
    'cluster_str'] = 'cluster' + customers_data_norm['cluster'].astype(str)

customers_data['cluster'] = clustering_model.labels_
customers_data['cluster_str'] = 'cluster' + customers_data['cluster'].astype(
    str)

# ***************************************************