【问题标题】:IndexError: index 6 is out of bounds for axis 0 with size 2IndexError:索引 6 超出轴 0 的范围,大小为 2
【发布时间】:2019-03-01 20:37:50
【问题描述】:

我是 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 号。但我不知道如何使它工作。

【问题讨论】:

  • 哪一行报错?
  • best_gamma = gamma[np.argmax(avg_rbf_f1)] 这个,best_C 有效
  • best_C 偶然“工作”。引用你没有得到正确答案,它只是停留在界限内,因此不会产生错误。

标签: python machine-learning svm


【解决方案1】:

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

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。无需大量编码。

【讨论】:

  • @Poppy 不客气!请接受我的回答,以便其他人知道它已解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-16
  • 2020-04-24
  • 2016-07-29
  • 2021-07-17
  • 2021-05-24
相关资源
最近更新 更多