参数中的随机森林分类器对象

Pau*_*eau 3 python random arguments classification

我目前正在研究随机森林分类器,使用 gridsearch 来获得最佳参数

所以当我得到我的参数时,它们在我的 var 中:

params = {'bootstrap': 'True', 
          'criterion': 'entropy', 
          'max_depth': 'None', 
          'max_features': '3', 
          'min_samples_leaf': '4', 
          'min_samples_split': '3'}
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情:

clf = RandomForestClassifier(params)

但是这里params代替了n_estimators所以我有一些错误,例如:

ValueError: n_estimators must be an integer, got <class 'dict'>.
Run Code Online (Sandbox Code Playgroud)

Mik*_*tty 9

您需要解压缩函数调用的参数

clf = RandomForestClassifier(**params) 
Run Code Online (Sandbox Code Playgroud)

让我向您展示使用 dict 作为具有默认参数的函数的函数参数的各种方法的结果。函数调用后的注释就是打印的结果。

def foo(bar=None, baz=None):
    print(bar, baz)

params = { "bar": "Hello", "baz": "World"}

# pass params as the first parameter
foo(params) # {'baz': 'World', 'bar': 'Hello'} None

# pass keys of params as parameters (order is out of our hands)
foo(*params) # baz bar

# unpacks params with key=value as parameters
foo(**params) # Hello World
Run Code Online (Sandbox Code Playgroud)

我注意到你总是在你的值中使用字符串表示,即使对于Noneor True- 如果函数期望实际None或布尔值而不是字符串,你可能会遇到麻烦。最好检查 API 哪些值是有效的。