支持向量类型错误。 none 类型对象不可迭代
Support Vector Type error. none type object is not iterable
我在 python 中有这段代码,它使用不同的内核创建 SVM:
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, roc_auc_score
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
# The gamma parameter is the kernel coefficient for kernels rbf/poly/sigmoid
svm = SVC(gamma='auto', probability=True)
svm.fit(X_train,y_train.values.ravel())
prediction = svm.predict(X_test)
prediction_prob = svm.predict_proba(X_test)
print('Accuracy:', accuracy_score(y_test,prediction))
print('AUC:',roc_auc_score(y_test,prediction_prob[:,1]))
print(X_train)
print(y_train)
现在我想用不同的内核 rbf 构建它并将值存储到 arrays.I 需要将折叠的结果存储到列表中,如下所示:所以像这样
def svm_grid_search(parameters, cv):
# Store the outcome of the folds in these lists
means = []
stds = []
params = []
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
for parameter in parameters:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
# The gamma parameter is the kernel coefficient for kernels rbf/poly/sigmoid
svm = SVC(gamma=1,kernel ='rbf',probability=True)
svm.fit(X_train,y_train.values.ravel())
prediction = svm.predict(X_test)
prediction_prob = svm.predict_proba(X_test)
return means, stddevs, params
使用不同的参数和内核函数,答案应该类似于下面显示的代码:
from sklearn.model_selection import GridSearchCV
parameters = {'kernel':['linear','poly','rbf'],'C':[0.2,0.5,1.0]}
means, stddevs, params = svm_grid_search(parameters, 10)
print('Mean AUC (+/- standard deviation), for parameters')
for mean, std, params in zip(means, stddevs, params):
print("%0.3f (+/- %0.03f) for %r"
% (mean, std, params))
但是我一直收到这个错误,这表明我没有正确地通过列表传递折叠?
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-3f5f99897a92> in <module>()
3 parameters = {'kernel':['linear','poly','rbf'],'C':[0.2,0.5,1.0]}
4
----> 5 means, stddevs, params = svm_grid_search(parameters, 10)
6
7 print('Mean AUC (+/- standard deviation), for parameters')
TypeError: 'NoneType' object is not iterable
如果有人能指出正确的方向,我将不胜感激
看起来您实际上并没有在函数中创建的列表中存储任何内容。此外,您正在创建一个名为 stds 的空列表,但返回 stddevs(您从未定义)。
编辑:我认为您正在尝试使用 sklearns GridSerachCV,但您从未调用过两者。下面是一些帮助您入门的代码:
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn import datasets
import pandas as pd
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.
y = iris.target
parameters = {'kernel':['linear','poly','rbf'],'C':[0.2,0.5,1.0]}
svc = SVC()
clf = GridSearchCV(svc, parameters)
clf.fit(X,y)
results = pd.DataFrame.from_dict(clf.cv_results_)
其中 results 就是您的网格搜索的结果。
mean_fit_time std_fit_time mean_score_time std_score_time param_C
0 0.000600 0.000490 0.0004 0.000490 0.2
1 0.000400 0.000490 0.0002 0.000400 0.2
2 0.000600 0.000490 0.0004 0.000490 0.2
3 0.001008 0.000013 0.0000 0.000000 0.5
4 0.000800 0.000400 0.0002 0.000399 0.5
5 0.000800 0.000400 0.0004 0.000490 0.5
6 0.000800 0.000400 0.0000 0.000000 1.0
7 0.000800 0.000400 0.0002 0.000400 1.0
8 0.000800 0.000400 0.0002 0.000400 1.0
如果您想将其包装在您自己的函数中,那么您需要阅读一些关于 python 函数和 GridSearchCV returns 的内容。
希望对你有帮助。
我在 python 中有这段代码,它使用不同的内核创建 SVM:
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, roc_auc_score
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
# The gamma parameter is the kernel coefficient for kernels rbf/poly/sigmoid
svm = SVC(gamma='auto', probability=True)
svm.fit(X_train,y_train.values.ravel())
prediction = svm.predict(X_test)
prediction_prob = svm.predict_proba(X_test)
print('Accuracy:', accuracy_score(y_test,prediction))
print('AUC:',roc_auc_score(y_test,prediction_prob[:,1]))
print(X_train)
print(y_train)
现在我想用不同的内核 rbf 构建它并将值存储到 arrays.I 需要将折叠的结果存储到列表中,如下所示:所以像这样
def svm_grid_search(parameters, cv):
# Store the outcome of the folds in these lists
means = []
stds = []
params = []
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
for parameter in parameters:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3)
# The gamma parameter is the kernel coefficient for kernels rbf/poly/sigmoid
svm = SVC(gamma=1,kernel ='rbf',probability=True)
svm.fit(X_train,y_train.values.ravel())
prediction = svm.predict(X_test)
prediction_prob = svm.predict_proba(X_test)
return means, stddevs, params
使用不同的参数和内核函数,答案应该类似于下面显示的代码:
from sklearn.model_selection import GridSearchCV
parameters = {'kernel':['linear','poly','rbf'],'C':[0.2,0.5,1.0]}
means, stddevs, params = svm_grid_search(parameters, 10)
print('Mean AUC (+/- standard deviation), for parameters')
for mean, std, params in zip(means, stddevs, params):
print("%0.3f (+/- %0.03f) for %r"
% (mean, std, params))
但是我一直收到这个错误,这表明我没有正确地通过列表传递折叠?
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-3f5f99897a92> in <module>()
3 parameters = {'kernel':['linear','poly','rbf'],'C':[0.2,0.5,1.0]}
4
----> 5 means, stddevs, params = svm_grid_search(parameters, 10)
6
7 print('Mean AUC (+/- standard deviation), for parameters')
TypeError: 'NoneType' object is not iterable
如果有人能指出正确的方向,我将不胜感激
看起来您实际上并没有在函数中创建的列表中存储任何内容。此外,您正在创建一个名为 stds 的空列表,但返回 stddevs(您从未定义)。
编辑:我认为您正在尝试使用 sklearns GridSerachCV,但您从未调用过两者。下面是一些帮助您入门的代码:
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn import datasets
import pandas as pd
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features.
y = iris.target
parameters = {'kernel':['linear','poly','rbf'],'C':[0.2,0.5,1.0]}
svc = SVC()
clf = GridSearchCV(svc, parameters)
clf.fit(X,y)
results = pd.DataFrame.from_dict(clf.cv_results_)
其中 results 就是您的网格搜索的结果。
mean_fit_time std_fit_time mean_score_time std_score_time param_C
0 0.000600 0.000490 0.0004 0.000490 0.2
1 0.000400 0.000490 0.0002 0.000400 0.2
2 0.000600 0.000490 0.0004 0.000490 0.2
3 0.001008 0.000013 0.0000 0.000000 0.5
4 0.000800 0.000400 0.0002 0.000399 0.5
5 0.000800 0.000400 0.0004 0.000490 0.5
6 0.000800 0.000400 0.0000 0.000000 1.0
7 0.000800 0.000400 0.0002 0.000400 1.0
8 0.000800 0.000400 0.0002 0.000400 1.0
如果您想将其包装在您自己的函数中,那么您需要阅读一些关于 python 函数和 GridSearchCV returns 的内容。 希望对你有帮助。