我发现sklearn.utils.Bunch
和dict
基本上是一样的。例如,如果有一个dict
对象,比如说
dict_1 = {"a":1, "b":2}
并且一个bunch对象称为bunch
bunch_1 = Bunch(a=1, b=2)
两者具有相同的行为集。
我发现sklearn.utils.Bunch
和dict
基本上是一样的。例如,如果有一个dict
对象,比如说
dict_1 = {"a":1, "b":2}
并且一个bunch对象称为bunch
bunch_1 = Bunch(a=1, b=2)
两者具有相同的行为集。
Initialization
Initialization of bunch does not require {}
, but an explicit function with attributes of the elements you required to be in the bunch.
d1 = {'a':1, 'b':'one', 'c':[1,2,3], 4:'d'}`
b1 = Bunch(a=1, b='one', c=[1,2,3]) # Also note: here the keys of Bunch are
# attributes of the class. They must be
# mutable and also follow the
# conventions for variables.
Accessing the value of the key This is the main difference between the two.
d1['a']
b1['a']
b1.a
dir(Bunch对象)
来了解更多信息。
请参阅此链接以了解有关bunch的更多信息:Bunch
如果您的目标是将一堆数据转换为数据框,您可以参考此链接https://github.com/viswanathanc/basic_python/blob/master/sklearn.utils.bunch%20to%20pandas%20Dataframe.ipynb。
d['keys']
中,并使用字典的方法作为d.keys()
,否则这将导致命名冲突。即使大多数情况下您可能不会遇到这种情况,但像Bunch
这样的类需要处理这个根本性问题。 - deceze