0
$\begingroup$

During the split function of the decision tree, I am getting an assertion error stating:

Error > Assert: type(C) == dict

I couldn't find any errors in the code up until now. Could you please correct it if there is any problem with it?

My code:

def split(X,Y,i):

    def __find_best_split(self):
                best_split = {}
                for column in self.X:     
                information_gain,split = 
    self.__find_best_split_for_column(column)
                    if split: continue
                    if not best_split or best_split["information_gain"] 
       <information_gain:
                       best_split = 
    {"split":split,"col":column,"information_gain":information_gain}

                    C= (best_split)     

                return C
$\endgroup$
1
$\begingroup$
def __find_best_split(X, Y, i):
best_split = {}
for column in X:     
    information_gain,split = (2,3)#self.__find_best_split_for_column(column)
    if not split: continue
    if not best_split or best_split["information_gain"] <information_gain:
        best_split = {"split":split,"col":column,"information_gain":information_gain}
C= (best_split)
return C

I ran this code in python 3.7 and it gave me the correct output: {'split': 3, 'col': 'a', 'information_gain': 2}. Also as pointed out by Ben Reiniger, it should be if not split: continue rather than if split: continue. In your case based on the indentation, C should never get defined or if indented properly, it should give empty dictionary. In both the scenario, getting error assert type(C) == dict does not makes sense.

| improve this answer | |
$\endgroup$
0
$\begingroup$

A little more context (and a more cleanly formatted code block) would probably be helpful, but shouldn't the line
if split: continue
be instead the opposite,
if not split: continue?

It seems to me that the continue to go to the next column in the for loop should only be happening if __find_best_split_for_column fails (in case at a particular node the column is constant). As written, the continue will take effect nearly every time, making best_split and therefore C never get redefined. Then again, this should just mean best_split remains an empty dictionary, so failing the type check seems odd. (That definition of C is supposed to be happening outside the loop, right?)

| improve this answer | |
$\endgroup$
  • $\begingroup$ Hi @Ben, i tried changing the code as per your suggestion, but it still gives the same error. $\endgroup$ – VishwaV Sep 8 '19 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.