简体   繁体   English

在 SVM 上出现错误“TypeError: an integer is required”

[英]Getting an error "TypeError: an integer is required" on SVM

I'm pretty new to coding so I get a bit confused about what my code actually means and dictates, so sorry if this is a dumb question or if I struggle to understand answers!我对编码很陌生,所以我对我的代码的实际含义和规定有点困惑,如果这是一个愚蠢的问题或者我很难理解答案,我很抱歉!

So here's the code I'm having issues with:所以这是我遇到问题的代码:

krn= ['linear', 'poly', 'rbf', 'sigmoid']
rng_C = np.arange(1,101,20)
rng_degree=np.arange(2,5)
rng_ga= ['auto', 'scale']
rng_m=np.arange(0.001,10,0.5)
best_score=0
for i in krn:
    for j in rng_C:
        for k in rng_ga:
            for m in rng_m:
                SVModel=SVC(kernel=i, C=j, degree=k, gamma=m)
                SVModel.fit(x_train, y_train)
                acc_score= accuracy_score(y_test, SVModel.predict(x_test))
                if best_score<acc_score:
                    best_score=acc_score
                    bi=i
                    bj=j
                    bk=k
                    bm=m
print(best_score, bi, bj, bk, bm)

The error screen I'm getting is:我得到的错误屏幕是:

TypeError                                 Traceback (most recent call last)
<ipython-input-9-5eb9a5279e65> in <module>
      5             for m in rng_m:
      6                 SVModel=SVC(kernel=i, C=j, degree=k, gamma=m)
----> 7                 SVModel.fit(x_train, y_train)
      8                 acc_score= accuracy_score(y_test, SVModel.predict(x_test))
      9                 if best_score<acc_score:

~\anaconda3\lib\site-packages\sklearn\svm\_base.py in fit(self, X, y, sample_weight)
    224 
    225         seed = rnd.randint(np.iinfo('i').max)
--> 226         fit(X, y, sample_weight, solver_type, kernel, random_seed=seed)
    227         # see comment on the other call to np.iinfo in this file
    228 

~\anaconda3\lib\site-packages\sklearn\svm\_base.py in _dense_fit(self, X, y, sample_weight, solver_type, kernel, random_seed)
    275         self.support_, self.support_vectors_, self._n_support, \
    276             self.dual_coef_, self.intercept_, self._probA, \
--> 277             self._probB, self.fit_status_ = libsvm.fit(
    278                 X, y,
    279                 svm_type=solver_type, sample_weight=sample_weight,

sklearn\svm\_libsvm.pyx in sklearn.svm._libsvm.fit()

TypeError: an integer is required

From what I've read so far, the issue seems to be that I'm somehow getting a string and it's getting input where I actually need an integer.从我目前所读的内容来看,问题似乎是我以某种方式获得了一个字符串,并且它在我实际需要 integer 的地方获得了输入。 I'm unsure of how to do that, and where I would add the potential code needed to fix the issue.我不确定如何做到这一点,以及我将在哪里添加解决问题所需的潜在代码。 If anyone has any insight I would greatly appreciate it!如果有人有任何见解,我将不胜感激!

You've mixed up rng_ga and rng_degree in the line for k in rng_ga: , so the degree value k becomes a string "auto" when passed to line SVModel=SVC(kernel=i, C=j, degree=k, gamma=m)for k in rng_ga:行中混合了rng_garng_degree ,因此degreek在传递到行"auto" SVModel=SVC(kernel=i, C=j, degree=k, gamma=m)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM