本文整理汇总了Python中sklearn.cluster.AffinityPropagation.predict方法的典型用法代码示例。如果您正苦于以下问题:Python AffinityPropagation.predict方法的具体用法?Python AffinityPropagation.predict怎么用?Python AffinityPropagation.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.cluster.AffinityPropagation
的用法示例。
在下文中一共展示了AffinityPropagation.predict方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: loadKmeansData
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import predict [as 别名]
def loadKmeansData(dataArrayTest,dataArrayTrain,k,m='load'):
if m=='load':
centroidRead=open('centroid','r')
labelClusterRead=open('labelCluster','r')
labelPreRead=open('labelPre','r')
centroid=pickle.load(centroidRead)
labelCluster=pickle.load(labelClusterRead)
labelPre=pickle.load(labelPreRead)
else:
dataArrayTestNorm = preprocessing.normalize(dataArrayTest)
dataArrayTrainNorm = preprocessing.normalize(dataArrayTrain)
#clf=MiniBatchKMeans(init='k-means++', n_clusters=k, n_init=10)
clf=AffinityPropagation()
#clf=DBSCAN(min_samples=30)
pre=clf.fit(dataArrayTrainNorm)
centroid=pre.cluster_centers_
centroidWrite=open('centroid','w')
#pickle.dump(centroid,centroidWrite)
labelCluster=pre.labels_
labelClusterWrite=open('labelCluster','w')
#pickle.dump(labelCluster,labelClusterWrite)
labelPre=clf.predict(dataArrayTestNorm)
labelPreWrite=open('labelPre','w')
#pickle.dump(labelPre,labelPreWrite)
return centroid,labelCluster,labelPre
示例2: affinity_propagation
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import predict [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)
示例3: ap
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import predict [as 别名]
def ap():
data_index = [
"dp_tr",
"dp_t",
"dq_tr",
"dq_t",
"du_tr",
"du_t",
"di_tr",
"di_t",
"dp_s",
"dq_s",
"du_s",
"di_s",
"dp_dq",
"first_h",
"third_h",
"fifth_h",
]
feature = readFeature(db)
sample, n_com, pca_fit = do_pca()
sample = pd.DataFrame(sample)
sample["p_n"] = feature["p_n"].values
sample_up = sample[sample.p_n == 1].iloc[:, 0:n_com]
sample_down = sample[sample.p_n == 0].iloc[:, 0:n_com]
start = time.time()
print("start to do training")
p = -0.5
af_up = AffinityPropagation(damping=0.5, preference=p).fit(sample_up)
af_down = AffinityPropagation(damping=0.5, preference=p).fit(sample_down)
print("Event number of starting appliances:", af_up.predict(sample_up))
print("Event number of stoping appliances:", af_down.predict(sample_down))
# feature['labels'] = af.labels_
saveModel2mdb(
db,
[
af_up,
af_down,
pca_fit,
feature.loc[:, data_index].max(),
feature.loc[:, data_index].min(),
feature.loc[:, data_index].mean(),
],
)
print("done with training take:", time.time() - start, "seconds")
示例4: clustering_affinity_propagation
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import predict [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
示例5: print
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import predict [as 别名]
lowercase=True, stop_words="english")
print("Vectorizing...")
t0 = time()
samples = dataset.data[:args.n_samples]
counts = vectorizer.fit_transform(samples)
tfidf = text.TfidfTransformer(norm="l2", use_idf=True).fit_transform(counts)
print("done in %0.3fs." % (time() - t0))
# Fit the model
print("Fitting the model on with n_samples=%d and n_features=%d..."
% (args.n_samples, args.n_features))
t0 = time()
d = Decomposition()
nmf = d.fit(tfidf)
print("done in %0.3fs." % (time() - t0))
# Fit the model
print("Predicting labels...")
t0 = time()
labels = d.predict(tfidf)
print("done in %0.3fs." % (time() - t0))
for sample, label in izip(samples, labels):
print(sample, label)
示例6: AffinityPropagation
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import predict [as 别名]
mini_batch_valid_performance_metrics_for_plotting[item + 1] = mini_batch_valid_performance_metric_array[item]
mini_batch_test_performance_metrics_for_plotting[item + 1] = mini_batch_test_performance_metric_array[item]
Figures.save_valid_test_performance_measures_vs_hyper_parameters_figure(mini_batch_parameter_search_space_for_plotting,
mini_batch_valid_performance_metrics_for_plotting,
mini_batch_test_performance_metrics_for_plotting,
'Adjusted Mutual Information Score',
'MiniBatch K-Means Clustering n_init parameter',
'Mini_Batch_k-Means_Performance',
0,
0.5)
# Do AffinityPropagation, optimizing damping over a validation set
current_optimal_affinity_propagation_parameter = 0.5
initial_optimal_affinity_propagation_clusterer = AffinityPropagation(damping=current_optimal_affinity_propagation_parameter)
initial_optimal_affinity_propagation_clusterer.fit(train_data_set)
initial_affinity_propagation_valid_predictions = initial_optimal_affinity_propagation_clusterer.predict(valid_data_set)
initial_affinity_propagation_test_predictions = initial_optimal_affinity_propagation_clusterer.predict(test_data_set)
# Add one to the predictions to make them match up with range of labels, then apply Hungarian Fix
for element in range(number_of_valid_observations):
initial_affinity_propagation_valid_predictions[element] += 1
for element in range(number_of_test_observations):
initial_affinity_propagation_test_predictions[element] += 1
initial_affinity_propagation_valid_predictions = Clustering.Hungarian_Fix(initial_affinity_propagation_valid_predictions,
valid_labels).astype('int')
initial_affinity_propagation_test_predictions = Clustering.Hungarian_Fix(initial_affinity_propagation_test_predictions,
test_labels).astype('int')
# Set a starting point for optimality of the initial performance metric, to be possibly adjusted later
affinity_propagation_parameter_integer_search_space_start = current_optimal_affinity_propagation_parameter + 0.05
affinity_propagation_parameter_integer_search_space_stop = current_optimal_affinity_propagation_parameter + 0.45
示例7: print
# 需要导入模块: from sklearn.cluster import AffinityPropagation [as 别名]
# 或者: from sklearn.cluster.AffinityPropagation import predict [as 别名]
print("Calculate AP codebook for quantization ...")
af = AffinityPropagation().fit(resnet50_train_overall)
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_
AP_codebook_size = len(cluster_centers_indices)
bovw_matrix_train=np.zeros((raw_matrix_train.shape[0],raw_matrix_train.shape[1]))
for i in xrange(bovw_matrix_train.shape[0]):
for j in xrange(bovw_matrix_train.shape[1]):
current_frame_rawfeature=raw_matrix_train[i,j]
current_frame_w=kmeans_codebook.predict(current_frame_rawfeature.reshape(1,-1))[0]
current_frame_w=af.predict(current_frame_rawfeature.reshape(1,-1))[0]
bovw_matrix_train[i,j]=int(current_frame_w)
bovw_matrix_test=np.zeros((raw_matrix_test.shape[0],raw_matrix_test.shape[1]))
for i in xrange(bovw_matrix_test.shape[0]):
for j in xrange(bovw_matrix_test.shape[1]):
current_frame_rawfeature=raw_matrix_test[i,j]
current_frame_w=kmeans_codebook.predict(current_frame_rawfeature.reshape(1,-1))[0]
current_frame_w=af.predict(current_frame_rawfeature.reshape(1,-1))[0]
bovw_matrix_test[i,j]=int(current_frame_w)