本文整理汇总了Python中sklearn.cluster.Ward.fit_predict方法的典型用法代码示例。如果您正苦于以下问题:Python Ward.fit_predict方法的具体用法?Python Ward.fit_predict怎么用?Python Ward.fit_predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.Ward
的用法示例。
在下文中一共展示了Ward.fit_predict方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hieclu
# 需要导入模块: from sklearn.cluster import Ward [as 别名]
# 或者: from sklearn.cluster.Ward import fit_predict [as 别名]
def hieclu(data_matrix, k):
#use Hierarchical clustering
print 'using hierarchical clustering......'
ac = Ward(n_clusters=k)
ac.fit(data_matrix)
result = ac.fit_predict(data_matrix)
return result
示例2: hierarchicalClustering
# 需要导入模块: from sklearn.cluster import Ward [as 别名]
# 或者: from sklearn.cluster.Ward import fit_predict [as 别名]
def hierarchicalClustering(x,k):
model = Ward(n_clusters=k)
labels = model.fit_predict(np.asarray(x))
# Centroids is a list of lists
centroids = []
for c in range(k):
base = []
for d in range(len(x[0])):
base.append(0)
centroids.append(base)
# Stores number of examples per cluster
ctrs = np.zeros(k)
# Sum up all vectors for each cluster
for c in range(len(x)):
centDex = labels[c]
for d in range(len(centroids[centDex])):
centroids[centDex][d] += x[c][d]
ctrs[centDex] += 1
# Average the vectors in each cluster to get the centroids
for c in range(len(centroids)):
for d in range(len(centroids[c])):
centroids[c][d] = centroids[c][d]/ctrs[c]
return (centroids,labels)
示例3: __hieclu
# 需要导入模块: from sklearn.cluster import Ward [as 别名]
# 或者: from sklearn.cluster.Ward import fit_predict [as 别名]
def __hieclu(self):
#use Hierarchical clustering
print 'using hierarchical clustering......'
ac = Ward(n_clusters = self.k)
ac.fit(self.data_matrix)
result = ac.fit_predict(self.data_matrix)
return result
示例4: constraint
# 需要导入模块: from sklearn.cluster import Ward [as 别名]
# 或者: from sklearn.cluster.Ward import fit_predict [as 别名]
def constraint(self, nodes, edges, lables):
if len(nodes) != len(lables):
print("#nodes(%d) != #clusters(%d)" % (len(nodes), len(lables)))
N = len(nodes)
circles = {}
guidance_matrix = sp.zeros([N, N])
# guidance_matrix = {}
for i in range(len(nodes)):
if lables[i] in circles:
circles[lables[i]].append(nodes[i])
else:
circles[lables[i]] = [nodes[i]]
for key in circles.iterkeys():
print(key, len(circles[key]))
c = 36
for ni in circles[c]:
i = nodes.index(ni)
for nj in circles[c]:
j = nodes.index(nj)
guidance_matrix[i, j] = 1.0
guidance_matrix = sparse.lil_matrix(guidance_matrix)
# pos = sum(x > 0 for x in guidance_matrix)
print(guidance_matrix)
ward = Ward(n_clusters=6, n_components=2, connectivity=guidance_matrix)
predicts = ward.fit_predict(self.A)
print(predicts)
示例5: agglomerate
# 需要导入模块: from sklearn.cluster import Ward [as 别名]
# 或者: from sklearn.cluster.Ward import fit_predict [as 别名]
def agglomerate(self, nodes, edges, clusters):
if len(nodes) != len(clusters):
print("#nodes(%d) != #clusters(%d)" % (len(nodes), len(clusters)))
neighbors = {}
for edge in edges:
if edge[0] in neighbors:
neighbors[edge[0]].append(edge[1])
else:
neighbors[edge[0]] = [edge[1]]
node_clusters = {} # node: its cluster id
communities = {} # cluster id: all neighbors for its members
for i in range(len(nodes)):
if clusters[i] in communities:
communities[clusters[i]].extend(neighbors[nodes[i]])
else:
communities[clusters[i]] = neighbors[nodes[i]]
node_clusters[nodes[i]] = clusters[i]
N = len(communities)
affinity_matrix = sp.zeros([N, N])
for comm in communities:
members = [node_clusters[node] for node in communities[comm]]
degree = dict(Counter(members))
for key in degree:
affinity_matrix[comm, key] = degree[key]
ward = Ward(n_clusters=6)
predicts = ward.fit_predict(affinity_matrix)
return [predicts[node_clusters[node]] for node in nodes]
示例6: cluster_ward
# 需要导入模块: from sklearn.cluster import Ward [as 别名]
# 或者: from sklearn.cluster.Ward import fit_predict [as 别名]
def cluster_ward(classif_data, vect_data):
ward = Ward(n_clusters=10)
np_arr_train = np.array(vect_data["train_vect"])
np_arr_label = np.array(classif_data["topics"])
np_arr_test = np.array(vect_data["test_vect"])
labels = ward.fit_predict(np_arr_train)
print "Ward"
sil_score = metrics.silhouette_score(np_arr_train, labels, metric='euclidean')
print sil_score
return labels
示例7: get_km_segments
# 需要导入模块: from sklearn.cluster import Ward [as 别名]
# 或者: from sklearn.cluster.Ward import fit_predict [as 别名]
def get_km_segments(x, image, sps, n_segments=25):
if len(x) == 2:
feats, edges = x
else:
feats, edges, _ = x
colors_ = get_colors(image, sps)
centers = get_centers(sps)
n_spixel = len(feats)
graph = sparse.coo_matrix((np.ones(edges.shape[0]), edges.T), shape=(n_spixel, n_spixel))
ward = Ward(n_clusters=n_segments, connectivity=graph + graph.T)
# km = KMeans(n_clusters=n_segments)
color_feats = np.hstack([colors_, centers * 0.5])
# return km.fit_predict(color_feats)
return ward.fit_predict(color_feats)
示例8: hierarchical
# 需要导入模块: from sklearn.cluster import Ward [as 别名]
# 或者: from sklearn.cluster.Ward import fit_predict [as 别名]
def hierarchical(self, n_clusters):
ward = Ward(n_clusters=n_clusters)
return ward.fit_predict(sp.array(self.A))