🌐
scikit-learn
scikit-learn.org › stable › modules › generated › sklearn.linear_model.SGDRegressor.html
SGDRegressor — scikit-learn 1.8.0 documentation
Theil-Sen Estimator robust multivariate regression model. ... Online Passive-Aggressive Algorithms <http://jmlr.csail.mit.edu/papers/volume7/crammer06a/crammer06a.pdf> K. Crammer, O. Dekel, J. Keshat, S. Shalev-Shwartz, Y. Singer - JMLR (2006) ... >>> import numpy as np >>> from sklearn.linear_model import SGDRegressor >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> n_samples, n_features = 10, 5 >>> rng = np.random.RandomState(0) >>> y = rng.randn(n_samples) >>> X = rng.randn(n_samples, n_features) >>> # Always scale the input.
🌐
scikit-learn
scikit-learn.org › stable › modules › sgd.html
1.5. Stochastic Gradient Descent — scikit-learn 1.8.0 documentation
For regression with a squared loss and a \(L_2\) penalty, another variant of SGD with an averaging strategy is available with Stochastic Average Gradient (SAG) algorithm, available as a solver in Ridge. ... The class sklearn.linear_model.SGDOneClassSVM implements an online linear version of the One-Class SVM using a stochastic gradient descent.
🌐
scikit-learn
scikit-learn.org › stable › modules › generated › sklearn.linear_model.SGDClassifier.html
SGDClassifier — scikit-learn 1.8.0 documentation
Logistic regression. ... Inherits from SGDClassifier. Perceptron() is equivalent to SGDClassifier(loss="perceptron", eta0=1, learning_rate="constant", penalty=None). ... Online Passive-Aggressive Algorithms <http://jmlr.csail.mit.edu/papers/volume7/crammer06a/crammer06a.pdf> K. Crammer, O. Dekel, J. Keshat, S. Shalev-Shwartz, Y. Singer - JMLR (2006) ... >>> import numpy as np >>> from sklearn.linear_model import SGDClassifier >>> from sklearn.preprocessing import StandardScaler >>> from sklearn.pipeline import make_pipeline >>> X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]]) >>> Y = np.array([1, 1, 2, 2]) >>> # Always scale the input.
🌐
TutorialsPoint
tutorialspoint.com › scikit_learn › scikit_learn_stochastic_gradient_descent.htm
Scikit Learn - Stochastic Gradient Descent
Attributes of SGDRegressor are also same as that were of SGDClassifier module. Rather it has three extra attributes as follows − ... As name suggest, it provides the average weights assigned to the features. ... As name suggest, it provides the averaged intercept term. ... It provides the number of weight updates performed during the training phase. Note − the attributes average_coef_ and average_intercept_ will work after enabling parameter average to True. ... import numpy as np from sklearn import linear_model n_samples, n_features = 10, 5 rng = np.random.RandomState(0) y = rng.randn(n_samples) X = rng.randn(n_samples, n_features) SGDReg =linear_model.SGDRegressor( max_iter = 1000,penalty = "elasticnet",loss = 'huber',tol = 1e-3, average = True ) SGDReg.fit(X, y)
🌐
scikit-learn
ogrisel.github.io › scikit-learn.org › sklearn-tutorial › modules › generated › sklearn.linear_model.SGDRegressor.html
8.15.1.18. sklearn.linear_model.SGDRegressor — scikit-learn 0.11-git documentation
>>> import numpy as np >>> from sklearn import linear_model >>> n_samples, n_features = 10, 5 >>> np.random.seed(0) >>> y = np.random.randn(n_samples) >>> X = np.random.randn(n_samples, n_features) >>> clf = linear_model.SGDRegressor() >>> clf.fit(X, y) SGDRegressor(alpha=0.0001, eta0=0.01, fit_intercept=True, learning_rate='invscaling', loss='squared_loss', n_iter=5, p=0.1, penalty='l2', power_t=0.25, rho=0.85, seed=0, shuffle=False, verbose=0, warm_start=False)
🌐
Datatechnotes
datatechnotes.com › 2020 › 09 › regression-example-with-sgdregressor-in-python.html
DataTechNotes: Regression Example with SGDRegressor in Python
In this tutorial, we've briefly learned how to fit and predict regression data by using Scikit-learn API's SGDRegressor class in Python. The full source code is listed below. ... from sklearn.linear_model import SGDRegressor from sklearn.datasets import load_boston from sklearn.datasets import make_regression from sklearn.metrics import mean_squared_error from sklearn.model_selection import train_test_split from sklearn.model_selection import cross_val_score from sklearn.preprocessing import scale import matplotlib.pyplot as plt x, y = make_regression(n_samples=1000, n_features=30) print(x[0:2
🌐
scikit-learn
scikit-learn.org › 0.15 › modules › generated › sklearn.linear_model.SGDRegressor.html
sklearn.linear_model.SGDRegressor — scikit-learn 0.15-git documentation
>>> import numpy as np >>> from sklearn import linear_model >>> n_samples, n_features = 10, 5 >>> np.random.seed(0) >>> y = np.random.randn(n_samples) >>> X = np.random.randn(n_samples, n_features) >>> clf = linear_model.SGDRegressor() >>> clf.fit(X, y) SGDRegressor(alpha=0.0001, epsilon=0.1, eta0=0.01, fit_intercept=True, l1_ratio=0.15, learning_rate='invscaling', loss='squared_loss', n_iter=5, penalty='l2', power_t=0.25, random_state=None, shuffle=False, verbose=0, warm_start=False)
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › stochastic-gradient-descent-regressor-using-scikit-learn
Stochastic Gradient Descent Regressor using Scikit-learn - GeeksforGeeks
July 23, 2025 - We will use the diabetes dataset to build and evaluate a linear regression model using SGD. ... from sklearn.datasets import load_diabetes import numpy as np from sklearn.linear_model import LinearRegression from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split
🌐
scikit-learn
scikit-learn.org › 1.5 › modules › generated › sklearn.linear_model.SGDRegressor.html
SGDRegressor — scikit-learn 1.5.2 documentation
Theil-Sen Estimator robust multivariate regression model. ... >>> import numpy as np >>> from sklearn.linear_model import SGDRegressor >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> n_samples, n_features = 10, 5 >>> rng = np.random.RandomState(0) >>> y = rng.randn(n_samples) >>> X = rng.randn(n_samples, n_features) >>> # Always scale the input.
Find elsewhere
🌐
scikit-learn
scikit-learn.org › 0.15 › modules › sgd.html
1.3. Stochastic Gradient Descent — scikit-learn 0.15-git documentation
The class SGDRegressor implements a plain stochastic gradient descent learning routine which supports different loss functions and penalties to fit linear regression models.
🌐
scikit-learn
scikit-learn.org › dev › modules › generated › sklearn.linear_model.SGDRegressor.html
SGDRegressor — scikit-learn 1.9.dev0 documentation
Theil-Sen Estimator robust multivariate regression model. ... Online Passive-Aggressive Algorithms <http://jmlr.csail.mit.edu/papers/volume7/crammer06a/crammer06a.pdf> K. Crammer, O. Dekel, J. Keshat, S. Shalev-Shwartz, Y. Singer - JMLR (2006) ... >>> import numpy as np >>> from sklearn.linear_model import SGDRegressor >>> from sklearn.pipeline import make_pipeline >>> from sklearn.preprocessing import StandardScaler >>> n_samples, n_features = 10, 5 >>> rng = np.random.RandomState(0) >>> y = rng.randn(n_samples) >>> X = rng.randn(n_samples, n_features) >>> # Always scale the input.
🌐
Evening Session
sdsawtelle.github.io › blog › output › week2-andrew-ng-machine-learning-with-python.html
LinearRegression vs. SGDRegressor
September 1, 2016 - If you really want to get gradient descent in scikit-learn then the relevant object is called SGDRegressor (or for classification problems SGDClassifier - recall classification is where your labels are discrete category-like).
🌐
GeeksforGeeks
geeksforgeeks.org › python › stochastic-gradient-descent-regressor
Stochastic Gradient Descent Regressor - GeeksforGeeks
July 23, 2025 - from sklearn.linear_model import SGDRegressor sgd_regressor = SGDRegressor(parameters,...)
🌐
scikit-learn
ogrisel.github.io › scikit-learn.org › sklearn-tutorial › modules › sgd.html
3.3. Stochastic Gradient Descent — scikit-learn 0.11-git documentation
The class SGDRegressor implements a plain stochastic gradient descent learning routine which supports different loss functions and penalties to fit linear regression models.
🌐
scikit-learn
scikit-learn.org › 1.5 › modules › sgd.html
1.5. Stochastic Gradient Descent — scikit-learn 1.5.2 documentation
For regression with a squared loss and a l2 penalty, another variant of SGD with an averaging strategy is available with Stochastic Average Gradient (SAG) algorithm, available as a solver in Ridge. The class sklearn.linear_model.SGDOneClassSVM implements an online linear version of the One-Class ...
🌐
SourceForge
scikit-learn.sourceforge.net › stable › modules › sgd.html
1.5. Stochastic Gradient Descent — scikit-learn 0.16.1 documentation
The class SGDRegressor implements a plain stochastic gradient descent learning routine which supports different loss functions and penalties to fit linear regression models.
🌐
GitHub
github.com › scikit-learn › scikit-learn › blob › main › sklearn › linear_model › _stochastic_gradient.py
scikit-learn/sklearn/linear_model/_stochastic_gradient.py at main · scikit-learn/scikit-learn
"""Classification, regression and One-Class SVM using Stochastic Gradient · Descent (SGD). """ · import warnings · from abc import ABCMeta, abstractmethod · from numbers import Integral, Real · · import numpy as np · · from sklearn._loss._loss import CyHalfBinomialLoss, CyHalfSquaredError, CyHuberLoss ·
Author   scikit-learn
🌐
scikit-learn
scikit-learn.org › 0.19 › modules › generated › sklearn.linear_model.SGDRegressor.html
sklearn.linear_model.SGDRegressor — scikit-learn 0.19.2 documentation
class sklearn.linear_model.SGDRegressor(loss=’squared_loss’, penalty=’l2’, alpha=0.0001, l1_ratio=0.15, fit_intercept=True, max_iter=None, tol=None, shuffle=True, verbose=0, epsilon=0.1, random_state=None, learning_rate=’invscaling’, eta0=0.01, power_t=0.25, warm_start=False, average=False, n_iter=None)[source]¶
Top answer
1 of 2
1

With n=10000 data points (draw samples with replacement from your 3 original points) you get the following results with SGD

n = 10000

X = np.random.choice([0,0.5,1], n, replace=True)
y = X

X = X.reshape((n,1))

sgd = SGDRegressor(verbose=1)  
sgd.fit(X, y)

# -- Epoch 1
# Norm: 0.86, NNZs: 1, Bias: 0.076159, T: 10000, Avg. loss: 0.012120
# Total training time: 0.04 seconds.
# -- Epoch 2
# Norm: 0.96, NNZs: 1, Bias: 0.024337, T: 20000, Avg. loss: 0.000586
# Total training time: 0.04 seconds.
# -- Epoch 3
# Norm: 0.98, NNZs: 1, Bias: 0.008826, T: 30000, Avg. loss: 0.000065
# Total training time: 0.04 seconds.
# -- Epoch 4
# Norm: 0.99, NNZs: 1, Bias: 0.003617, T: 40000, Avg. loss: 0.000010
# Total training time: 0.04 seconds.
# -- Epoch 5
# Norm: 1.00, NNZs: 1, Bias: 0.001686, T: 50000, Avg. loss: 0.000002
# Total training time: 0.05 seconds.
# -- Epoch 6
# Norm: 1.00, NNZs: 1, Bias: 0.000911, T: 60000, Avg. loss: 0.000000
# Total training time: 0.05 seconds.
# -- Epoch 7
# Norm: 1.00, NNZs: 1, Bias: 0.000570, T: 70000, Avg. loss: 0.000000
# Total training time: 0.05 seconds.
# Convergence after 7 epochs took 0.05 seconds

print("intercept=", sgd.intercept_)
print("coef=", sgd.coef_)
# intercept= [0.00057032]
# coef= [0.99892893]

plt.plot(X, y, 'r.')
plt.plot(X, sgd.intercept_ + sgd.coef_*X, 'b-')

The following animation shows how SGD regressor starts converging to the correct optima as n goes up in the above code:

2 of 2
0

SGD (Stochastic Gradient Descent) is used for large scale data. For such a trivial amount I would advise you to use a simple Linear Regression instead. As stated by the "No Free Lunch Theorem", there is not one model fits all solution and hence you should often experiment with different models to find an optimal (however you should also be aware of the background of your data such as distribution types, diversity factor, skewness, etc.). Check out the below model instead:

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X,y.ravel())
lr.predict([[0],[0.5],[1]])
# output -> array([1.11022302e-16, 5.00000000e-01, 1.00000000e+00])