本文整理汇总了Python中sklearn.cluster.optics_.OPTICS类的典型用法代码示例。如果您正苦于以下问题:Python OPTICS类的具体用法?Python OPTICS怎么用?Python OPTICS使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OPTICS类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dbscan_optics_parity
def test_dbscan_optics_parity(eps, min_samples):
# Test that OPTICS clustering labels are <= 5% difference of DBSCAN
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=750, centers=centers,
cluster_std=0.4, random_state=0)
# calculate optics with dbscan extract at 0.3 epsilon
op = OPTICS(min_samples=min_samples).fit(X)
core_optics, labels_optics = op.extract_dbscan(eps)
# calculate dbscan labels
db = DBSCAN(eps=eps, min_samples=min_samples).fit(X)
contingency = contingency_matrix(db.labels_, labels_optics)
agree = min(np.sum(np.max(contingency, axis=0)),
np.sum(np.max(contingency, axis=1)))
disagree = X.shape[0] - agree
# verify core_labels match
assert_array_equal(core_optics, db.core_sample_indices_)
non_core_count = len(labels_optics) - len(core_optics)
percent_mismatch = np.round((disagree - 1) / non_core_count, 2)
# verify label mismatch is <= 5% labels
assert percent_mismatch <= 0.05
示例2: test_auto_extract_hier
def test_auto_extract_hier():
# Generate sample data
np.random.seed(0)
n_points_per_cluster = 250
X = np.empty((0, 2))
X = np.r_[X, [-5, -2] + .8 * np.random.randn(n_points_per_cluster, 2)]
X = np.r_[X, [4, -1] + .1 * np.random.randn(n_points_per_cluster, 2)]
X = np.r_[X, [1, -2] + .2 * np.random.randn(n_points_per_cluster, 2)]
X = np.r_[X, [-2, 3] + .3 * np.random.randn(n_points_per_cluster, 2)]
X = np.r_[X, [3, -2] + 1.6 * np.random.randn(n_points_per_cluster, 2)]
X = np.r_[X, [5, 6] + 2 * np.random.randn(n_points_per_cluster, 2)]
# Compute OPTICS
clust = OPTICS(eps=30.3, min_samples=9)
# Run the fit
clust.fit(X)
# Extract the result
# eps not used for 'auto' extract
clust.extract(0.0, 'auto')
assert_equal(len(set(clust.labels_)), 6)
示例3: test_correct_number_of_clusters
def test_correct_number_of_clusters():
# in 'auto' mode
n_clusters = 3
X = generate_clustered_data(n_clusters=n_clusters)
# Parameters chosen specifically for this task.
# Compute OPTICS
clust = OPTICS(max_eps=5.0 * 6.0, min_samples=4)
clust.fit(X)
# number of clusters, ignoring noise if present
n_clusters_1 = len(set(clust.labels_)) - int(-1 in clust.labels_)
assert_equal(n_clusters_1, n_clusters)
# check attribute types and sizes
assert clust.core_sample_indices_.ndim == 1
assert clust.core_sample_indices_.size > 0
assert clust.core_sample_indices_.dtype.kind == 'i'
assert clust.labels_.shape == (len(X),)
assert clust.labels_.dtype.kind == 'i'
assert clust.reachability_.shape == (len(X),)
assert clust.reachability_.dtype.kind == 'f'
assert clust.core_distances_.shape == (len(X),)
assert clust.core_distances_.dtype.kind == 'f'
assert clust.ordering_.shape == (len(X),)
assert clust.ordering_.dtype.kind == 'i'
assert set(clust.ordering_) == set(range(len(X)))
示例4: test_bad_reachability
def test_bad_reachability():
msg = "All reachability values are inf. Set a larger max_eps."
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=750, centers=centers,
cluster_std=0.4, random_state=0)
with pytest.warns(UserWarning, match=msg):
clust = OPTICS(max_eps=5.0 * 0.003, min_samples=10, eps=0.015)
clust.fit(X)
示例5: test_min_cluster_size
def test_min_cluster_size(min_cluster_size):
redX = X[::2] # reduce for speed
clust = OPTICS(min_samples=9, min_cluster_size=min_cluster_size).fit(redX)
cluster_sizes = np.bincount(clust.labels_[clust.labels_ != -1])
if cluster_sizes.size:
assert min(cluster_sizes) >= min_cluster_size
# check behaviour is the same when min_cluster_size is a fraction
clust_frac = OPTICS(min_samples=9,
min_cluster_size=min_cluster_size / redX.shape[0])
clust_frac.fit(redX)
assert_array_equal(clust.labels_, clust_frac.labels_)
示例6: test_bad_extract
def test_bad_extract():
# Test an extraction of eps too close to original eps
msg = "Specify an epsilon smaller than 0.15. Got 0.3."
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=750, centers=centers,
cluster_std=0.4, random_state=0)
# Compute OPTICS
clust = OPTICS(max_eps=5.0 * 0.03, min_samples=10)
clust2 = clust.fit(X)
assert_raise_message(ValueError, msg, clust2.extract_dbscan, 0.3)
示例7: test_correct_number_of_clusters
def test_correct_number_of_clusters():
# in 'auto' mode
n_clusters = 3
X = generate_clustered_data(n_clusters=n_clusters)
# Parameters chosen specifically for this task.
# Compute OPTICS
clust = OPTICS(max_bound=5.0 * 6.0, min_samples=4, metric='euclidean')
clust.fit(X)
# number of clusters, ignoring noise if present
n_clusters_1 = len(set(clust.labels_)) - int(-1 in clust.labels_)
assert_equal(n_clusters_1, n_clusters)
示例8: test_bad_extract
def test_bad_extract():
# Test an extraction of eps too close to original eps
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=750, centers=centers,
cluster_std=0.4, random_state=0)
##########################################################################
# Compute OPTICS
clust = OPTICS(eps=0.003, min_samples=10)
clust2 = clust.fit(X)
assert clust2.extract(0.3) is None
示例9: test_optics2
def test_optics2():
# Tests the optics clustering method and all functions inside it
# 'dbscan' mode
# Compute OPTICS
X = [[1, 1]]
clust = OPTICS(eps=0.3, min_samples=10)
# Run the fit
clust2 = clust.fit(X)
assert clust2 is None
示例10: test_close_extract
def test_close_extract():
# Test extract where extraction eps is close to scaled epsPrime
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=750, centers=centers,
cluster_std=0.4, random_state=0)
# Compute OPTICS
clust = OPTICS(eps=0.2, min_samples=10)
clust3 = clust.fit(X)
clust3.extract(0.3, clustering='dbscan')
assert max(clust3.labels_) == 3
示例11: test_close_extract
def test_close_extract():
# Test extract where extraction eps is close to scaled epsPrime
centers = [[1, 1], [-1, -1], [1, -1]]
X, labels_true = make_blobs(n_samples=750, centers=centers,
cluster_std=0.4, random_state=0)
# Compute OPTICS
clust = OPTICS(max_eps=1.0, min_samples=10)
clust3 = clust.fit(X)
# check warning when centers are passed
assert_warns(RuntimeWarning, clust3.extract_dbscan, .3)
# Cluster ordering starts at 0; max cluster label = 2 is 3 clusters
assert_equal(max(clust3.extract_dbscan(.3)[1]), 2)
示例12: test_optics
def test_optics():
# Tests the optics clustering method and all functions inside it
# 'auto' mode
n_clusters = 3
X = generate_clustered_data(n_clusters=n_clusters)
print(np.shape(X))
# Parameters chosen specifically for this task.
# Compute OPTICS
clust = OPTICS(eps=6.0, min_samples=4, metric='euclidean')
clust.fit(X)
# number of clusters, ignoring noise if present
n_clusters_1 = len(set(clust.labels_)) - int(-1 in clust.labels_)
assert_equal(n_clusters_1, n_clusters)
示例13: test_auto_extract_hier
def test_auto_extract_hier():
# Tests auto extraction gets correct # of clusters with varying density
# Generate sample data
rng = np.random.RandomState(0)
n_points_per_cluster = 250
C1 = [-5, -2] + .8 * rng.randn(n_points_per_cluster, 2)
C2 = [4, -1] + .1 * rng.randn(n_points_per_cluster, 2)
C3 = [1, -2] + .2 * rng.randn(n_points_per_cluster, 2)
C4 = [-2, 3] + .3 * rng.randn(n_points_per_cluster, 2)
C5 = [3, -2] + 1.6 * rng.randn(n_points_per_cluster, 2)
C6 = [5, 6] + 2 * rng.randn(n_points_per_cluster, 2)
X = np.vstack((C1, C2, C3, C4, C5, C6))
# Compute OPTICS
clust = OPTICS(min_samples=9)
# Run the fit
clust.fit(X)
assert_equal(len(set(clust.labels_)), 6)
示例14: test_filter
def test_filter():
# Tests the filter function.
n_clusters = 3
X = generate_clustered_data(n_clusters=n_clusters)
# Parameters chosen specifically for this task.
clust = OPTICS(eps=6.0, min_samples=4, metric='euclidean')
# Run filter (before computing OPTICS)
bool_memb = clust.filter(X, 0.5)
idx_memb = clust.filter(X, 0.5, index_type='idx')
# Test for equivalence between 'idx' and 'bool' extraction
assert_equal(sum(bool_memb), len(idx_memb))
# Compute OPTICS
clust.fit(X)
clust.extract(0.5, clustering='dbscan')
# core points from filter and extract should be the same within 1 point,
# with extract occasionally underestimating due to start point of the
# OPTICS algorithm. Here we test for at least 95% similarity in
# classification of core/not core
agree = sum(clust._is_core == bool_memb)
assert_greater_equal(float(agree)/len(X), 0.95)
示例15: test_min_cluster_size_invalid2
def test_min_cluster_size_invalid2():
clust = OPTICS(min_cluster_size=len(X) + 1)
with pytest.raises(ValueError, match="must be no greater than the "):
clust.fit(X)