<jupyter_start><jupyter_text>New to Plotly?Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).You can set up Plotly to work in [online](https://plot.ly/python/getting-started/initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/start-plotting-online).We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!  Version<jupyter_code>import sklearn
sklearn.__version__<jupyter_output><empty_output><jupyter_text>Imports This tutorial imports [MeanShift](http://scikit-learn.org/stable/modules/generated/sklearn.cluster.MeanShift.htmlsklearn.cluster.MeanShift), [estimate_bandwidth](http://scikit-learn.org/stable/modules/generated/sklearn.cluster.estimate_bandwidth.htmlsklearn.cluster.estimate_bandwidth), [make_blobs](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_blobs.htmlsklearn.datasets.make_blobs).<jupyter_code>print(__doc__)

import plotly.plotly as py
import plotly.graph_objs as go
import plotly

import numpy as np
from sklearn.cluster import MeanShift, estimate_bandwidth
from sklearn.datasets.samples_generator import make_blobs<jupyter_output>Automatically created module for IPython interactive environment<jupyter_text>Calculations Generate sample data.<jupyter_code>centers = [[1, 1], [-1, -1], [1, -1]]
X, _ = make_blobs(n_samples=10000, centers=centers, cluster_std=0.6)<jupyter_output><empty_output><jupyter_text>Compute clustering with MeanShift.<jupyter_code># The following bandwidth can be automatically detected using
bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=500)

ms = MeanShift(bandwidth=bandwidth, bin_seeding=True)
ms.fit(X)
labels = ms.labels_
cluster_centers = ms.cluster_centers_

labels_unique = np.unique(labels)
n_clusters_ = len(labels_unique)

print("number of estimated clusters : %d" % n_clusters_)<jupyter_output>number of estimated clusters : 3<jupyter_text>Plot Results<jupyter_code>colors = ['blue','green','red','cyan','magenta']
cluster = [ ] 
for k, col in zip(range(n_clusters_), colors):
    my_members = labels == k
    cluster_center = cluster_centers[k]
    trace = go.Scatter(x=X[my_members, 0], y=X[my_members, 1], 
                       mode='markers', showlegend=False, 
                       marker= dict(color=col, size=10,))
    
    trace1 = go.Scatter(x=[cluster_center[0]], y=[cluster_center[1]], 
                        mode='markers',  showlegend=False,
                        marker=dict(color=col, size=20,
                               line=dict(color = 'black',
                                           width = 1)))
    cluster.append(trace)
    cluster.append(trace1)
    
layout = go.Layout(title = 'Estimated number of clusters: %d' % n_clusters_,
                   xaxis = dict(showgrid = False, zeroline = False),
                   yaxis = dict(showgrid = False, zeroline = False),)

fig = go.Figure(data = cluster, layout = layout)
py.iplot(fig)<jupyter_output><empty_output><jupyter_text>Reference <NAME> and <NAME>, “Mean Shift: A robust approach toward feature space analysis”. IEEE Transactions on Pattern Analysis and Machine Intelligence. 2002. pp. 603-619.<jupyter_code>from IPython.display import display, HTML

display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))

! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
    'mean-shift clustering algorithm.ipynb', 'scikit-learn/plot-mean-shift/', 'Mean-Shift Clustering Algorithm | plotly',
    ' ',
    title = 'Mean-Shift Clustering Algorithm | plotly',
    name = 'Mean-Shift Clustering Algorithm',
    has_thumbnail='true', thumbnail='thumbnail/mean-shift.jpg', 
    language='scikit-learn', page_type='example_index',
    display_as='clustering', order=1,
    ipynb= '~Diksha_Gabha/2750')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12965076466560146, 'fraction_numerical': 0.014380278475233965, 'mean_word_length': 4.758212877792379, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '112128', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1384, 'n_words': 262, 'path': '_posts/scikit/mean-shift-clustering-algorithm/mean-shift clustering algorithm.ipynb', 'repo_name': 'bmb804/documentation', 'size': '4394'}