Scikit-Learn
60
Attributes
Description
coef_
: array, shape (1, n_features) if
n_classes==2, else (n_classes, n_features)
This attribute provides the weight assigned to the
features.
intercept_
:
array,
shape
(1,)
if
n_classes==2, else (n_classes,)
It represents the independent
term in decision
function.
n_iter_:
int
It gives the number
of iterations to reach the
stopping criterion.
Implementation Example
Like other classifiers, Stochastic Gradient Descent (SGD) has to be fitted with following
two arrays:
An array X holding the training samples. It is of size [n_samples, n_features].
An array Y holding the target values i.e. class labels for the training samples. It is
of size [n_samples].
Following Python script uses SGDClassifier linear model
:
import
numpy as np
from sklearn import linear_model
X = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1]])
Y = np.array([1, 1, 2, 2])
SGDClf = linear_model.SGDClassifier(max_iter=1000, tol=1e-
3,penalty="elasticnet")
SGDClf.fit(X, Y)
Output
SGDClassifier(alpha=0.0001, average=False, class_weight=None,
early_stopping=False, epsilon=0.1, eta0=0.0, fit_intercept=True,
l1_ratio=0.15, learning_rate='optimal', loss='hinge', max_iter=1000,
n_iter=None, n_iter_no_change=5, n_jobs=None, penalty='elasticnet',
power_t=0.5, random_state=None, shuffle=True, tol=0.001,
validation_fraction=0.1, verbose=0, warm_start=False)
Now, once fitted, the model can predict new values as follows:
SGDClf.predict([[2.,2.]])
Dostları ilə paylaş: