As was discussed in Chapter 2, Working with Linear Models, Stochastic Gradient Descent is a fundamental technique to fit a model for regression. There are natural connections between the two techniques, as the name so obviously implies.
正如在第二章讨论的运行线性模型,随机梯度下降对于拟合回归模型是一个基本的技术,在两个技术间,像它们的名字指出的那样有些自然联系。
Getting ready准备工作
In regression, we minimized a cost function that penalized for bad choices on a continuous scale, but for classification, we'll minimize a cost function that penalizes for two (or more) cases.
在回归模型中,我们用惩罚在连续范围内的错误选择来最小化代价函数,但是对于分类方法,我们用惩罚两个或多个例子来最小化损失函数。
How to do it…怎么做
First, let's create some very basic data:首先,让我们生成一些非常基本的数据集。
from sklearn import datasets
X, y = datasets.make_classification()
Next, we'll create a SGDClassifier instance:然后,我们将生成一个SGDClassifier例子
from sklearn import linear_model
sgd_clf = linear_model.SGDClassifier()
As usual, we'll fit the model:和以前一样,来拟合模型:
sgd_clf.fit(X, y)
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_no_change=5, n_jobs=None, penalty='l2',
power_t=0.5, random_state=None, shuffle=True, tol=0.001,
validation_fraction=0.1, verbose=0, warm_start=False)
We can set the class_weight parameter to account for the varying amounts of unbalance in a dataset.
我们能设置class_weight参数来调整数据集中不均衡的变化情况。
The Hinge loss function is defined as follows:以下是定义的hinge损失函数
Here, t is the true classification denoted as +1 for one case and -1 for the other. The vector of coefficients is denoted by y as fit from the model, and x is the value of interest. There is also an intercept for good measure. To put it another way:
这里,t是一个真实的分类标志+1为一类和-1为另一类。系数的矢量被标志模型中拟合的y。x是感兴趣的一个值。一般还有个额外的截距,展示为另一种方法。
本文系外文翻译,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系外文翻译,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。