<jupyter_start><jupyter_text>Cleaned the Data and removed all the continious column from itThe data used here is titanic dataset<jupyter_code>X = titanic 
X = X.values #converting X into np array<jupyter_output><empty_output><jupyter_text>set the column array for us as the last column is Survived which is output and hence we dont count it in features<jupyter_code>column = ["Pclass", "Sex","SibSp", "Parch"]
def gainRatio(X,column,index):
    y = [X[X[:,index]==k] for k in np.unique(X[:,index])] #split the data based on best filt index/features
    y=np.asarray(y)                                       #saved the multidimensional array as nparray
    n,c = np.unique(X[:,4],return_counts=True)            #for entropy of parent node
    eParent = calcEntro(n,c)
    infoCh =0
    splitInfo=0
    for i in range(y.shape[0]):
        n1 ,c1 = np.unique(y[i][:,4],return_counts=True)   
        temp = calcEntro(n1,c1)                            #entropy of each child node
        xxx = (np.sum(c1)/np.sum(c))
        infoCh = infoCh + (xxx*temp)                    #infoReq for child node
        splitInfo = splitInfo + (-1*xxx)*np.log2(xxx)    #split info for child node 
    iG = eParent - infoCh                               #information gain infoParent - infoChild
   # print("eparent",eParent,"infoG",infoG)
    if(splitInfo == 0):                                 #division by 0 check
    #    print(iG," ",splitInfo)
        return 0 
    gainRatio = (iG/splitInfo)                           #calc Gain Ratio
    return gainRatio
def findBestFit(X,column):
    maxx=-99
    f = -1
    for i in range(len(column)):        #iterate over all the rest columns for best feature
        t = gainRatio(X,column,i)
        if(maxx < t):
          #  print(" ")
            maxx = t
            f = i
    return f,t
def calcEntro(n,c):
    totalSum = np.sum(c)
    ans =0
    for i in range(len(n)):
        m = c[i]/totalSum
        ans = ans + (-1*m)*(np.log2(m))         #entropy formula
    return ans
def printInfo(X,column,level,entropy=0,gainRatio=0,x="NA"):
    if(x=="NA"):
        print("Level",level)
        n,c = np.unique(X[:,4],return_counts=True)
     #   print(len(n))
        for i in range(len(n)):
                print("Count of",n[i],c[i])
        print("Entropy",entropy)
        print("Reached leaf Node")
    else:
        print("Level",level)
        n,c = np.unique(X[:,4],return_counts=True)
    # print(len(n))
        for i in range(len(n)):
                print("Count of",n[i],c[i])
        print("Entropy",entropy)
        print("splitting Feature based on",x," with gain ratio",gainRatio)
def decisionTree(X,column,level=0):
    if(len(np.unique(X[:,4]))==1):    #base case if node is pure 
        printInfo(X,column,level)
        print(" ")
        return
    if(len(column)==0):             #base case if no feature left to split
        n,c = np.unique(X[:,4],return_counts=True)
        entropy = calcEntro(n,c)
        printInfo(X,column,level,entropy)
        print(" ")
        return
    index,gainRatio= findBestFit(X,column)   #find the best fit feature and its gainRatio
    n,c = np.unique(X[:,4],return_counts=True)
    entropy = calcEntro(n,c)                        
    name = column[index]
    printInfo(X,column,level,entropy,gainRatio,name)
    print(" ")
    level += 1                      #inc the level
    y = [X[X[:,index]==k] for k in np.unique(X[:,index])]     #split the data on basis of feature
    y1=np.asarray(y)
    del column[index]
    
    for i in range(y1.shape[0]):
        #y1 = np.delete(y1[i], np.s_[index:index+1], axis=1)
        decisionTree(y1[i],column,level)
    return
column = ["Pclass", "Sex","SibSp", "Parch"]
decisionTree(X,column,0)
column<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13659314376827, 'fraction_numerical': 0.01089556205155461, 'mean_word_length': 2.322153574580759, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '380136', 'license': 'MIT', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1264, 'n_words': 308, 'path': '11. DesicionTree Implementation/titanicDt.ipynb', 'repo_name': 'yaminikonka/ML-Notebook', 'size': '3763'}