本文整理汇总了Python中sklearn.cluster.AffinityPropagation.fit方法的典型用法代码示例。如果您正苦于以下问题:Python AffinityPropagation.fit方法的具体用法?Python AffinityPropagation.fit怎么用?Python AffinityPropagation.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.AffinityPropagation
的用法示例。
在下文中一共展示了AffinityPropagation.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def main():
'''
>>> main() # stuff happens
'''
args = parse_args()
setup_logging(args.log, verbose=args.verbose)
chunks = sequence_chunk_generator(args.fasta_file,
chunk_size=args.chunk_size)
hasher = HashingVectorizer(analyzer='char',
n_features = 2 ** 18,
ngram_range=(args.ngram_min, args.ngram_max),
)
estimator = AffinityPropagation()
for chunk in chunks:
logging.info('hashing chunk')
chunk_vector = hasher.transform([ str(i.seq) for i in chunk ])
logging.info('clustering')
estimator.fit(chunk_vector)
logging.info('got %s clusters' % len(set(estimator.labels_)))
示例2: clusterSimilarityWithSklearnAPC
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def clusterSimilarityWithSklearnAPC(data_file,damping=0.9,max_iter=200,convergence_iter=15,preference='min'):
"""
Compare Sparse Affinity Propagation (SAP) result with SKlearn Affinity Propagation (AP) Clustering result.
Please note that convergence condition for Sklearn AP is "no change in the number of estimated clusters",
for SAP the condition is "no change in the cluster assignment".
So SAP may take more iterations and the there will be slightly difference in final cluster assignment (exemplars for each sample).
"""
# loading data
simi_mat=loadMatrix(data_file)
simi_mat_dense=simi_mat.todense()
# get preference
if preference=='min':
preference=np.min(simi_mat_dense)
elif preference=='median':
preference=np.median(simi_mat_dense)
print('{0}, start SKlearn Affinity Propagation'.format(datetime.now()))
af=AffinityPropagation(damping=damping, preference=preference, affinity='precomputed',verbose=True)
af.fit(simi_mat_dense)
cluster_centers_indices,labels = af.cluster_centers_indices_,af.labels_
sk_exemplars=np.asarray([cluster_centers_indices[i] for i in labels])
print('{0}, start Fast Sparse Affinity Propagation Cluster'.format(datetime.now()))
sap=SAP(preference=preference,convergence_iter=convergence_iter,max_iter=max_iter,damping=damping,verboseIter=100)
sap_exemplars=sap.fit_predict(simi_mat_dense)
# Caculate similarity between sk_exemplars and sap_exemplars
exemplars_similarity=sparseAP_cy.arrSamePercent(np.array(sk_exemplars), np.array(sap_exemplars))
return exemplars_similarity
示例3: clusterAffinityPropagation
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def clusterAffinityPropagation(self):
"""
Cluster the embeddings with affinity propagation
:return:
"""
affin = AffinityPropagation()
affin.fit(self.emb1.m)
aflabels1 = affin.labels_
afclusters1 = dict()
word2cluster1 = dict()
for i,l in enumerate(aflabels1):
points = afclusters1.setdefault(l,list())
points.append(self.emb1.rd[i])
for l,c in afclusters1.items():
for w in c:
word2cluster1[w] = l
self.cluster1 = afclusters1
self.word2cluster1 = word2cluster1
affin.fit(self.emb2.m)
aflabels2 = affin.labels_
afclusters2 = dict()
word2cluster2 = dict()
for i,l in enumerate(aflabels2):
points = afclusters2.setdefault(l,list())
points.append(self.emb2.rd[i])
for l,c in afclusters2.items():
for w in c:
word2cluster2[w] = l
self.cluster2 = afclusters2
self.word2cluster2 = word2cluster2
示例4: cluster
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def cluster(scope):
# Setup data
df = pd.read_sql('playtype_data', db_engine)
# Manipulate data into scope
if scope == 'Team':
df = df.drop('Player', 1).groupby('Team', as_index=False).mean()
elif scope == 'Player':
df = df.drop('Team', 1)
else:
raise Exception('This is never supposed to happen')
# Normalize the data
df[FEATURES] = (df[FEATURES] - df[FEATURES].mean()) / (df[FEATURES].max() - df[FEATURES].min())
# Run clustering
clstr = AffinityPropagation()
clstr.fit(df[FEATURES])
# Clump results
df['cluster'] = clstr.labels_
df = df.sort('cluster')
# Convert results to JSON for frontend
return clusters_to_json(df, scope)
示例5: affinity_propagation
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def affinity_propagation(crime_rows, column_names):
"""
damping : float, optional, default: 0.5
Damping factor between 0.5 and 1.
convergence_iter : int, optional, default: 15
Number of iterations with no change in the number of estimated
clusters that stops the convergence.
max_iter : int, optional, default: 200
Maximum number of iterations.
preference : array-like, shape (n_samples,) or float, optional
Preferences for each point - points with larger values of preferences
are more likely to be chosen as exemplars.
The number of exemplars, ie of clusters, is influenced by the input
preferences value. If the preferences are not passed as arguments,
they will be set to the median of the input similarities.
affinity : string, optional, default=``euclidean``
Which affinity to use. At the moment precomputed and euclidean are
supported. euclidean uses the negative squared euclidean distance
between points.
"""
crime_xy = [crime[0:2] for crime in crime_rows]
crime_info = [crime[2:] for crime in crime_rows]
print("Running Affinity Propagation")
# TODO: Parameterize this
affinity_prop = AffinityPropagation()
#affinity_propagation_labels = affinity_prop.fit_predict(crime_xy)
affinity_prop.fit(random_sampling(crime_xy, num_samples=5000))
affinity_propagation_labels = affinity_prop.predict(crime_xy)
print("formatting....")
return _format_clustering(affinity_propagation_labels, crime_xy, crime_info,
column_names)
示例6: cluster
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def cluster(self, feat_mtx, df_lm_allusers):
# clustering artists based on AffinityPropogation
start = time.time()
af = AffinityPropagation()
af.fit(feat_mtx)
self.labels = af.labels_
self.af = af
# adding cluster labels to least misery dataframe and sorting by rank and cluster
#df_least_misery_clustered = self.df_least_misery.copy() --> changing to df_lm_allusers
print 'number of labels: ', len(self.labels)
print 'labels', self.labels
# print 'least misery clustered length', len(df_least_misery_clustered)
df_least_misery_clustered = df_lm_allusers.copy()
print 'len df least misery: ', len(df_least_misery_clustered)
df_least_misery_clustered['cluster'] = self.labels
df_least_misery_clustered[['cluster', self.score_col]] = df_least_misery_clustered[['cluster', self.score_col]].astype(float)
''' will do different sorting if not using rank '''
# now set to false as looking for highest score
df_least_misery_clustered = df_least_misery_clustered.sort(['cluster', self.score_col], ascending = False)
self.df_least_misery_clustered = df_least_misery_clustered
end = time.time()
print 'clustering completed in: ', end - start
return df_least_misery_clustered
示例7: cluster
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def cluster(mat, doc_indices):
X = mat[:, doc_indices].T
# Other clustering algorithms can easily be swapped in:
# http://scikit-learn.org/stable/modules/classes.html#module-sklearn.cluster
clust = AffinityPropagation()
clust.fit(X)
return zip(doc_indices, clust.labels_)
示例8: classify_core
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def classify_core(self, N_CLUSTERS, clusterType, data_for_trial_type, begin_time, end_time):
BEGIN_TIME_FRAME = begin_time*self.griddy.TIME_GRID_SPACING
END_TIME_FRAME = end_time*self.griddy.TIME_GRID_SPACING
data = data_for_trial_type[:,BEGIN_TIME_FRAME:END_TIME_FRAME,self.griddy.VEL_X]
labels = None
if clusterType == 'kmeans':
kmeans = KMeans(n_clusters=N_CLUSTERS)
kmeans.fit(data)
labels = kmeans.labels_
elif clusterType == 'affinity_propagation':
ap = AffinityPropagation(damping=0.75)
ap.fit(data)
labels = ap.labels_
N_CLUSTERS = np.max(self.labels)+1
elif clusterType == 'DBSCAN':
dbscan = DBSCAN()
dbscan.fit(data)
labels = dbscan.labels_
N_CLUSTERS = np.max(labels)+1
print 'N_CLUSTERS=' + str(N_CLUSTERS)
elif clusterType == 'AgglomerativeClustering':
ac = AgglomerativeClustering(n_clusters=N_CLUSTERS)
ac.fit(data)
labels = ac.labels_
else:
print 'ERROR: clusterType: ' + clusterType + ' is not recognized'
return (labels, N_CLUSTERS)
开发者ID:SashaRayshubskiy,项目名称:osmotropotaxis_analysis_python,代码行数:33,代码来源:fly_trajectory_classifier.py
示例9: clustering
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def clustering(self):
# Calculate similarity matrix
X = self.create_tfidf_vector()
X = X.toarray()
pca = PCA(n_components=300, copy=False)
X = pca.fit(X).transform(X)
S = cosine_similarity(X, X)
# Run affinity propogation
af = AffinityPropagation()
af.fit(S)
# Formulate result
tmp_clusters = defaultdict(list)
goal_clusters = defaultdict(list)
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_
count = 0
for label in labels:
tmp_clusters[\
self.goal_list[cluster_centers_indices[label]]].append(\
self.goal_list[count])
count += 1
# 2nd-layer clutering of each cluster
for goal, item_list in tmp_clusters.items():
subclusters = self.subcluster_by_editdistance(goal, item_list)
for subgoal, items in subclusters.items():
goal_clusters[subgoal] = items
return goal_clusters
示例10: make_cluster_map
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def make_cluster_map(damping=0.992):
test_labels, prediction = pickle.load(open(f_path_pred, 'rb'))
prob_conf = np.zeros((121, 121))
for l in range(121):
inds = np.squeeze(np.array(np.where(test_labels == l)))
class_conf = prediction[inds, :].mean(axis=0)
prob_conf[l, :] = class_conf
F = prob_conf
D = (1-F)
np.fill_diagonal(D, 0)
D_p = 0.5*(D+D.T)
clst = AP(damping=damping, # damping determines # of clusters
max_iter=500,
convergence_iter=15,
affinity='euclidean',
verbose=False)
clst.fit(D_p)
print 'Number of cluster:', len(clst.cluster_centers_)
membership = np.c_[range(121), clst.labels_]
fine_to_coarse = dict(membership)
coarse_to_fine = {l: [] for l in clst.labels_}
for k, v in fine_to_coarse.items():
coarse_to_fine[v].append(k)
pickle.dump(coarse_to_fine, open(os.path.join(curdir, 'coarse_to_fine.p'), 'wb'))
pickle.dump(fine_to_coarse, open(os.path.join(curdir, 'fine_to_coarse.p'), 'wb'))
示例11: affinity_propagation
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def affinity_propagation(feature_matrix):
sim = feature_matrix * feature_matrix.T
sim = sim.todense()
ap = AffinityPropagation()
ap.fit(sim)
clusters = ap.labels_
return ap, clusters
示例12: cluster_prop
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def cluster_prop(self, filtered_data):
prop_dict={}
for review in filtered_data:
for dicti in review['line']:
if not prop_dict.has_key(dicti["prop"][0]):
prop_dict[dicti["prop"][0]]={"freq":0,"data":[],"idx":[]}
prop_dict[dicti["prop"][0]]['idx'].append(review['index'])
prop_dict[dicti["prop"][0]]["freq"] += 1
prop_dict[dicti["prop"][0]]["data"].append(dicti)
d_list=[]
word_list=[]
for word in prop_dict:
try:
d_list.append(self.wmodel[word])
word_list.append(word)
except:
pass
Aprop = AffinityPropagation(damping=0.6, convergence_iter=100, max_iter=10000)
Aprop.fit(d_list)
cluster_dict = {}
for idx, each in enumerate(Aprop.labels_):
vec = d_list[idx]
if not cluster_dict.has_key(each):
cluster_dict[each] = {"word":[],"freq":0,"seed":"","sim":0.0}
cluster_dict[each]["word"].append(word_list[idx])
total_freq=0
for each in cluster_dict.keys():
target_group_id = each
group_id = each
last_group_id = target_group_id
cluster_freq=0
max_seed=""
max_freq=0
for idx,data in enumerate(cluster_dict[each]["word"]):
cluster_freq+=prop_dict[data]["freq"]
if prop_dict[data]["freq"] > max_freq:
max_freq=prop_dict[data]["freq"]
max_seed=data
cluster_dict[each]["freq"]=cluster_freq
cluster_dict[each]["seed"]=max_seed
return (cluster_dict, prop_dict, Aprop)
示例13: cluster_concepts
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def cluster_concepts(context="location"):
"""
Cluster related concepts of a specific type to different categories
"""
db = Database()
concept_category = ConceptCategory()
cmd = "SELECT * FROM %s" % (context)
context_res = db.query_db(cmd)
concept_list = []
concept_matrix = []
for item in context_res:
concept_list = []
concept_matrix = []
if context == "action":
context_id, context_chinese, context_name = item[:3]
elif context == "location":
context_id, context_name, context_chinese = item
cmd = (
"SELECT b.name, b.id FROM %s_concept AS a, concept AS b \
WHERE a.%s_id = %s AND a.concept_id = b.id"
% (context, context, context_id)
)
concept_res = db.query_db(cmd)
if len(concept_res) == 0:
continue
for item in concept_res:
concept, concept_id = item
concept_vector = concept_category.concept_axes.row_named(concept)
concept_list.append((concept_id, concept))
concept_matrix.append(concept_vector)
# Run affinity propogation
S = cosine_similarity(concept_matrix, concept_matrix)
af = AffinityPropagation()
af.fit(S)
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_
count = 0
clusters = defaultdict(list)
for label in labels:
clusters[concept_list[cluster_centers_indices[label]][1]].append(concept_list[count])
count += 1
category_num = 0
for key, value in clusters.items():
category_num += 1
for concept in value:
cmd = (
"UPDATE %s_concept SET category = %d WHERE \
%s_id = %s AND concept_id = %s"
% (context, category_num, context, context_id, concept[0])
)
db.query_db(cmd)
print concept[1].encode("utf-8") + " ",
print ""
print "----------" + context_chinese.encode("utf-8") + "----------"
示例14: clustering_affinity_propagation
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def clustering_affinity_propagation(data_res):
"""
Executes sklearn's affinity propagation function with the given data frame
"""
af = AffinityPropagation()
af.fit(data_res)
predictions = af.predict(data_res)
cluster_centers = af.cluster_centers_
return predictions, cluster_centers, af
示例15: affinityprop
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import fit [as 别名]
def affinityprop(lngs, lats, city, cluster_diameter):
city_area = city["area"]
city_lng = city["lng"]
city_lat = city["lat"]
lngs = np.array(lngs)#*(math.cos(city["lat"])**2)
affinity = AffinityPropagation(damping=0.75, max_iter=200, convergence_iter=15, copy=True, preference=None, affinity='euclidean', verbose=False)
affinity.fit(np.array([lngs, lats]).transpose())
cluster_labels = np.array(affinity.labels_)
return labels_to_index(cluster_labels)