gpt4 book ai didi

python - 如何将分类器和参数转储到表中?

转载 作者:行者123 更新时间:2023-11-29 10:44:39 24 4
gpt4 key购买 nike

我正在尝试将分类器及其参数转储到表中,如下所示:

from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets

iris = datasets.load_iris()
X, y = iris.data, iris.target
clf = DecisionTreeClassifier().fit(X, y)

当我打印 clf 时,我得到以下内容:

DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
max_features=None, max_leaf_nodes=None,
min_impurity_split=1e-07, min_samples_leaf=1,
min_samples_split=2, min_weight_fraction_leaf=0.0,
presort=False, random_state=None, splitter='best')

如何将其转储到 .txt 中,甚至更好地转储到在列下包含此信息的表中。例如,在算法名称列下会显示C4.5等...

我尝试使用 from sklearn.externals import joblib 并执行:joblib.dump(clf, "outputfile.txt")。我会弄乱文本或非 ASCII 字符。

理想输出:table

我知道这可能有点牵强,但我的问题只是如何正确输出分类器并捕获所有所需的信息。

最佳答案

如果你想按原样加载对象/模型,那么joblib就是方法(或者pickle,但scikit建议使用joblib)。如果您想保留参数并使用它们:

from sklearn.tree import DecisionTreeClassifier
import json

dt = DecisionTreeClassifier()
# do your stuff
# ...
# you can dump the parameters to json or to any other type of storage, load them and re use them.
with open("somefile.json", "wb") as f:
json.dump(dt.get_params(), f)

# ...
# and load them...with some proper error handling...
with open("somefile.json") as f:
dt.set_params(**json.load(f))

一般来说,对于您所要求的内容,您必须做一些定制的事情。 (我也在实现一些东西来将信息保存在数据库中,以便能够重用它,但我还没有找到 joblib 的解决方法)

关于python - 如何将分类器和参数转储到表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44848294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com