简体   繁体   English

使用** kwargs在函数之间传递值

[英]Passing Values between functions using **kwargs

I currently have one function run_prob that takes my models and returns the y_prob. 目前,我有一个函数run_prob ,它接受我的模型并返回y_prob。 It works perfectly. 它运作完美。 However, below, I have put together another two functions pred_prob and cal_prob , and I would like these two functions to work together to print out my pred_prob,count,true_prob into a new dataframe. 但是,在下面,我将另外两个函数pred_probcal_prob放在一起,并且我希望这两个函数一起工作以将我的pred_prob,count,true_prob打印到一个新的数据框中

I know that def pred_prob_ and def cal_prob_ do not work. 我知道def pred_prob_def cal_prob_不起作用。 However, the code between the functions is operational. 但是,功能之间的代码是可操作的。

The issue that I'm having is that I'm not sure how to set run_prob_cv to pass my other models that I have built into them (See below). 我遇到的问题是我不确定如何设置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

The models that I have built and their names are: KNN, SVM, RF, and GNB 我建立的模型及其名称为:KNN,SVM,RF和GNB

I was thinking that the functions would flow something like this: 我当时以为这些函数会像这样流动:

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])

The end goal is to be able to take my four models and run them like this without having to generate repetitive code: 最终目标是能够采用我的四个模型并像这样运行它们,而不必生成重复的代码:

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)

The **kwargs syntax is used to collect zero or more named parameters and manage them as a dict . **kwargs语法用于收集零个或多个命名参数 ,并将它们作为dict管理。 Similarly, the *posargs syntax does the same things with unnamed positional parameters , making them available within the function as a tuple . 类似地, *posargs语法对未命名的位置参数执行相同的操作,从而使它们在函数中可以作为tuple

You can, for example, call a function like this: 例如,您可以调用以下函数:

foo(1, 2, 3, namedparam1=4, namedparam2=5)

If you like, you can explicitly receive the named parameters: 如果愿意,可以显式接收命名参数:

def foo(x, y, z, namedparam1, namedparam2):

Or you can "glob them together" using ** : 或者,您可以使用**它们“组合在一起”:

def foo(x, y, z, **kwargs):

In this case, you can inspect, edit, or modify the resulting dict: 在这种情况下,您可以检查,编辑或修改结果字典:

    if 'zanzibar' not in kwargs:
        kwargs['zanzibar'] = (-6.167904, 39.228628)

You can also pass the dict to another function, as named arguments, using the exact same ** syntax: 您还可以使用完全相同的 **语法将dict作为命名参数传递给另一个函数:

    bar(z, y, x, **kwargs)

If bar has explicitly named parameters matching the keys in kwargs , they will receive the values in the dict. 如果bar具有与kwargs的键匹配的显式命名参数,则它们将在dict中接收值。 If bar does not have named parameters matching the keys in dict, then if bar has a ** catch-all dictionary, the values will go there, otherwise an exception will be thrown. 如果bar 没有与dict中的键匹配的命名参数,则如果bar具有** catch-all字典,则值将转到那里,否则将引发异常。

For example: 例如:

def bar(z, y, x, zanzibar, **kwargs):

In this example, zanzibar is now a required parameter. 在此示例中, zanzibar现在是必需参数。 You can pass it via **kwargs or by spelling it out in your call, but it must be present somehow. 您可以通过** kwargs传递它,也可以在通话中将其拼写出来,但它必须以某种方式出现。 Other named parameters in **kwargs are still optional. ** kwargs中的其他命名参数仍然是可选的。

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

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