13

I found out that sklearn.utils.Bunch and dict work more or less the same. Like if there is a dict object say

dict_1 = {"a":1, "b":2}

and a bunch object say bunch

bunch_1 = Bunch(a=1, b=2)

both have the same set of behaviour.

yoonghm
  • 4,198
  • 1
  • 32
  • 48
TheLastCoder
  • 610
  • 2
  • 6
  • 15
  • 2
    Since this isn’t a standard built in class, you should really clarify where this comes from. – deceze May 24 '19 at 05:21
  • from sklearn module, the object returned by datasets.load_iris() was of type Bunch – TheLastCoder May 24 '19 at 05:29
  • 2
    I won’t recommend using Bunch, since this project is not maintained any longer (last released in dec. 2011). See also https://github.com/dsc/bunch/issues – Laurent LAPORTE May 24 '19 at 05:36
  • is there anything that is similar to bunch? where i can get attribute style access to dict values? – DataCruncher May 24 '19 at 06:07
  • 1
    @fireblaze The distinction exists explicitly, so you can store data in `d['keys']` and use methods of the dict as `d.keys()`, otherwise this would be a naming clash. Even if most of the time you may not encounter this, it’s a fundamental problem classes like `Bunch` need to deal with. – deceze May 24 '19 at 06:11
  • 2
    @Abhilash Prakash Python hide/abandon dict's key as attribute, plz see https://stackoverflow.com/questions/17761202/is-python-dict-an-object – Tommy Levi May 24 '19 at 07:24

2 Answers2

16

Bunch is a subclass of the Dict class and supports all the methods as dict does. In addition, it allows you to use the keys as attributes.

b = Bunch(a=1, b=2)
>>> b['b']
2
>>> b.b
2

Read more here

DataCruncher
  • 850
  • 7
  • 11
2

Bunch is just like dictionary but it supports attribute type access.

  1. Data Type
  • Dictionary is in-built type, whereas Bunch is from bunchclass package. bunchclass.

  • Bunch works fine in python 2, but in python 3 it does not work! You import Bunch from sklearn.utils

    from bunchclass import Bunch # python 2

    from sklearn.utils import Bunch # python 3

  1. 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.
    
  2. Accessing the value of the key This is the main difference between the two.

    d1['a']
    b1['a']
    b1.a
    

In a Bunch, you can access the attributes using dot notations. In a dict this is not possible.

Similarities Both Dictionary and bunch can contain values of any data type. But keys must be mutable. There can be nested dictionaries and nested bunches.

Utilities of Bunch

  • Bunch() is useful serialization to json.
  • Bunch() is used to load data in sklearn. Here normally a bunch contains various attributes of various types(list, numpy array, etc.,).

More on Bunch as with any other object use dir(Bunch object) to know more. Refer to this link to know more about bunch:Bunch

In case your aim is to convert a bunch into dataframe, you can refer this link https://github.com/viswanathanc/basic_python/blob/master/sklearn.utils.bunch%20to%20pandas%20Dataframe.ipynb

E. Ducateme
  • 4,028
  • 2
  • 20
  • 30