SVM 算法:不使用 sklearn 包(从头开始编码)

SVM Algorithm: Without using sklearn package (Coded From the Scratch)

我正在尝试在不使用 sklearn 包的情况下从头开始编写 SVM 算法,现在我想测试 X_test 和 Y_predict 的准确度分数。 sklearn 已经为此工作:

clf.score(X_test,Y_predict)

现在,我从 sklearn 包中追踪代码,我找不到 'score' 函数是如何从头开始编码的。

以及如何从 sklearn SVC 生成模型:

SVM classifier :: SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma=2, kernel='poly', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False)

在我拟合和训练数据集后,我希望生成模型,以便我可以使用 Pickle 保存和加载它。

如果您使用 IPython,您通常可以通过将 ?? 附加到函数来找出定义函数的位置。例如:

>>> from sklearn.svm import SVC
>>> svc = SVC()
>>> svc.score??
Signature: svc.score(X, y, sample_weight=None)
Source:   
    def score(self, X, y, sample_weight=None):
        """Returns the mean accuracy on the given test data and labels.

        In multi-label classification, this is the subset accuracy
        which is a harsh metric since you require for each sample that
        each label set be correctly predicted.

        Parameters
        ----------
        X : array-like, shape = (n_samples, n_features)
            Test samples.

        y : array-like, shape = (n_samples) or (n_samples, n_outputs)
            True labels for X.

        sample_weight : array-like, shape = [n_samples], optional
            Sample weights.

        Returns
        -------
        score : float
            Mean accuracy of self.predict(X) wrt. y.

        """
        from .metrics import accuracy_score
        return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
File:      ~/miniconda/lib/python3.6/site-packages/sklearn/base.py
Type:      method

在本例中,它来自 ClassifierMixin,因此此代码可用于所有分类器。

https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/base.py#L310

https://ipython.readthedocs.io/en/stable/interactive/python-ipython-diff.html#accessing-help