def test_instance_validation(self): clf = self.Clusterer() scikitplot.clustering_factory(clf) not_clf = self.NotClusterer() self.assertRaises(TypeError, scikitplot.clustering_factory, not_clf)
def test_copy(self): np.random.seed(0) clf = KMeans() scikitplot.clustering_factory(clf) ax = clf.plot_silhouette(self.X) assert not hasattr(clf, "cluster_centers_") ax = clf.plot_silhouette(self.X, copy=False) assert hasattr(clf, "cluster_centers_")
def test_ax(self): np.random.seed(0) clf = KMeans() scikitplot.clustering_factory(clf) fig, ax = plt.subplots(1, 1) out_ax = clf.plot_silhouette(self.X) assert ax is not out_ax out_ax = clf.plot_silhouette(self.X, ax=ax) assert ax is out_ax
def test_n_clusters_in_clf(self): np.random.seed(0) class DummyClusterer: def __init__(self): pass def fit(self): pass def fit_predict(self): pass clf = DummyClusterer() scikitplot.clustering_factory(clf) self.assertRaises(TypeError, clf.plot_elbow_curve, self.X)
def test_method_insertion(self): clf = self.Clusterer() scikitplot.clustering_factory(clf) assert hasattr(clf, 'plot_silhouette') assert hasattr(clf, 'plot_elbow_curve') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') scikitplot.clustering_factory(clf) assert len(w) >= 2 for warning in w[1:]: assert issubclass(warning.category, UserWarning) assert ' method already in clf. ' \ 'Overriding anyway. This may ' \ 'result in unintended behavior.' in str(warning.message)
def test_method_insertion(self): clf = self.Clusterer() scikitplot.clustering_factory(clf) assert hasattr(clf, 'plot_silhouette') assert hasattr(clf, 'plot_elbow_curve') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') scikitplot.clustering_factory(clf) assert len(w) == 2 for warning in w: assert issubclass(warning.category, UserWarning) assert ' method already in clf. ' \ 'Overriding anyway. This may ' \ 'result in unintended behavior.' in str(warning.message)
def test_cmap(self): np.random.seed(0) clf = KMeans() scikitplot.clustering_factory(clf) ax = clf.plot_silhouette(self.X, cmap='Spectral') ax = clf.plot_silhouette(self.X, cmap=plt.cm.Spectral)
def test_cluster_ranges(self): np.random.seed(0) clf = KMeans() scikitplot.clustering_factory(clf) ax = clf.plot_elbow_curve(self.X, cluster_ranges=range(1, 10)) ax = clf.plot_elbow_curve(self.X)
"""An example showing the plot_silhouette method used by a scikit-learn clusterer""" from __future__ import absolute_import import matplotlib.pyplot as plt from scikitplot import clustering_factory from sklearn.cluster import KMeans from sklearn.datasets import load_iris as load_data X, y = load_data(return_X_y=True) kmeans = clustering_factory(KMeans(n_clusters=4, random_state=1)) kmeans.plot_silhouette(X) plt.show()
"""An example showing the plot_silhouette method used by a scikit-learn clusterer""" from __future__ import absolute_import import matplotlib.pyplot as plt from scikitplot import clustering_factory from sklearn.cluster import KMeans from sklearn.datasets import load_iris as load_data X, y = load_data(return_X_y=True) kmeans = clustering_factory(KMeans(random_state=1)) kmeans.plot_elbow_curve(X, cluster_ranges=range(1, 11)) plt.show()