I'm working on support vector machine where I extracted the best parameters for polynomial, linear and RBF using gridsearchcv . I wanted to separate C and degree values so that I can call the best parameters for my dataset for the fit function where I can compute the accuracy with the tuned parameters.
The dictionary with tuple is
({'C': 1, 'degree': 1}, {'C': 0.1, 'degree': 1}, {'C': 1, 'degree': 1})
I tried using
for i in c.items():
value=i
print(value)
But got error: AttributeError: 'tuple' object has no attribute 'items'
The progress of my code is:
def svc_param_selection(self, X, y, nfolds):
#We tune two hyperparameters C and d using svc_param_selection
#the slack penalty hyperparameter
Cs = [0.0001, 0.001, 0.01, 0.1, 1, 10, 100]
#degrees of polynomial kernel of svc
degrees = [1, 2, 3, 4, 5]
#initialize the paremeter grid as dictionary
param_grid = {'C': Cs, 'degree' : degrees}
#initialize search for best parameters using input nfold cross validation
search = grid_search.GridSearchCV(svm.SVC(kernel='poly'), param_grid, cv=nfolds)
search1 = grid_search.GridSearchCV(svm.SVC(kernel='linear'), param_grid, cv=nfolds)
search2 = grid_search.GridSearchCV(svm.SVC(kernel='rbf'), param_grid, cv=nfolds)
#fit the search object to input training data
search.fit(X, y)
search1.fit(X, y)
search2.fit(X, y)
#return the best parameters
search.best_params_
search1.best_params_
search2.best_params_
print("[*] Searching for the best parameters for fitting the data.......")
print("Parameters are :")
#print(search.best_params_)
#print(search1.best_params_)
#print(search2.best_params_)
return search.best_params_,search1.best_params_,search2.best_params_
def param_sel(self):
X_train,y_train,X_test,y_test=self.norm()
#self.svc_param_selection(X_train, y_train, 10)
degree=np.array([])
c= self.svc_param_selection(X_train, y_train, 10)
print(c)
for i in c.items():
value=i
print(value)
def fit(self):
X_train,y_train,X_test,y_test=self.norm()
final_svc_poly1 = svm.SVC(C=1, degree=1, kernel='poly')
final_svc_poly2 = svm.SVC(C=1, degree=1, kernel='linear')
final_svc_poly3 = svm.SVC(C=1, degree=1, kernel='rbf')
final_svc_poly1.fit(X_train, y_train)
final_svc_poly2.fit(X_train, y_train)
final_svc_poly3.fit(X_train, y_train)
print("[*] Computing accuracy of test dataset")
print("Accuracy with polynomial kernel",final_svc_poly1.score(X_test, y_test))
print("Accuracy with linear kernel",final_svc_poly2.score(X_test, y_test))
print("Accuracy with RBF kernel",final_svc_poly3.score(X_test, y_test))
Output:
[*] Searching for the best parameters for fitting the data.......
Parameters are :
({'C': 1, 'degree': 1}, {'C': 0.1, 'degree': 1}, {'C': 1, 'degree': 1})
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-106-4be4dd58a851> in <module>
1 if __name__=='__main__':
----> 2 main()
<ipython-input-105-7b466d716ef5> in main()
7 #X_train,y_train=first.split_data()
8 #print(X_train)
----> 9 param=second.param_sel()
10 second.fit()
<ipython-input-104-57f0d958ed41> in param_sel(self)
69 c= self.svc_param_selection(X_train, y_train, 10)
70 print(c)
---> 71 for i in c.items():
72 value=i
73 print(value)
AttributeError: 'tuple' object has no attribute 'items'
Your value - C=({'C': 1, 'degree': 1}, {'C': 0.1, 'degree': 1}, {'C': 1, 'degree': 1})
is a tuple of dictionaries, hence it has no attribute items
.
You can iterate over the tuple with -
for item in C:
print(item)
So, fixing according to the clarification -
c_values = []
degrees = []
for item in C:
c_values.append(item['C'])
degrees.append(item['degree'])
btw - in your loop you override value in each iteration any print it outside the loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.