本文整理汇总了Python中sklearn.cluster.AgglomerativeClustering.n_clusters方法的典型用法代码示例。如果您正苦于以下问题:Python AgglomerativeClustering.n_clusters方法的具体用法?Python AgglomerativeClustering.n_clusters怎么用?Python AgglomerativeClustering.n_clusters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.AgglomerativeClustering
的用法示例。
在下文中一共展示了AgglomerativeClustering.n_clusters方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: plot_agglomerative
# 需要导入模块: from sklearn.cluster import AgglomerativeClustering [as 别名]
# 或者: from sklearn.cluster.AgglomerativeClustering import n_clusters [as 别名]
def plot_agglomerative():
from sklearn.datasets import make_blobs
from sklearn.cluster import AgglomerativeClustering
from sklearn.neighbors import KernelDensity
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
m = 16
k = 3
X, y = make_blobs(n_samples= m, n_features=2, centers=k, cluster_std=1.3, random_state = 2255)
agg = AgglomerativeClustering(n_clusters=3)
eps = X.std() / 2.
x_min, x_max = X[:, 0].min() - eps, X[:, 0].max() + eps
y_min, y_max = X[:, 1].min() - eps, X[:, 1].max() + eps
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100))
gridpoints = np.c_[xx.ravel().reshape(-1, 1), yy.ravel().reshape(-1, 1)]
ax = plt.gca()
for i, x in enumerate(X):
ax.text(x[0] + .1, x[1], "%d" % i, horizontalalignment='left', verticalalignment='center')
ax.scatter(X[:, 0], X[:, 1], s=20, c='grey')
ax.set_xticks(())
ax.set_yticks(())
for i in range((m-1)):
agg.n_clusters = X.shape[0] - i
agg.fit(X)
bins = np.bincount(agg.labels_)
for cluster in range(agg.n_clusters):
if bins[cluster] > 1:
points = X[agg.labels_ == cluster]
other_points = X[agg.labels_ != cluster]
kde = KernelDensity(bandwidth= 0.9).fit(points)
scores = kde.score_samples(gridpoints)
score_inside = np.min(kde.score_samples(points))
score_outside = np.max(kde.score_samples(other_points))
levels = .80 * score_inside + .20 * score_outside
ax.contour(xx, yy, scores.reshape(100, 100), levels=[levels],
colors='k', linestyles='solid', linewidths=0.8)
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
开发者ID:ktzioumis,项目名称:dsc-3-35-07-hierarchical-agglomerative-clustering-lab-online-ds-pt-112618,代码行数:50,代码来源:plot_agg.py
示例2: plot_agglomerative
# 需要导入模块: from sklearn.cluster import AgglomerativeClustering [as 别名]
# 或者: from sklearn.cluster.AgglomerativeClustering import n_clusters [as 别名]
def plot_agglomerative():
X, y = make_blobs(random_state=0, n_samples=12)
agg = AgglomerativeClustering(n_clusters=3)
eps = X.std() / 2.0
x_min, x_max = X[:, 0].min() - eps, X[:, 0].max() + eps
y_min, y_max = X[:, 1].min() - eps, X[:, 1].max() + eps
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100))
gridpoints = np.c_[xx.ravel().reshape(-1, 1), yy.ravel().reshape(-1, 1)]
ax = plt.gca()
for i, x in enumerate(X):
ax.text(x[0] + 0.1, x[1], "%d" % i, horizontalalignment="left", verticalalignment="center")
ax.scatter(X[:, 0], X[:, 1], s=60, c="grey")
ax.set_xticks(())
ax.set_yticks(())
for i in range(11):
agg.n_clusters = X.shape[0] - i
agg.fit(X)
bins = np.bincount(agg.labels_)
for cluster in range(agg.n_clusters):
if bins[cluster] > 1:
points = X[agg.labels_ == cluster]
other_points = X[agg.labels_ != cluster]
kde = KernelDensity(bandwidth=0.5).fit(points)
scores = kde.score_samples(gridpoints)
score_inside = np.min(kde.score_samples(points))
score_outside = np.max(kde.score_samples(other_points))
levels = 0.8 * score_inside + 0.2 * score_outside
ax.contour(
xx, yy, scores.reshape(100, 100), levels=[levels], colors="k", linestyles="solid", linewidths=1
)
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
示例3: plot_agglomerative_algorithm
# 需要导入模块: from sklearn.cluster import AgglomerativeClustering [as 别名]
# 或者: from sklearn.cluster.AgglomerativeClustering import n_clusters [as 别名]
def plot_agglomerative_algorithm():
# generate synthetic two-dimensional data
X, y = make_blobs(random_state=0, n_samples=12)
agg = AgglomerativeClustering(n_clusters=X.shape[0], compute_full_tree=True).fit(X)
fig, axes = plt.subplots(X.shape[0] // 5, 5, subplot_kw={"xticks": (), "yticks": ()}, figsize=(20, 8))
eps = X.std() / 2
x_min, x_max = X[:, 0].min() - eps, X[:, 0].max() + eps
y_min, y_max = X[:, 1].min() - eps, X[:, 1].max() + eps
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100))
gridpoints = np.c_[xx.ravel().reshape(-1, 1), yy.ravel().reshape(-1, 1)]
for i, ax in enumerate(axes.ravel()):
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)
agg.n_clusters = X.shape[0] - i
agg.fit(X)
ax.set_title("Step %d" % i)
ax.scatter(X[:, 0], X[:, 1], s=60, c="grey")
bins = np.bincount(agg.labels_)
for cluster in range(agg.n_clusters):
if bins[cluster] > 1:
points = X[agg.labels_ == cluster]
other_points = X[agg.labels_ != cluster]
kde = KernelDensity(bandwidth=0.5).fit(points)
scores = kde.score_samples(gridpoints)
score_inside = np.min(kde.score_samples(points))
score_outside = np.max(kde.score_samples(other_points))
levels = 0.8 * score_inside + 0.2 * score_outside
ax.contour(
xx, yy, scores.reshape(100, 100), levels=[levels], colors="k", linestyles="solid", linewidths=2
)
axes[0, 0].set_title("Initialization")