python中变量中的函数参数名称

时间:2018-11-16 14:43:34

标签: python scikit-learn

我正在使用sklearn来训练不同的模型。我想传递sklearn的决策树分类器,相同参数的不同值并绘制图形。我想对许多此类参数执行此操作。因此,我想创建一个通用函数来处理所有参数及其值。

我的问题是,有一种方法可以将参数名称(而不是值)分配给变量并将其传递给我的函数。

例如-决策树采用max_depthmin_samples_leaf等自变量。我想一次尝试两个参数的不同值,然后分别绘制max_depthmin_samples_leaf的结果。

2 个答案:

答案 0 :(得分:2)

使用字典并将其与**一起传递。

kwargs = {
    "max_depth": value,
    "min_samples_leaf": value,
}
fun(**kwargs)

答案 1 :(得分:0)

该解决方案不是很“ Pythonic”,但是很容易遵循。您可以只在循环或嵌套循环或类似的函数中调用该函数。

dt = DecisionTreeClassifier(criterion='entropy', min_samples_leaf=150, min_samples_split=100)

是使用决策树的标准调用,只需循环使用要使用的值并替换min_samples_leafmin_samples_split

from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, roc_curve, auc
from sklearn.model_selection import train_test_split

min_samples_leafs = [50, 100, 150]
min_samples_splits =[50, 100, 150]

for sample_leafs in min_samples_leafs:
    for sample_splits in min_samples_splits:

        dt = DecisionTreeClassifier(criterion='entropy', min_samples_leaf=sample_leafs, min_samples_split=sample_splits)

        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)

        dt = dt.fit(X_train, y_train)
        y_pred_train = dt.predict(X_train)
        y_pred_test = dt.predict(X_test)


        print("Training Accuracy: %.5f" %accuracy_score(y_train, y_pred_train))
        print("Test Accuracy: %.5f" %accuracy_score(y_test, y_pred_test))
        print('sample_leafs: ', sample_leafs)
        print('sample_leafs: ', sample_splits)
        print('\n')

输出:

Training Accuracy: 0.96689
Test Accuracy: 0.96348
sample_leafs:  50
sample_leafs:  50


Training Accuracy: 0.96689
Test Accuracy: 0.96348
sample_leafs:  50
sample_leafs:  100


Training Accuracy: 0.96509
Test Accuracy: 0.96293
sample_leafs:  50
sample_leafs:  150


Training Accuracy: 0.96313
Test Accuracy: 0.96256
sample_leafs:  100
sample_leafs:  50


Training Accuracy: 0.96313
Test Accuracy: 0.96256
sample_leafs:  100
sample_leafs:  100


Training Accuracy: 0.96313
Test Accuracy: 0.96256
sample_leafs:  100
sample_leafs:  150


Training Accuracy: 0.96188
Test Accuracy: 0.96037
sample_leafs:  150
sample_leafs:  50


Training Accuracy: 0.96188
Test Accuracy: 0.96037
sample_leafs:  150
sample_leafs:  100


Training Accuracy: 0.96188
Test Accuracy: 0.96037
sample_leafs:  150
sample_leafs:  150

您可以通过传递这样的列表来实现此功能

def do_decision_tree_stuff(min_samples_leafs, min_samples_splits):

您这样调用函数

 do_decision_tree_stuff([50, 100, 150], [50, 100, 150])