IndexError: index 6 is out of bounds for axis 0 with size 2

IndexError: index 6 is out of bounds for axis 0 with size 2

我是 SVM(RBF 内核)来学习我的数据并尝试找到最优的 gamma 和 C,我的代码是这样的:

from sklearn import svm

C = np.array([1, 10, 100, 1000])
gamma = np.array([1e-3, 1e-4])

avg_rbf_f1 = []

for a in C:
    for b in gamma:
        rbf_model = svm.SVC(kernel='rbf',C=a, gamma=b)
        rbf_scores = cross_val_score(rbf_model, X_train, y_train, cv=10, scoring='f1_macro')
        avg_rbf_f1.append(np.mean(rbf_scores))

best_gamma = gamma[np.argmax(avg_rbf_f1)]
best_C = C[np.argmax(avg_rbf_f1)]

print('The gamma with the highest accuracy is {}'.format(best_gamma))
print('The C with the highest accuracy is {}'.format(best_C))

我的标题是错误。我知道这可能是因为我的伽马只有 2 个大小。但我不知道如何让它工作。

为了得到答案,让我们以其他人可以重现问题的形式编写您的代码:

from sklearn import svm
from sklearn.model_selection import cross_val_score

np.random.seed(42)
X = np.random.rand(2000, 2)
y = np.random.randint(0,2,2000)

C = np.array([1, 10, 100, 1000])
gamma = np.array([1e-3, 1e-4])

avg_rbf_f1 = []

for a in C:
    for b in gamma:
        rbf_model = svm.SVC(kernel='rbf',C=a, gamma=b)
        rbf_scores = cross_val_score(rbf_model, X, y, cv=10, scoring='f1_macro')
        avg_rbf_f1.append(np.mean(rbf_scores))

best_gamma = gamma[np.argmax(avg_rbf_f1)]
best_C = C[np.argmax(avg_rbf_f1)]

print('The gamma with the highest accuracy is {}'.format(best_gamma))
print('The C with the highest accuracy is {}'.format(best_C))

错误本身:

IndexError                                Traceback (most recent call last)
<ipython-input-30-84d1adf5e2d9> in <module>()
     17         avg_rbf_f1.append(np.mean(rbf_scores))
     18 
---> 19 best_gamma = gamma[np.argmax(avg_rbf_f1)]
     20 best_C = C[np.argmax(avg_rbf_f1)]
     21 

IndexError: index 6 is out of bounds for axis 0 with size 2

超参数 gamma 有 2 个可能的值,而 avg_rbf_f1 是一个包含 8 个值的列表。在您当前实施网格搜索的方式中,您无法取回最佳参数。以下是修改代码以使其正常工作的方法:

from sklearn import svm
from sklearn.model_selection import cross_val_score

np.random.rand(42)
X = np.random.rand(2000, 2)
y = np.random.randint(0,2,2000)

C = np.array([1, 10, 100, 1000])
gamma = np.array([1e-3, 1e-4])

avg_rbf_f1 = []
search = []

for a in C:
    for b in gamma:
        search.append((a,b))
        rbf_model = svm.SVC(kernel='rbf',C=a, gamma=b)
        rbf_scores = cross_val_score(rbf_model, X, y, cv=10, scoring='f1_macro')
        avg_rbf_f1.append(np.mean(rbf_scores))

best_C, best_gamma = search[np.argmax(avg_rbf_f1)]

print('The gamma with the highest accuracy is {}'.format(best_gamma))
print('The C with the highest accuracy is {}'.format(best_C))

远非最佳。我只是添加了 search 列表来收集 C 和 gamma 的组合。

那什么是最佳的呢?使用 GridSearchCV。减轻您的大量编码负担。