函数来决定哪种分类方法与可选参数一起使用-Python
我的目标是包括一个“ mod_type”参数,该参数指示要运行的模型类型,即决策树或knn,使用kwargs让用户通过可选的关键字params“ k” for KNN和决策树的“ max_depth” 。如果用户将其传递给它们,则在初始化模型时使用它们。返回模型对象。
为此,我正在使用以下功能:
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
def my_classification(x,y,mod_type,**kwargs):
if mod_type == "dt":
if max_d in kwargs.keys():
dt = DecisionTreeClassifier(max_depth=max_d.values())
dt.fit(x,y)
return dt
else:
dt = DecisionTreeClassifier()
dt.fit(x,y)
return dt
elif mod_type == "knn":
if k in kwargs.keys():
knn = KNeighborsClassifier(k.values())
knn.fit(x,y)
return knn
else:
knn = KNeighborsClassifier()
knn.fit(x,y)
return knn
else:
print("unavailable type")
iris = load_iris()
x = pd.DataFrame(iris.data)
y = iris.target
my_classification(x,y,"dt")
了解Kwargs并不容易,但我想我现在可能有它,错误它给我的是:name error:name'max_d'未定义
。我已经尝试在功能之前创建它们,然后更改其中的功能,但它在没有任何更改的情况下打印了模型。
有人可以帮忙吗?
My goal is to include a "mod_type" param that indicates the type of model to run, either a decision tree or knn, using kwargs to let the user pass in the optional keyword params "k" for knn and "max_depth" for decision tree. If the user passes these in, when initializing the model use them as appropriate. Return the model object.
For that, I'm using below function:
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
def my_classification(x,y,mod_type,**kwargs):
if mod_type == "dt":
if max_d in kwargs.keys():
dt = DecisionTreeClassifier(max_depth=max_d.values())
dt.fit(x,y)
return dt
else:
dt = DecisionTreeClassifier()
dt.fit(x,y)
return dt
elif mod_type == "knn":
if k in kwargs.keys():
knn = KNeighborsClassifier(k.values())
knn.fit(x,y)
return knn
else:
knn = KNeighborsClassifier()
knn.fit(x,y)
return knn
else:
print("unavailable type")
iris = load_iris()
x = pd.DataFrame(iris.data)
y = iris.target
my_classification(x,y,"dt")
Understanding kwargs wasn't easy but I think I might have it now, error it's giving me is: NameError: name 'max_d' is not defined
. I've tried creating them prior the function and then changing those within but it prints the model without any alteration.
Could someone please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
字典对象中的键相当于字符串。
如果不是字符串,它会在
kwargs.keys()
列表中查找名为max_d
的对象。Keys in a dict object equate to strings.
without that being a string, it's looking for an object named
max_d
in thekwargs.keys()
list.kwargs
是一个命令,其名称为键,其值是值。这就是您可以使用它的方式:
kwargs
is a dictonary with the names of the arguments as keys, and their values as values.This is how you can use it: