本文整理汇总了Python中sklearn.cluster.ward_tree函数的典型用法代码示例。如果您正苦于以下问题:Python ward_tree函数的具体用法?Python ward_tree怎么用?Python ward_tree使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ward_tree函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_scikit_vs_scipy
def test_scikit_vs_scipy():
"""Test scikit ward with full connectivity (i.e. unstructured) vs scipy
"""
from scipy.sparse import lil_matrix
n, p, k = 10, 5, 3
rnd = np.random.RandomState(0)
connectivity = lil_matrix(np.ones((n, n)))
for i in range(5):
X = 0.1 * rnd.normal(size=(n, p))
X -= 4 * np.arange(n)[:, np.newaxis]
X -= X.mean(axis=1)[:, np.newaxis]
out = hierarchy.ward(X)
children_ = out[:, :2].astype(np.int)
children, _, n_leaves, _ = ward_tree(X, connectivity)
cut = _hc_cut(k, children, n_leaves)
cut_ = _hc_cut(k, children_, n_leaves)
assess_same_labelling(cut, cut_)
# Test error management in _hc_cut
assert_raises(ValueError, _hc_cut, n_leaves + 1, children, n_leaves)
开发者ID:VirgileFritsch,项目名称:scikit-learn,代码行数:25,代码来源:test_hierarchical.py
示例2: _seg_by_hc_single_frame
def _seg_by_hc_single_frame(obs_len, connectivity, data, width=9, hier=False, **kwargs):
_children, _n_c, _n_leaves, parents, distances = \
sklhc.ward_tree(data, connectivity=connectivity, return_distance=True)
reconstructed_z = np.zeros((obs_len - 1, 4))
reconstructed_z[:, :2] = _children
reconstructed_z[:, 2] = distances
if 'criterion' in kwargs.keys():
criterion = kwargs['criterion']
else:
criterion = 'distance'
if hier:
t_list = range(2, 11)
label_dict = OrderedDict()
boundary_dict = OrderedDict()
criterion = 'maxclust'
for t in t_list:
boundaries, labels = _agg_segment(reconstructed_z, t, criterion, width, data)
label_dict[np.max(labels) + 1] = labels
boundary_dict[np.max(labels) + 1] = boundaries
return boundary_dict, label_dict
else:
t = 0.7 * np.max(reconstructed_z[:, 2])
return _agg_segment(reconstructed_z, t, criterion, width, data)
开发者ID:wangsix,项目名称:vmo,代码行数:27,代码来源:segmentation.py
示例3: test_unstructured_ward_tree
def test_unstructured_ward_tree():
"""
Check that we obtain the correct solution for unstructured ward tree.
"""
rnd = np.random.RandomState(0)
X = rnd.randn(50, 100)
children, n_nodes, n_leaves = ward_tree(X.T)
n_nodes = 2 * X.shape[1] - 1
assert_true(len(children) + n_leaves == n_nodes)
开发者ID:AlexLerman,项目名称:scikit-learn,代码行数:9,代码来源:test_hierarchical.py
示例4: test_ward_tree_children_order
def test_ward_tree_children_order():
# Check that children are ordered in the same way for both structured and
# unstructured versions of ward_tree.
# test on five random datasets
n, p = 10, 5
rng = np.random.RandomState(0)
connectivity = np.ones((n, n))
for i in range(5):
X = .1 * rng.normal(size=(n, p))
X -= 4. * np.arange(n)[:, np.newaxis]
X -= X.mean(axis=1)[:, np.newaxis]
out_unstructured = ward_tree(X)
out_structured = ward_tree(X, connectivity=connectivity)
assert_array_equal(out_unstructured[0], out_structured[0])
开发者ID:kevin-coder,项目名称:scikit-learn-fork,代码行数:18,代码来源:test_hierarchical.py
示例5: test_height_ward_tree
def test_height_ward_tree():
"""
Check that the height of ward tree is sorted.
"""
rnd = np.random.RandomState(0)
mask = np.ones([10, 10], dtype=np.bool)
X = rnd.randn(50, 100)
connectivity = grid_to_graph(*mask.shape)
children, n_nodes, n_leaves, parent = ward_tree(X.T, connectivity)
n_nodes = 2 * X.shape[1] - 1
assert_true(len(children) + n_leaves == n_nodes)
开发者ID:2011200799,项目名称:scikit-learn,代码行数:11,代码来源:test_hierarchical.py
示例6: test_structured_ward_tree
def test_structured_ward_tree():
"""
Check that we obtain the correct solution for structured ward tree.
"""
rnd = np.random.RandomState(0)
mask = np.ones([10, 10], dtype=np.bool)
X = rnd.randn(50, 100)
connectivity = grid_to_graph(*mask.shape)
children, n_components, n_leaves = ward_tree(X.T, connectivity)
n_nodes = 2 * X.shape[1] - 1
assert_true(len(children) + n_leaves == n_nodes)
开发者ID:AlexLerman,项目名称:scikit-learn,代码行数:11,代码来源:test_hierarchical.py
示例7: test_ward_tree
def test_ward_tree(self):
iris = datasets.load_iris()
df = pdml.ModelFrame(iris)
result = df.cluster.ward_tree()
expected = cluster.ward_tree(iris.data)
self.assertEqual(len(result), 4)
self.assert_numpy_array_almost_equal(result[0], expected[0])
self.assertEqual(result[1], expected[1])
self.assertEqual(result[2], expected[2])
self.assertEqual(result[3], expected[3])
connectivity = np.ones((len(df), len(df)))
result = df.cluster.ward_tree(connectivity)
expected = cluster.ward_tree(iris.data, connectivity)
self.assert_numpy_array_almost_equal(result[0], expected[0])
self.assertEqual(result[1], expected[1])
self.assertEqual(result[2], expected[2])
self.assert_numpy_array_almost_equal(result[3], expected[3])
开发者ID:Sandy4321,项目名称:pandas-ml,代码行数:21,代码来源:test_cluster.py
示例8: test_unstructured_ward_tree
def test_unstructured_ward_tree():
"""
Check that we obtain the correct solution for unstructured ward tree.
"""
rnd = np.random.RandomState(0)
X = rnd.randn(50, 100)
for this_X in (X, X[0]):
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always", UserWarning)
# With specified a number of clusters just for the sake of
# raising a warning and testing the warning code
children, n_nodes, n_leaves, parent = ward_tree(this_X.T, n_clusters=10)
assert_equal(len(warning_list), 1)
n_nodes = 2 * X.shape[1] - 1
assert_equal(len(children) + n_leaves, n_nodes)
开发者ID:aswart,项目名称:scikit-learn,代码行数:15,代码来源:test_hierarchical.py
示例9: test_structured_ward_tree
def test_structured_ward_tree():
"""
Check that we obtain the correct solution for structured ward tree.
"""
rnd = np.random.RandomState(0)
mask = np.ones([10, 10], dtype=np.bool)
# Avoiding a mask with only 'True' entries
mask[4:7, 4:7] = 0
X = rnd.randn(50, 100)
connectivity = grid_to_graph(*mask.shape)
children, n_components, n_leaves, parent = ward_tree(X.T, connectivity)
n_nodes = 2 * X.shape[1] - 1
assert_true(len(children) + n_leaves == n_nodes)
# Check that ward_tree raises a ValueError with a connectivity matrix
# of the wrong shape
assert_raises(ValueError, ward_tree, X.T, np.ones((4, 4)))
开发者ID:2011200799,项目名称:scikit-learn,代码行数:16,代码来源:test_hierarchical.py
示例10: test_ward_linkage_tree_return_distance
def test_ward_linkage_tree_return_distance():
"""Test return_distance option on linkage and ward trees"""
# test that return_distance when set true, gives same
# output on both structured and unstructured clustering.
n, p = 10, 5
rng = np.random.RandomState(0)
connectivity = np.ones((n, n))
for i in range(5):
X = .1 * rng.normal(size=(n, p))
X -= 4. * np.arange(n)[:, np.newaxis]
X -= X.mean(axis=1)[:, np.newaxis]
out_unstructured = ward_tree(X, return_distance=True)
out_structured = ward_tree(X, connectivity=connectivity,
return_distance=True)
# get children
children_unstructured = out_unstructured[0]
children_structured = out_structured[0]
# check if we got the same clusters
assert_array_equal(children_unstructured, children_structured)
# check if the distances are the same
dist_unstructured = out_unstructured[-1]
dist_structured = out_structured[-1]
assert_array_almost_equal(dist_unstructured, dist_structured)
for linkage in ['average', 'complete']:
structured_items = linkage_tree(
X, connectivity=connectivity, linkage=linkage,
return_distance=True)[-1]
unstructured_items = linkage_tree(
X, linkage=linkage, return_distance=True)[-1]
structured_dist = structured_items[-1]
unstructured_dist = unstructured_items[-1]
structured_children = structured_items[0]
unstructured_children = unstructured_items[0]
assert_array_almost_equal(structured_dist, unstructured_dist)
assert_array_almost_equal(
structured_children, unstructured_children)
# test on the following dataset where we know the truth
# taken from scipy/cluster/tests/hierarchy_test_data.py
X = np.array([[1.43054825, -7.5693489],
[6.95887839, 6.82293382],
[2.87137846, -9.68248579],
[7.87974764, -6.05485803],
[8.24018364, -6.09495602],
[7.39020262, 8.54004355]])
# truth
linkage_X_ward = np.array([[3., 4., 0.36265956, 2.],
[1., 5., 1.77045373, 2.],
[0., 2., 2.55760419, 2.],
[6., 8., 9.10208346, 4.],
[7., 9., 24.7784379, 6.]])
linkage_X_complete = np.array(
[[3., 4., 0.36265956, 2.],
[1., 5., 1.77045373, 2.],
[0., 2., 2.55760419, 2.],
[6., 8., 6.96742194, 4.],
[7., 9., 18.77445997, 6.]])
linkage_X_average = np.array(
[[3., 4., 0.36265956, 2.],
[1., 5., 1.77045373, 2.],
[0., 2., 2.55760419, 2.],
[6., 8., 6.55832839, 4.],
[7., 9., 15.44089605, 6.]])
n_samples, n_features = np.shape(X)
connectivity_X = np.ones((n_samples, n_samples))
out_X_unstructured = ward_tree(X, return_distance=True)
out_X_structured = ward_tree(X, connectivity=connectivity_X,
return_distance=True)
# check that the labels are the same
assert_array_equal(linkage_X_ward[:, :2], out_X_unstructured[0])
assert_array_equal(linkage_X_ward[:, :2], out_X_structured[0])
# check that the distances are correct
assert_array_almost_equal(linkage_X_ward[:, 2], out_X_unstructured[4])
assert_array_almost_equal(linkage_X_ward[:, 2], out_X_structured[4])
linkage_options = ['complete', 'average']
X_linkage_truth = [linkage_X_complete, linkage_X_average]
for (linkage, X_truth) in zip(linkage_options, X_linkage_truth):
out_X_unstructured = linkage_tree(
X, return_distance=True, linkage=linkage)
out_X_structured = linkage_tree(
X, connectivity=connectivity_X, linkage=linkage,
return_distance=True)
# check that the labels are the same
assert_array_equal(X_truth[:, :2], out_X_unstructured[0])
#.........这里部分代码省略.........
开发者ID:foresthz,项目名称:scikit-learn,代码行数:101,代码来源:test_hierarchical.py
示例11: test_ward_tree_distance
def test_ward_tree_distance():
"""
Check that children are ordered in the same way for both structured and
unstructured versions of ward_tree.
"""
# test on five random datasets
n, p = 10, 5
rng = np.random.RandomState(0)
connectivity = np.ones((n, n))
for i in range(5):
X = .1 * rng.normal(size=(n, p))
X -= 4. * np.arange(n)[:, np.newaxis]
X -= X.mean(axis=1)[:, np.newaxis]
out_unstructured = ward_tree(X, return_distance=True)
out_structured = ward_tree(X, connectivity=connectivity,
return_distance=True)
# get children
children_unstructured = out_unstructured[0]
children_structured = out_structured[0]
# check if we got the same clusters
assert_array_equal(children_unstructured, children_structured)
# check if the distances are the same
dist_unstructured = out_unstructured[-1]
dist_structured = out_structured[-1]
assert_array_almost_equal(dist_unstructured, dist_structured)
# test on the following dataset where we know the truth
# taken from scipy/cluster/tests/hierarchy_test_data.py
X = np.array([[1.43054825, -7.5693489],
[6.95887839, 6.82293382],
[2.87137846, -9.68248579],
[7.87974764, -6.05485803],
[8.24018364, -6.09495602],
[7.39020262, 8.54004355]])
# truth
linkage_X_ward = np.array([[3., 4., 0.36265956, 2.],
[1., 5., 1.77045373, 2.],
[0., 2., 2.55760419, 2.],
[6., 8., 9.10208346, 4.],
[7., 9., 24.7784379, 6.]])
n_samples, n_features = np.shape(X)
connectivity_X = np.ones((n_samples, n_samples))
out_X_unstructured = ward_tree(X, return_distance=True)
out_X_structured = ward_tree(X, connectivity=connectivity_X,
return_distance=True)
# check that the labels are the same
assert_array_equal(linkage_X_ward[:, :2], out_X_unstructured[0])
assert_array_equal(linkage_X_ward[:, :2], out_X_structured[0])
# check that the distances are correct
assert_array_almost_equal(linkage_X_ward[:, 2], out_X_unstructured[4])
assert_array_almost_equal(linkage_X_ward[:, 2], out_X_structured[4])
开发者ID:zofuthan,项目名称:scikit-learn,代码行数:61,代码来源:test_hierarchical.py
示例12: fit
def fit(self, X, y):
"""
Fits Supervised Clustering.
Parameters
----------
X : ndarray of shape = (n_samples, n_features)
Y : ndarray of shape = (n_samples)
Returns
-------
self
"""
# n_components computed here because the user can change connectivity
if self.connectivity is not None:
self.n_components = cs_graph_components(self.connectivity)[0]
else:
self.n_components = 1
children, n_components, n_leaves = ward_tree(X.T,
connectivity=self.connectivity, n_components=self.n_components)
children = children.tolist() # Faster with a list
avg_signals = average_signals(X, children, n_leaves)
# The first parcellations is the list of the tree roots
parcellation = tree_roots(children, n_components, n_leaves)
parcellations = [] # List of the best parcellations
self.scores_ = []
if self.verbose >= 2:
print "\n# First parcellation (=tree roots) : %s" % parcellations
## EXPLORATION LOOP
for i in range(1, self.n_iterations+1): # for verbose mode
if self.verbose:
print "# Iteration %d" % i
iteration_parcellations = split_parcellation(parcellation,
children, n_leaves)
if (len(iteration_parcellations) == 0):
# No parcellation can be splitted
print " UserWARNING : n_iterations is too big :"
print " Ending function at iteration %d." % i
break
# Selecting the best parcellation for current iteration
scores = Parallel(n_jobs=self.n_jobs)(delayed(cross_val_score)
(estimator=self.estimator, X=avg_signals[:, j], y=y,
cv=self.cv, n_jobs=1, verbose=0)
for j in iteration_parcellations)
scores = np.mean(scores, axis=1)
indice = np.argmax(scores)
parcellation = np.copy(iteration_parcellations[indice])
parcellations.append(np.copy(parcellation))
self.scores_.append(np.copy(scores[indice]))
## SELECTION LOOP
# We select the parcellation for wich the variation of score is
# the bigger, only if it score is > score_max / 2
# Furthermore we select only parcellations obtained after 20 iterations
indice_min = 20
self.score_min_ = 7 * (np.max(self.scores_) / 10)
max = 0
indice = 0
self.delta_scores_ = [0]
for i in range(indice_min):
self.delta_scores.append(0)
for i in range(indice_min, len(self.scores_)-1):
if self.scores_[i+1] >= self.score_min_:
current_delta = self.scores_[i+1] - self.scores_[i]
if current_delta > max:
max = current_delta
indice = i
self.delta_scores_.append(current_delta)
else:
self.delta_scores_.append(0)
parcellation = parcellations[indice]
# Computing the corresponding labels array
self.labels_ = parcellation_to_label(parcellation, children, n_leaves)
self.estimator.fit(avg_signals[:, parcellation], y)
if hasattr(self.estimator, 'coef_'):
if len(self.estimator.coef_.shape) == 1:
self.coef_ = self.estimator.coef_
else:
self.coef_ = self.estimator.coef_[-1]
return self
开发者ID:JeanKossaifi,项目名称:tutorial,代码行数:92,代码来源:supervised_clustering.py
注:本文中的sklearn.cluster.ward_tree函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论