本文整理匯總了Python中sklearn.cluster.MeanShift方法的典型用法代碼示例。如果您正苦於以下問題:Python cluster.MeanShift方法的具體用法?Python cluster.MeanShift怎麽用?Python cluster.MeanShift使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.cluster
的用法示例。
在下文中一共展示了cluster.MeanShift方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: findClusters_meanShift
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def findClusters_meanShift(data):
'''
Cluster data using Mean Shift method
'''
bandwidth = cl.estimate_bandwidth(data,
quantile=0.25, n_samples=500)
# create the classifier object
meanShift = cl.MeanShift(
bandwidth=bandwidth,
bin_seeding=True
)
# fit the data
return meanShift.fit(data)
# the file name of the dataset
示例2: get_typical_durations
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def get_typical_durations(raw_durations, bandwidth_percentile=0.05,
min_intersection=0.5, miss_covered=0.1):
"""Return typical durations in a dataset."""
dur = (raw_durations).reshape(raw_durations.shape[0], 1)
bandwidth = estimate_bandwidth(dur, quantile=bandwidth_percentile)
ms = MeanShift(bandwidth=bandwidth, bin_seeding=False)
ms.fit(dur.reshape((dur.shape[0]), 1))
tw = np.sort(np.array(
ms.cluster_centers_.reshape(ms.cluster_centers_.shape[0]), dtype=int))
# Warranty a min intersection in the output durations.
p = np.zeros((dur.shape[0], tw.shape[0]))
for idx in range(tw.shape[0]):
p[:, idx] = (dur/tw[idx]).reshape(p[:,idx].shape[0])
ll = (p>=min_intersection) & (p<=1.0/min_intersection)
if (ll.sum(axis=1)>0).sum() / float(raw_durations.shape[0]) < (1.0-miss_covered):
assert False, "Condition of minimum intersection not satisfied"
return tw
示例3: _cluster
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def _cluster(prediction, bandwidth):
"""
實現論文SectionⅡ的cluster部分
:param prediction:
:param bandwidth:
:return:
"""
ms = MeanShift(bandwidth, bin_seeding=True)
log.info('開始Mean shift聚類 ...')
tic = time.time()
try:
ms.fit(prediction)
except ValueError as err:
log.error(err)
return 0, [], []
log.info('Mean Shift耗時: {:.5f}s'.format(time.time() - tic))
labels = ms.labels_
cluster_centers = ms.cluster_centers_
num_clusters = cluster_centers.shape[0]
log.info('聚類簇個數為: {:d}'.format(num_clusters))
return num_clusters, labels, cluster_centers
示例4: test
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def test():
while True:
a = np.random.rand(1000, 2)
ta = torch.from_numpy(a.astype(np.float32)).cuda()
ms = MeanShiftTorch(0.05)
ctr, _ = ms.fit(ta)
a_idx = (a * 480).astype("uint8")
show_a = np.zeros((480, 480, 3), dtype="uint8")
show_a[a_idx[:, 0], a_idx[:, 1], :] = np.array([255, 255, 255])
ctr = (ctr.cpu().numpy() * 480).astype("uint8")
show_a = cv2.circle(show_a, (ctr[1], ctr[0]), 3, (0, 0, 255), -1)
ms_cpu = MeanShift(
bandwidth=0.05, n_jobs=8
)
ms_cpu.fit(a)
clus_ctrs = np.array(ms_cpu.cluster_centers_)
clus_labels = ms_cpu.labels_
ctr = (clus_ctrs[0] * 480).astype("uint8")
show_a = cv2.circle(show_a, (ctr[1], ctr[0]), 3, (255, 0, 0), -1)
imshow('show_a', show_a)
waitKey(0)
print(clus_ctrs[0])
示例5: cluster_embed
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def cluster_embed(embeddings, preds_bin, band_width):
c = embeddings.shape[1]
n, _, h, w = preds_bin.shape
preds_bin = preds_bin.view(n, h, w)
preds_inst = torch.zeros_like(preds_bin)
for idx, (embedding, bin_pred) in enumerate(zip(embeddings, preds_bin)):
# print(embedding.size(), bin_pred.size())
embedding_fg = torch.transpose(torch.masked_select(embedding, bin_pred.byte()).view(c, -1), 0, 1)
# print(embedding_fg.size())
# embedding_expand = embedding.view(embedding.shape[0],
# embedding.shape[1] * embedding.shape[2])
# embedding_expand =torch.transpose(embedding_expand, 1, 0)
# print(embedding_expand.shape)
clustering = MeanShift(bandwidth=band_width, bin_seeding=True, min_bin_freq=100).fit(embedding_fg.cpu().detach().numpy())
preds_inst[idx][bin_pred.byte()] = torch.from_numpy(clustering.labels_).cuda() + 1
# labels_color = get_color(clustering.labels_)
# preds_inst[idx][bin_pred.byte()] = torch.from_numpy(labels_color).cuda() + 1
# print(torch.unique(preds_inst[idx]))
return preds_inst
示例6: test_objectmapper
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def test_objectmapper(self):
df = pdml.ModelFrame([])
self.assertIs(df.cluster.AffinityPropagation, cluster.AffinityPropagation)
self.assertIs(df.cluster.AgglomerativeClustering, cluster.AgglomerativeClustering)
self.assertIs(df.cluster.Birch, cluster.Birch)
self.assertIs(df.cluster.DBSCAN, cluster.DBSCAN)
self.assertIs(df.cluster.FeatureAgglomeration, cluster.FeatureAgglomeration)
self.assertIs(df.cluster.KMeans, cluster.KMeans)
self.assertIs(df.cluster.MiniBatchKMeans, cluster.MiniBatchKMeans)
self.assertIs(df.cluster.MeanShift, cluster.MeanShift)
self.assertIs(df.cluster.SpectralClustering, cluster.SpectralClustering)
self.assertIs(df.cluster.bicluster.SpectralBiclustering,
cluster.bicluster.SpectralBiclustering)
self.assertIs(df.cluster.bicluster.SpectralCoclustering,
cluster.bicluster.SpectralCoclustering)
示例7: test_Classifications
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def test_Classifications(self):
iris = datasets.load_iris()
df = pdml.ModelFrame(iris)
models = ['AffinityPropagation', 'MeanShift']
for model in models:
mod1 = getattr(df.cluster, model)()
mod2 = getattr(cluster, model)()
df.fit(mod1)
mod2.fit(iris.data)
result = df.predict(mod1)
expected = mod2.predict(iris.data)
self.assertIsInstance(result, pdml.ModelSeries)
self.assert_numpy_array_almost_equal(result.values, expected)
示例8: cluster
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def cluster(prediction, bandwidth):
ms = MeanShift(bandwidth, bin_seeding=True)
#print ('Mean shift clustering, might take some time ...')
#tic = time.time()
ms.fit(prediction)
#print ('time for clustering', time.time() - tic)
labels = ms.labels_
cluster_centers = ms.cluster_centers_
num_clusters = cluster_centers.shape[0]
return num_clusters, labels, cluster_centers
示例9: transform_rays_model_cdf_mixture
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def transform_rays_model_cdf_mixture(list_rays, coef_components=1):
""" compute the mixture model and transform it into cumulative distribution
:param list(list(int)) list_rays: list ray features (distances)
:param int coef_components: multiplication for number of components
:return any, list(list(int)): mixture model, cumulative distribution
>>> np.random.seed(0)
>>> list_rays = [[9, 4, 9], [4, 9, 7], [9, 7, 11], [10, 8, 10],
... [9, 11, 8], [4, 8, 5], [8, 10, 6], [9, 7, 11]]
>>> mm, cdist = transform_rays_model_cdf_mixture(list_rays)
>>> # the rounding variate a bit according GMM estimated model
>>> np.round(np.array(cdist) * 4) / 4. # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
array([[ 1. , 1. , 1. , 1. , 1. , 1. , 0.75, 0.75, 0.5 , 0.25, 0. ],
[ 1. , 1. , 1. , 1. , 1. , 1. , 1. , 0.75, 0.5 , 0.25, 0. ],
[ 1. , 1. , 1. , 1. , 1. , 1. , ..., 0.75, 0.5 , 0.25, 0. ]])
"""
rays = np.array(list_rays)
ms = cluster.MeanShift()
ms.fit(rays)
logging.debug('MeanShift found: %r', np.bincount(ms.labels_))
nb_components = int(len(np.unique(ms.labels_)) * coef_components)
mm = mixture.BayesianGaussianMixture(n_components=nb_components)
# gmm.fit(np.array(list_rays))
mm.fit(rays, ms.labels_)
logging.debug('Mixture model found % components with weights: %r',
len(mm.weights_), mm.weights_)
# compute the fairest mean + sigma over all components and ray angles
max_dist = np.max([[m[i] + np.sqrt(c[i, i]) for i in range(len(m))]
for m, c in zip(mm.means_, mm.covariances_)])
# max_dist = np.max(rays)
# fixing, AttributeError: 'BayesianGaussianMixture' object has no attribute 'covariances'
covs = mm.covariances if hasattr(mm, 'covariances') else mm.covariances_
stds = np.sqrt(abs(covs))[:, np.eye(mm.means_.shape[1], dtype=bool)]
# stds = np.sum(mm.covariances_, axis=-1)
cdist = compute_cumulative_distrib(mm.means_, stds, mm.weights_, max_dist)
return mm, cdist.tolist()
示例10: mean_shift
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def mean_shift(matrix):
mean_shift = skcluster.MeanShift()
mean_shift.fit(matrix)
labels = mean_shift.labels_
# Number of clusters in labels, ignoring noise if present.
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
print('Estimated number of clusters:', n_clusters_)
return labels
示例11: get_cld_bigest_clus
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def get_cld_bigest_clus(p3ds):
n_clus_jobs = 8
ms = MeanShift(
bandwidth=radius, bin_seeding=True, n_jobs=n_clus_jobs
)
ms.fit(p3ds)
clus_labels = ms.labels_
bg_clus = p3ds[np.where(clus_labels == 0)[0], :]
return bg_clus
示例12: main
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def main(filename):
# read csv files with daily data per tick
df = pandas.read_csv(filename, parse_dates=[0], index_col=0, names=['Date_Time', 'Buy', 'Sell'],
date_parser=lambda x: pandas.to_datetime(x, format="%d/%m/%y %H:%M:%S"))
# group by day and drop NA values (usually weekends)
grouped_data = df.dropna()
ticks_data = grouped_data['Sell'].resample('24H').ohlc()
# use 'ask'
sell_data = grouped_data.as_matrix(columns=['Sell'])
# calculate bandwidth (expirement with quantile and samples)
bandwidth = estimate_bandwidth(sell_data, quantile=0.1, n_samples=100)
ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
# fit the data
ms.fit(sell_data)
ml_results = []
for k in range(len(np.unique(ms.labels_))):
my_members = ms.labels_ == k
values = sell_data[my_members, 0]
# find the edges
ml_results.append(min(values))
ml_results.append(max(values))
# export the data for the visualizations
ticks_data.to_json('ticks.json', date_format='iso', orient='index')
# export ml support resisistance
with open('ml_results.json', 'w') as f:
f.write(json.dumps(ml_results))
print("Done. Goto 0.0.0.0:8000/chart.html")
示例13: cluster
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def cluster(prediction, bandwidth):
ms = MeanShift(bandwidth, bin_seeding=True)
# print ('Mean shift clustering, might take some time ...')
# tic = time.time()
ms.fit(prediction)
# print ('time for clustering', time.time() - tic)
labels = ms.labels_
cluster_centers = ms.cluster_centers_
num_clusters = cluster_centers.shape[0]
return num_clusters, labels, cluster_centers
示例14: compare
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def compare(data, n_groups, output_fol):
# plot_clusters(data.astype(np.float), scipy.cluster.vq.kmeans, 'scipy.cluster.vq.kmeans', output_fol, (n_groups,), {})
plot_clusters(data, cluster.KMeans, 'KMeans', output_fol, (), {'n_clusters': n_groups})
for ct in ['spherical', 'tied', 'diag', 'full']:
plot_clusters(data, mixture.GaussianMixture, 'GMM_{}'.format(ct), output_fol, (),
{'n_components': n_groups, 'covariance_type': ct})
plot_clusters(data, cluster.AffinityPropagation, 'AffinityPropagation', output_fol, (), {'preference': -5.0, 'damping': 0.95})
plot_clusters(data, cluster.MeanShift, 'MeanShift', output_fol, (0.175,), {'cluster_all': False})
plot_clusters(data, cluster.SpectralClustering, 'SpectralClustering', output_fol, (), {'n_clusters': n_groups})
plot_clusters(data, cluster.AgglomerativeClustering, 'AgglomerativeClustering', output_fol, (), {'n_clusters': n_groups, 'linkage': 'ward'})
plot_clusters(data, cluster.DBSCAN, 'DBSCAN', output_fol, (), {'eps': 0.025})
# plot_clusters(data, hdbscan.HDBSCAN, 'HDBSCAN', output_fol, (), {'min_cluster_size': 15})
示例15: cluster
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import MeanShift [as 別名]
def cluster(prediction, bandwidth):
ms = MeanShift(bandwidth, bin_seeding=True)
print ('Mean shift clustering, might take some time ...')
tic = time.time()
ms.fit(prediction)
print ('time for clustering', time.time() - tic)
labels = ms.labels_
cluster_centers = ms.cluster_centers_
num_clusters = cluster_centers.shape[0]
return num_clusters, labels, cluster_centers
開發者ID:hq-jiang,項目名稱:instance-segmentation-with-discriminative-loss-tensorflow,代碼行數:14,代碼來源:clustering.py