我正在嘗試使用 scikit-learn 構建優化的 SVM 分類模型,而且我在 Python 中相當新,而不是在一般的 ML 中。這是我正在使用的代碼:
# Training the SVM model on the Training set
from sklearn.svm import SVC
classifier = SVC()
# define evaluation
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=5, random_state=1)
# define search space
space = dict()
space['kernel'] = ["linear", "rbf", "sigmoid", "poly"]
space['C'] = [0.1, 1, 10, 100, 1000]
space['gamma'] = [1, 0.1, 0.01, 0.001, 0.0001]
space['tol'] = [1e-3, 1e-4, 1e-5, 1e-6]
# define search
search = RandomizedSearchCV(classifier, space, n_iter=500, scoring='accuracy', n_jobs=-1, cv=cv, random_state=1)
# execute search
result = search.fit(X_train, y_train)
# summarize result
print('Best Score: %s' % result.best_score_)
print('Best Hyperparameters: %s' % result.best_params_)
bestModel = result.best_estimator_
#Test
a = X_train
b = y_train
grid_predictions = bestModel.predict(a)
accuracy_score(b, grid_predictions)
我正在嘗試了解我的訓練資料的分類情況。我的問題是:為什么我得到不同的準確度輸出result.best_score_(這是最佳搜索模型的準確度)和accuracy_score(b, grid_predictions)(這是將確切的訓練資料提供給性能最佳的模型的地方)?
uj5u.com熱心網友回復:
不同之處在于best_score_顯示了最佳估計器的最佳分數(在您的情況下為準確性),“在遺漏資料上給出最高分數(或最小損失,如果指定)的估計器”。遺漏的資料來自您的交叉驗證,這意味著這是您的 CV 看不見的折疊的準確性(請記住,這發生在 a 內部RandomizedSearchCV)。
另一方面, 的輸出accuracy_score(b, grid_predictions)是由相同的預測器計算的,但在看不見的資料上:不是折疊,而是使用您的所有訓練資料(基于您提供的代碼)。
這意味著兩個指標的計算方式相同,使用相同的模型,但預測的是不同的資料集。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/435298.html
標籤:Python 机器学习 scikit-学习 支持向量机 网格搜索
