本文整理汇总了Python中sklearn.cluster.optics_.OPTICS类的典型用法代码示例。如果您正苦于以下问题:Python OPTICS类的具体用法?Python OPTICS怎么用?Python OPTICS使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OPTICS类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:27,代码来源:test_optics.py
示例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)
开发者ID:,项目名称:,代码行数:26,代码来源:
示例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)))
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:30,代码来源:test_optics.py
示例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)
开发者ID:allefpablo,项目名称:scikit-learn,代码行数:9,代码来源:test_optics.py
示例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_)
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:11,代码来源:test_optics.py
示例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)
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:11,代码来源:test_optics.py
示例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)
开发者ID:as133,项目名称:scikit-learn,代码行数:12,代码来源:test_optics.py
示例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
开发者ID:,项目名称:,代码行数:13,代码来源:
示例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
开发者ID:,项目名称:,代码行数:13,代码来源:
示例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
开发者ID:,项目名称:,代码行数:13,代码来源:
示例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)
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:14,代码来源:test_optics.py
示例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)
开发者ID:,项目名称:,代码行数:14,代码来源:
示例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)
开发者ID:as133,项目名称:scikit-learn,代码行数:23,代码来源:test_optics.py
示例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)
开发者ID:,项目名称:,代码行数:21,代码来源:
示例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)
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:4,代码来源:test_optics.py
示例16: test_min_cluster_size_invalid
def test_min_cluster_size_invalid(min_cluster_size):
clust = OPTICS(min_cluster_size=min_cluster_size)
with pytest.raises(ValueError, match="must be a positive integer or a "):
clust.fit(X)
开发者ID:MartinThoma,项目名称:scikit-learn,代码行数:4,代码来源:test_optics.py
示例17: test_reach_dists
def test_reach_dists():
# Tests against known extraction array
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)]
# eps not used for 'auto' extract
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=10, metric='minkowski')
# Run the fit
clust.fit(X)
# Expected values
# Skip to line 370
v = [np.inf, 0.606005, 0.472013, 0.162951, 0.161000, 0.385547, 0.179715,
0.213507, 0.348468, 0.308146, 0.560519, 0.266072, 0.764384, 0.253164,
0.435716, 0.153696, 0.363924, 0.194267, 0.392313, 0.230589, 0.260023,
0.535348, 0.168173, 0.296736, 0.310583, 0.277204, 0.250654, 0.153696,
0.215533, 0.175710, 0.168173, 0.283134, 0.256372, 0.313931, 0.234164,
0.179715, 0.352957, 0.277052, 0.180986, 0.203819, 0.296022, 0.356691,
0.515438, 0.219208, 0.265821, 0.346630, 0.275305, 0.229332, 0.433715,
0.153696, 0.584960, 0.265821, 0.471049, 0.259154, 0.461707, 0.400021,
0.422748, 0.300699, 0.162951, 0.290504, 0.315199, 0.327130, 0.168864,
0.462826, 0.188862, 0.259784, 0.216788, 0.259784, 0.195673, 0.315199,
0.313931, 0.189128, 0.461707, 0.265821, 0.233594, 0.433715, 0.222260,
0.251734, 0.352957, 0.218134, 0.453792, 0.179715, 0.296736, 0.260023,
0.311162, 0.214549, 0.266072, 0.318744, 0.180986, 0.194267, 0.262882,
0.420186, 0.352957, 0.288388, 0.360962, 0.328054, 0.293849, 0.198271,
0.248772, 0.461707, 0.216788, 0.396450, 0.352957, 0.289448, 0.241311,
0.213742, 0.220516, 0.218134, 0.153696, 0.516090, 0.218134, 0.221507,
0.328647, 0.255933, 0.195766, 0.233594, 0.205270, 0.296736, 0.726008,
0.251991, 0.168173, 0.214027, 0.262882, 0.342089, 0.260023, 0.266072,
0.253164, 0.230345, 0.262882, 0.296022, 0.227047, 0.205974, 0.328647,
0.184315, 0.196304, 0.831185, 0.514116, 0.168173, 0.189784, 0.664306,
0.327130, 0.379139, 0.208932, 0.266140, 0.362751, 0.168173, 0.764384,
0.327130, 0.187107, 0.194267, 0.414196, 0.251734, 0.220516, 0.363924,
0.166886, 0.327130, 0.233594, 0.203819, 0.230589, 0.203819, 0.222972,
0.311526, 0.218134, 0.422748, 0.314870, 0.315199, 0.315199, 0.594179,
0.328647, 0.415638, 0.244046, 0.250654, 0.214027, 0.203819, 0.213507,
0.260023, 0.311442, 0.168173, 0.389432, 0.229343, 0.162951, 0.311162,
0.153696, 0.214027, 0.250654, 0.315199, 0.172484, 0.153696, 0.352957,
0.314870, 0.328647, 0.546505, 0.378118, 0.260023, 0.387830, 0.199714,
0.262882, 0.250654, 0.345254, 0.396450, 0.250654, 0.179715, 0.328647,
0.179715, 0.263104, 0.265821, 0.231714, 0.514116, 0.213507, 0.474255,
0.212568, 0.376760, 0.196304, 0.844945, 0.194267, 0.264914, 0.210320,
0.316374, 0.184315, 0.179715, 0.250654, 0.153696, 0.162951, 0.315199,
0.179965, 0.297876, 0.213507, 0.475420, 0.439372, 0.241311, 0.260927,
0.194267, 0.422748, 0.222260, 0.411940, 0.414733, 0.260923, 0.396450,
0.380672, 0.333277, 0.290504, 0.196014, 0.844945, 0.506989, 0.153696,
0.218134, 0.392313, 0.698970, 0.168173, 0.227047, 0.028856, 0.033243,
0.028506, 0.057003, 0.038335, 0.051183, 0.063923, 0.022363, 0.030677,
0.036155, 0.017748, 0.062887, 0.036041, 0.051183, 0.078198, 0.068936,
0.032418, 0.040634, 0.022188, 0.022112, 0.036858, 0.040199, 0.025549,
0.083975, 0.032209, 0.025525, 0.032952, 0.034727, 0.068887, 0.040634,
0.048985, 0.047450, 0.022422, 0.023767, 0.028092, 0.047450, 0.029202,
0.026105, 0.030542, 0.032250, 0.062887, 0.038335, 0.026753, 0.028092,
0.099391, 0.021430, 0.020496, 0.021430, 0.025043, 0.023868, 0.050069,
0.023868, 0.044140, 0.038032, 0.022112, 0.044140, 0.031528, 0.028092,
0.020065, 0.055926, 0.031508, 0.025549, 0.028062, 0.036155, 0.023694,
0.029423, 0.026105, 0.028497, 0.023868, 0.044808, 0.035783, 0.033090,
0.038779, 0.032146, 0.038421, 0.057328, 0.020065, 0.020065, 0.028858,
0.021337, 0.041226, 0.022507, 0.028506, 0.030257, 0.057912, 0.050876,
0.120109, 0.020065, 0.034727, 0.038596, 0.037008, 0.031609, 0.095640,
0.083728, 0.064906, 0.030677, 0.057003, 0.037008, 0.018705, 0.030677,
0.044140, 0.034727, 0.045226, 0.032146, 0.032418, 0.029332, 0.030104,
0.033243, 0.030104, 0.032209, 0.026405, 0.024092, 0.048441, 0.036379,
0.030745, 0.023454, 0.018705, 0.124248, 0.041114, 0.020700, 0.042633,
0.042455, 0.028497, 0.029202, 0.057859, 0.053157, 0.036155, 0.029534,
0.032209, 0.038032, 0.024617, 0.023071, 0.033090, 0.023694, 0.047277,
0.024617, 0.023868, 0.043916, 0.025549, 0.046198, 0.041086, 0.042003,
0.022507, 0.021430, 0.038779, 0.025043, 0.036379, 0.036326, 0.029421,
0.023454, 0.058683, 0.025549, 0.039904, 0.022507, 0.046198, 0.029332,
0.032209, 0.036155, 0.038421, 0.025043, 0.023694, 0.030104, 0.022363,
0.048544, 0.035180, 0.030677, 0.022112, 0.030677, 0.036678, 0.022507,
0.024092, 0.064231, 0.022507, 0.032209, 0.025043, 0.221152, 0.029840,
0.038779, 0.040634, 0.024617, 0.032418, 0.025525, 0.033298, 0.028092,
0.045754, 0.032209, 0.017748, 0.033090, 0.017748, 0.048931, 0.038689,
0.022112, 0.027129, 0.032952, 0.036858, 0.027704, 0.032146, 0.052191,
0.042633, 0.071638, 0.044140, 0.022507, 0.046647, 0.028270, 0.050525,
0.036772, 0.058995, 0.038335, 0.025185, 0.022507, 0.040293, 0.032418,
0.064308, 0.026023, 0.036155, 0.032418, 0.038032, 0.018705, 0.040293,
0.030104, 0.030845, 0.064906, 0.025525, 0.036155, 0.022507, 0.022363,
0.032418, 0.021430, 0.032209, 0.102770, 0.036960, 0.031062, 0.025043,
0.036155, 0.031609, 0.036379, 0.030845, 0.048985, 0.021848, 0.025549,
0.022507, 0.035783, 0.023698, 0.034422, 0.032418, 0.022507, 0.023868,
0.020065, 0.023694, 0.040634, 0.055633, 0.054549, 0.044662, 0.087660,
0.048066, 0.143571, 0.068669, 0.065049, 0.076927, 0.044359, 0.041577,
0.052364, 0.100317, 0.062146, 0.067578, 0.054549, 0.047239, 0.062809,
0.033917, 0.087660, 0.077113, 0.055633, 0.061854, 0.059756, 0.059537,
#.........这里部分代码省略.........
开发者ID:,项目名称:,代码行数:101,代码来源:
示例18: test_empty_extract
def test_empty_extract():
# Test extract where fit() has not yet been run.
clust = OPTICS(eps=0.3, min_samples=10)
assert clust.extract(0.01, clustering='auto') is None
开发者ID:,项目名称:,代码行数:5,代码来源:
示例19: test_wrong_cluster_method
def test_wrong_cluster_method():
clust = OPTICS(cluster_method='superfancy')
with pytest.raises(ValueError, match="cluster_method should be one of "):
clust.fit(X)
开发者ID:allefpablo,项目名称:scikit-learn,代码行数:4,代码来源:test_optics.py
示例20: OPTICS
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)]
# Plot scatterplot of points
plt.figure(figsize=(10, 10))
plt.subplot(221)
plt.plot(X[:, 0], X[:, 1], 'b.', ms=2)
plt.title("Raw Data")
# Compute OPTICS
clust = OPTICS(eps=30.3, min_samples=9, metric='minkowski')
# Run the fit
clust.fit(X)
# Plot result
core_samples_mask = np.zeros_like(clust.labels_, dtype=bool)
core_samples_mask[clust.core_sample_indices_] = True
# Black removed and is used for noise instead.
unique_labels = set(clust.labels_)
colors = plt.cm.Spectral(np.linspace(0, 1, len(unique_labels)))
plt.subplot(222)
开发者ID:,项目名称:,代码行数:30,代码来源:
注:本文中的sklearn.cluster.optics_.OPTICS类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论