from sklearn.svm import SVC
from sklearn.datasets import make_blobs
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
X, y = make_blobs(n_samples=500, n_features=2, centers=2, random_state=34)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
clf = SVC()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print(clf.score(X_test, y_test) == accuracy_score(y_test, y_pred))
上述代码的输出如下:
True
我不知道他们之间有什么区别,谁能告诉我吗?
编辑: score
函数的源代码是:
def score(self, X, y, sample_weight=None):
"""
Return 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 of shape (n_samples, n_features)
Test samples.
y : array-like of shape (n_samples,) or (n_samples, n_outputs)
True labels for `X`.
sample_weight : array-like of shape (n_samples,), default=None
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)
也就是说,score
函数是用accuracy_score
函数实现的,所以这两个函数是相同的?
发布于 2022-02-12 13:43:24
clf.score()
实际上是用于SVC类的,它返回给定数据和标签的平均精度。
另一方面,accuracy_score
返回分类正确执行的一小部分实例。例如,如果您传入10项以进行分类,并且其中7项被正确分类(不管clsss / False是什么,多类),它的返回值将是0.7
。您可以将一个标志规范化为False
,然后它只返回正确分类的实例数的整数。
https://stackoverflow.com/questions/71092069
复制相似问题