本文整理汇总了Python中sklearn.cluster.AffinityPropagation.tolist方法的典型用法代码示例。如果您正苦于以下问题:Python AffinityPropagation.tolist方法的具体用法?Python AffinityPropagation.tolist怎么用?Python AffinityPropagation.tolist使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.AffinityPropagation
的用法示例。
在下文中一共展示了AffinityPropagation.tolist方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clusterise_data
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import tolist [as 别名]
def clusterise_data(data_obj):
""" Assigns a cluster label to each days present in the data received
using three different algorithms: MeanShift, Affinity Propagation,
or KMeans.
@param data_obj: List of dictionaries
"""
L = len(data_obj)
#Simply converts data_obj to a 2D list for computation
List2D = [[None for _ in range(4)] for _ in range(L-1)]
for i in range(L-1): #don't include current day
#wake_up and sleep_duration are the most important factors
List2D[i][0] = 5 * data_obj[i]["wake_up"]
List2D[i][1] = 1 * data_obj[i]["sleep"]
List2D[i][2] = 5 * data_obj[i]["sleep_duration"]
List2D[i][3] = 0.5 * data_obj[i]["activity"]
points = NumpyArray(List2D) #converts 2D list to numpyarray
if ALGO == "Affinity Propagation":
labels = AffinityPropagation().fit_predict(points)
elif ALGO == "KMeans":
labels= KMeans(init='k-means++', n_clusters=5, n_init=10) .fit_predict(points)
elif ALGO == "MeanShift":
bandwidth = estimate_bandwidth(points, quantile=0.2, n_samples=20)
labels = MeanShift(bandwidth=bandwidth, bin_seeding=True).fit_predict(points)
else:
raise Exception("Algorithm not defined: "+str(ALGO))
for i in range(L-1):
data_obj[i]["cluster"] = labels[i]
for unique_label in remove_duplicates(labels):
debug_print(ALGO+": Cluster "+str(unique_label)+" contains "+str(labels.tolist().count(unique_label))+" data points")
debug_print(ALGO+": Silhouette coefficient"+ str(metrics.silhouette_score(points, labels, metric='euclidean')*100)+"%")