<jupyter_start><jupyter_text>Plot the maximum margin separating hyperplane within a two-class separable dataset using a linear Support Vector Machines classifier trained using SGD.  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 [SGDClassifier](http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.htmlsklearn.linear_model.SGDClassifier) and [make_blobs](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_blobs.htmlsklearn.datasets.make_blobs).<jupyter_code>import plotly.plotly as py
import plotly.graph_objs as go

import numpy as np
from sklearn.linear_model import SGDClassifier
from sklearn.datasets.samples_generator import make_blobs<jupyter_output><empty_output><jupyter_text>Calculations<jupyter_code># we create 50 separable points
X, Y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.60)

# fit the model
clf = SGDClassifier(loss="hinge", alpha=0.01, n_iter=200, fit_intercept=True)
clf.fit(X, Y)

# plot the line, the points, and the nearest vectors to the plane
xx = np.linspace(-1, 5, 10)
yy = np.linspace(-1, 5, 10)

X1, X2 = np.meshgrid(xx, yy)
Z = np.empty(X1.shape)
for (i, j), val in np.ndenumerate(X1):
    x1 = val
    x2 = X2[i, j]
    p = clf.decision_function([[x1, x2]])
    Z[i, j] = p[0]<jupyter_output><empty_output><jupyter_text>Plot Results<jupyter_code>levels = [-1.0, 0.0, 1.0]
linestyles = ['dashed', 'solid', 'dashed']
cmap = [[0, 'black'],[1, 'white']]

trace = go.Contour(x=xx, y=yy, z=Z, 
                   colorscale=cmap,
                   showscale=False,
                   ncontours=4,
                   contours=dict(coloring='lines',
                                 start=-1,
                                 size=1,
                                 end=2
                                ),
                  )

trace1 = go.Scatter(x=X[:, 0], y=X[:, 1],
                    mode='markers',
                    marker=dict(color=X[:, 0],
                                colorscale='Viridis',
                                showscale=False,
                                line=dict(color='black', width=1))
                   )
data = [trace, trace1]
layout = go.Layout(xaxis=dict(zeroline=False, showgrid=False),
                   yaxis=dict(zeroline=False, showgrid=False),
                   hovermode='closest')
fig = go.Figure(data=data, layout=layout)
py.iplot(fig)
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(
    'SGD Maximum Margin Separating Hyperplane.ipynb', 'scikit-learn/plot-sgd-separating-hyperplane/', 'SGD Maximum Margin Separating Hyperplane | plotly',
    ' ',
    title = 'SGD Maximum Margin Separating Hyperplane | plotly',
    name = 'SGD Maximum Margin Separating Hyperplane',
    has_thumbnail='true', thumbnail='thumbnail/sgd.jpg', 
    language='scikit-learn', page_type='example_index',
    display_as='linear_models', order=4,
    ipynb= '~Diksha_Gabha/3173')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13268053855569156, 'fraction_numerical': 0.018115055079559364, 'mean_word_length': 3.436482084690554, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '149079', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1332, 'n_words': 260, 'path': '_posts/scikit/SGD-Maximum-margin-separating-hyperplane/SGD Maximum Margin Separating Hyperplane.ipynb', 'repo_name': 'bmb804/documentation', 'size': '4085'}