[英]Function parameter name in variable in python
I am using sklearn
to train different models. 我正在使用
sklearn
训练不同的模型。 I want to pass the decision tree classifier of sklearn
, different values of the same parameter and plot a graph. 我想通过
sklearn
决策树分类器(相同参数的不同值)并绘制图形。 I want to do this for many such parameters. 我想对许多此类参数执行此操作。 So, I want to create a general function which can handle all the parameters and their values.
因此,我想创建一个通用函数来处理所有参数及其值。
My question is that is there a way to assign the parameter name (not value) to a variable and pass it to my function. 我的问题是,有没有一种方法可以将参数名称(而不是值)分配给变量并将其传递给我的函数。
Eg.- Decision tree takes the max_depth
, min_samples_leaf
etc. arguments. 例如-决策树采用
max_depth
, min_samples_leaf
等参数。 I want to try different values of both parameters one at a time and plot results for both max_depth
and min_samples_leaf
separately. 我想一次尝试两个参数的不同值,并分别绘制
max_depth
和min_samples_leaf
结果。
Use a dictionary and pass it with **
. 使用字典并将其与
**
传递。
kwargs = {
"max_depth": value,
"min_samples_leaf": value,
}
fun(**kwargs)
This solution isn't very "Pythonic", but it's easy to follow. 这个解决方案不是很“ Pythonic”,但是很容易遵循。 You could just call the function in a loop or nested loop or something similar.
您可以只在循环或嵌套循环或类似的函数中调用该函数。
dt = DecisionTreeClassifier(criterion='entropy', min_samples_leaf=150, min_samples_split=100)
Is the standard call to use a decision tree, just loop over the values you want to use and replace min_samples_leaf
and min_samples_split
是使用决策树的标准调用,只需循环使用要使用的值并替换
min_samples_leaf
和min_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')
Output: 输出:
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
You can make this a function by passing the lists like so 您可以通过传递列表来使其成为一个函数,如下所示
def do_decision_tree_stuff(min_samples_leafs, min_samples_splits):
You call the function like this 你这样调用函数
do_decision_tree_stuff([50, 100, 150], [50, 100, 150])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.