[英]Extracting the values from dictionary with tuple
我正在研究支持向量機,我使用gridsearchcv提取了多項式、線性和 RBF 的最佳參數。 我想將 C 和度數值分開,以便我可以為擬合函數調用數據集的最佳參數,在那里我可以使用調整后的參數計算精度。
帶有元組的字典是
({'C': 1, 'degree': 1}, {'C': 0.1, 'degree': 1}, {'C': 1, 'degree': 1})
我嘗試使用
for i in c.items():
value=i
print(value)
但得到錯誤:AttributeError: 'tuple' object has no attribute 'items'
我的代碼的進度是:
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))
輸出:
[*] 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' 對象沒有屬性 'items'
你的價值 - C=({'C': 1, 'degree': 1}, {'C': 0.1, 'degree': 1}, {'C': 1, 'degree': 1})
是一個字典元組,因此它沒有屬性items
。
您可以使用 - 迭代元組
for item in C:
print(item)
因此,根據澄清進行修復 -
c_values = []
degrees = []
for item in C:
c_values.append(item['C'])
degrees.append(item['degree'])
順便說一句 - 在你的循環中,你在每次迭代中覆蓋值,任何在循環外打印它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.