當前位置: 首頁>>代碼示例>>Python>>正文


Python optics_.OPTICS類代碼示例

本文整理匯總了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
開發者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


注:本文中的sklearn.cluster.optics_.OPTICS類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。