<jupyter_start><jupyter_text>Import necessary packages<jupyter_code>from sklearn.linear_model import LogisticRegression, Perceptron
from sklearn.svm import LinearSVC
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.close_figures=False<jupyter_output><empty_output><jupyter_text>Generate random training data with positive and negative samples that are linearly separable<jupyter_code>negative = []
for i in range(100):
    rn1 = (np.random.random_sample()*5)
    rn2 = (np.random.random_sample()*5)
    negative.append([rn1,rn2])
outputs = [0] * 100

positive = []
for i in range(100):
    rn1 = (np.random.random_sample()+5)*2
    rn2 = (np.random.random_sample()+5)*2
    positive.append([rn1,rn2])

inputs = negative + positive
outputs = outputs + [1]*100

xs, ys = list(zip(*inputs))
colors = []
for o in outputs:
    if o:
        colors.append('blue')
    else:
        colors.append('red')
fig, ax = plt.subplots()
ax.scatter(xs, ys, color=colors)<jupyter_output><empty_output><jupyter_text>Run Perceptron and plot the decision boundary<jupyter_code>p = Perceptron()
p.fit(inputs, outputs)

w = p.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(0, 15)
yy = a * xx - (p.intercept_[0]) / w[1]

ax.plot(xx, yy, color='red', label='Perceptron')
ax.legend(loc='upper left')
plt.show()<jupyter_output>/home/dgmain-ind5/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/stochastic_gradient.py:166: FutureWarning: max_iter and tol parameters have been added in Perceptron in 0.19. If both are left unset, they default to max_iter=5 and tol=None. If tol is not None, max_iter defaults to max_iter=1000. From 0.21, default max_iter will be 1000, and default tol will be 1e-3.
  FutureWarning)<jupyter_text>Run logistic regression and plot the decision boundary<jupyter_code>lr = LogisticRegression()
lr.fit(inputs, outputs)

w = lr.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(0, 15)
yy = a * xx - (lr.intercept_[0]) / w[1]

ax.plot(xx, yy, color='magenta', label='Logistic Regression')
plt.show()<jupyter_output>/home/dgmain-ind5/anaconda3/lib/python3.6/site-packages/sklearn/linear_model/logistic.py:433: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)<jupyter_text>Run Support Vector Machine and plot the decision boundary<jupyter_code>svc = LinearSVC()
svc.fit(inputs, outputs)

w = svc.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(0, 15)
yy = a * xx - (svc.intercept_[0]) / w[1]

ax.plot(xx, yy, color='green', label='SVM')
ax.legend(loc='upper left')
plt.show()<jupyter_output>/home/dgmain-ind5/anaconda3/lib/python3.6/site-packages/sklearn/svm/base.py:931: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
  "the number of iterations.", ConvergenceWarning)
{'fraction_non_alphanumeric': 0.1356534090909091, 'fraction_numerical': 0.033025568181818184, 'mean_word_length': 5.137254901960785, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '568755', 'license': 'MIT', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1038, 'n_words': 250, 'path': 'comparing-linear-classifiers.ipynb', 'repo_name': 'brijml/greyatomSVM', 'size': '2816'}