SVM算法:不使用sklearn包(从头编写代码)

我正在尝试从头开始编写SVM算法而不使用sklearn包,现在我想测试我的X_test和Y_predict的准确率得分。sklearn已经有了这个功能的函数:

clf.score(X_test,Y_predict)

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

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

SVM分类器 :: 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.pyType:      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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注