索引6超出了尺寸为2的轴0的范围

索引6超出了尺寸为2的轴0的范围

我是SVM(RBF内核),用于学习我的数据并尝试找到最佳的伽玛和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和伽马的组合。

那么什么是最佳?使用GridSearchCV。无需编写大量代码。

关于python - IndexError:索引6超出了尺寸为2的轴0的范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52511424/

10-12 19:26