[英]Passing Values between functions using **kwargs
目前,我有一個函數run_prob ,它接受我的模型並返回y_prob。 它運作完美。 但是,在下面,我將另外兩個函數pred_prob和cal_prob放在一起,並且我希望這兩個函數一起工作以將我的pred_prob,count,true_prob打印到一個新的數據框中 。
我知道def pred_prob_和def cal_prob_不起作用。 但是,功能之間的代碼是可操作的。
我遇到的問題是我不確定如何設置run_prob_cv來傳遞我內置在其中的其他模型(請參見下文)。
def run_prob_cv(X, y, clf_class, **kwargs):
kf = KFold(len(y), n_folds=5, shuffle=True)
y_prob = np.zeros((len(y),2))
for train_index, test_index in kf:
X_train, X_test = X[train_index], X[test_index]
y_train = y[train_index]
clf = clf_class(**kwargs)
clf.fit(X_train,y_train)
# Predict probabilities, not classes
y_prob[test_index] = clf.predict_proba(X_test)
return y_prob
我建立的模型及其名稱為:KNN,SVM,RF和GNB
我當時以為這些函數會像這樣流動:
def pred_prob_(y_prob, run_prob_cv(X, y, MODEL, n_estimators, **kwargs):
# Use 10 estimators so predictions are all multiples of 0.1
pred_prob = run_prob_cv(X, y, MODEL, n_estimators=10)
pred_churn = pred_prob[:,1]
is_churn = y == 1
# Number of times a predicted probability is assigned to an observation
counts = pd.value_counts(pred_churn)
return counts
def cal_prob_(counts):
# calculate true probabilities
true_prob = {}
for prob in counts.index:
true_prob[prob] = np.mean(is_churn[pred_churn == prob])
true_prob = pd.Series(true_prob)
# pandas-fu
counts = pd.concat([counts,true_prob], axis=1).reset_index()
counts.columns = ['pred_prob', 'count', 'true_prob']
#counts
counts.sort_index(by=['pred_prob','true_prob'], ascending=[False, True])
最終目標是能夠采用我的四個模型並像這樣運行它們,而不必生成重復的代碼:
pred_prob_(y_prob, run_prob_cv(X, y, KNN, n_estimators, **kwargs)
pred_prob_(y_prob, run_prob_cv(X, y, RF, n_estimators, **kwargs)
pred_prob_(y_prob, run_prob_cv(X, y, SVM, n_estimators, **kwargs)
pred_prob_(y_prob, run_prob_cv(X, y, GNB, n_estimators, **kwargs)
def pred_prob(X, y, MODEL):
pred_prob = run_prob_cv(X, y, MODEL)
pred_churn = pred_prob[:,1]
is_churn = y == 1
# Number of times a predicted probability is assigned to an observation
counts = pd.value_counts(pred_churn)
# calculate true probabilities
true_prob = {}
for prob in counts.index:
true_prob[prob] = np.mean(is_churn[pred_churn == prob])
true_prob = pd.Series(true_prob)
# pandas-fu
counts = pd.concat([counts,true_prob], axis=1).reset_index()
counts.columns = ['pred_prob', 'count', 'true_prob']
#counts
print counts.sort_index(by=['pred_prob','true_prob'], ascending=[False, True])
pred_prob(X, y, KNN)
pred_prob(X, y, RF)
pred_prob(X, y, GNB)
pred_prob(X, y, SVC)
**kwargs
語法用於收集零個或多個命名參數 ,並將它們作為dict
管理。 類似地, *posargs
語法對未命名的位置參數執行相同的操作,從而使它們在函數中可以作為tuple
。
例如,您可以調用以下函數:
foo(1, 2, 3, namedparam1=4, namedparam2=5)
如果願意,可以顯式接收命名參數:
def foo(x, y, z, namedparam1, namedparam2):
或者,您可以使用**
它們“組合在一起”:
def foo(x, y, z, **kwargs):
在這種情況下,您可以檢查,編輯或修改結果字典:
if 'zanzibar' not in kwargs:
kwargs['zanzibar'] = (-6.167904, 39.228628)
您還可以使用完全相同的 **
語法將dict作為命名參數傳遞給另一個函數:
bar(z, y, x, **kwargs)
如果bar
具有與kwargs
的鍵匹配的顯式命名參數,則它們將在dict中接收值。 如果bar
沒有與dict中的鍵匹配的命名參數,則如果bar
具有**
catch-all字典,則值將轉到那里,否則將引發異常。
例如:
def bar(z, y, x, zanzibar, **kwargs):
在此示例中, zanzibar
現在是必需參數。 您可以通過** kwargs傳遞它,也可以在通話中將其拼寫出來,但它必須以某種方式出現。 ** kwargs中的其他命名參數仍然是可選的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.