<jupyter_start><jupyter_text>This estimator implements regularized linear models with stochastic gradient descent (SGD) learning: the gradient of the loss is estimated each sample at a time and the model is updated along the way with a decreasing strength schedule (aka learning rate). SGD allows minibatch (online/out-of-core) learning, see the partial_fit method. For best results using the default learning rate schedule, the data should have zero mean and unit variance.This implementation works with data represented as dense or sparse arrays of floating point values for the features. The model it fits can be controlled with the loss parameter; by default, it fits a linear support vector machine (SVM).The regularizer is a penalty added to the loss function that shrinks model parameters towards the zero vector using either the squared euclidean norm L2 or the absolute norm L1 or a combination of both (Elastic Net). If the parameter update crosses the 0.0 value because of the regularizer, the update is truncated to 0.0 to allow for learning sparse models and achieve online feature selection.<jupyter_code>import numpy as np
import mglearn
import matplotlib.pyplot as plt
from sklearn.linear_model import SGDClassifier
from sklearn.datasets import make_blobs
%matplotlib inline

X, y = make_blobs(random_state=42)

sgdClassifier = SGDClassifier().fit(X, y)
print("Coefficient shape= {}".format(sgdClassifier.coef_.shape))
print("Intercept shape= {}".format(sgdClassifier.intercept_.shape))<jupyter_output>Coefficient shape= (3, 2)
Intercept shape= (3,)<jupyter_text>We see that the shape of the coef_ is (3, 2), meaning that each row of coef_ contains the coefficient vector for one of the three classes and each column holds the coefficient value for a specific feature (there are two in this dataset). The intercept_ is now a one-dimensional array, storing the intercepts for each class.<jupyter_code>print(sgdClassifier.coef_)
print(sgdClassifier.intercept_)
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
line = np.linspace(-15, 15)
for coef, intercept, color in zip(sgdClassifier.coef_, sgdClassifier.intercept_, mglearn.cm3.colors):
    plt.plot(line, -(line * coef[0] + intercept) / coef[1], color)

plt.ylim(-20, 30)
plt.xlim(-20, 25)
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.legend(["Class 0", "Class 1", "Class 2"], loc=(1.01, 0.3))
mglearn.plots.plot_2d_classification(sgdClassifier, X, fill=True, alpha=0.7)
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
line = np.linspace(-15, 15)
for coef, intercept, color in zip(sgdClassifier.coef_, sgdClassifier.intercept_, mglearn.cm3.colors):
    plt.plot(line, -(line * coef[0] + intercept) / coef[1], color)

plt.legend(["Class 0", "Class 1", "Class 2", "Line class 0", "Line class 1", "Line class 2"])
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0979706088173548, 'fraction_numerical': 0.02099370188943317, 'mean_word_length': 5.633410672853828, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '406271', 'license': 'Unlicense', 'n_tokens_mistral': 925, 'n_tokens_neox': 854, 'n_words': 347, 'path': 'Source/Chapter02/LinearModels/Classification/.ipynb_checkpoints/sgdClassifier-checkpoint.ipynb', 'repo_name': 'irmoralesb/IntroductionToMLWithPython', 'size': '2858'}