Reputation: 133
I'm trying to code SVM algorithm from the scratch without using sklearn package, now I want to test the accuracy score of my X_test and Y_predict. The sklearn had already function for this:
clf.score(X_test,Y_predict)
Now, I traced the code from the sklearn package, I cannot find how the 'score' function has coded from the scratch.
And how the model generated from the 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)
After I fitted and trained the dataset, I want that the model will be generated so that I can Save and Load it using Pickle.
Upvotes: 1
Views: 3168
Reputation: 3076
If you use IPython you can usually find out where functions are defined with by appending ??
to the function. For example:
>>> 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
In this case it's coming from the ClassifierMixin
so this code can be used with all classifiers.
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
Upvotes: 1