<jupyter_start><jupyter_text>KAGGLE : TITANIC SURVIVAL PROBLEM learning goals:- How to pre-process data?- dropping not useful feature- filling the missing values(DAta imputation)<jupyter_code>import pandas as pd
import numpy as np
data=pd.read_csv("./titanic/train.csv")
data.head(n=10)
data.info()
columns_to_drop=["PassengerId","Name","Ticket","Cabin","Embarked"]
data_clean=data.drop(columns=columns_to_drop,axis=0)
data_clean.head()
from sklearn.preprocessing import LabelEncoder

le=LabelEncoder()
data_clean["Sex"]=le.fit_transform(data_clean["Sex"])
data_clean.head(n=10)
data_clean.info()
#data_clean=data_clean.fillna?
#data_clean=data_clean.fillna
data_clean=data_clean.fillna(data_clean["Age"].mean())
data_clean.info()
data_clean.head(n=10)
input_cols=["Pclass","Age","Sex","SibSp","Parch","Fare"]
output_cols=["Survived"]

X=data_clean[input_cols]
Y=data_clean[output_cols]
X.head()
Y.head()<jupyter_output><empty_output><jupyter_text>Def Function for entropy and information gain<jupyter_code>def entropy(col):
    counts=np.unique(col,return_counts=True)
    N=float(col.shape[0])
    entropy=0.0
    
    for ix in counts[1]:
        p=ix/N
        entropy+=(-1.0*p)*np.log2(p)
    
    return entropy
        
col=np.array([1,1,1,0,0,0])
entropy(col)
def divide_data(xdata,fkey,fval):
    x_left=pd.DataFrame([],columns=xdata.columns)
    x_right=pd.DataFrame([],columns=xdata.columns)
    
    for ix in range(xdata.shape[0]):
        val=xdata[fkey].loc[ix]
        if val> fval:
            x_right=x_right.append(xdata.loc[ix])
        else:
            x_left=x_left.append(xdata.loc[ix])
            
    return x_left,x_right
l,r=divide_data(data_clean[:10],"Sex",0.5)
print(l)
print(r)
def information_gain(x_test,fkey,fval):
    left,right=divide_data(x_test,fkey,fval)
    
    # % of left and right
    l=float(left.shape[0])/x_test.shape[0]
    r=float(right.shape[0])/x_test.shape[0]
    
    # if all entries go to one side then info gain is minimum
    if left.shape[0]==0 or right.shape[0]==0:
        return -100000
    
    i_gain=entropy(x_test.Survived)-(l*(entropy(left.Survived))+r*(entropy(right.Survived)))
    
    return i_gain
# test our function
for fx in X.columns:
    print(fx)
    print(information_gain(data_clean,fx,data_clean[fx].mean()))
class DecisionTree:
    
    def __init__(self,depth=0,max_depth=5):
        self.left=None
        self.right=None
        self.fkey=None
        self.fval=None
        self.max_depth=max_depth
        self.depth=depth
        self.target=None
        
    def train(self,x_train):
        features=["Pclass","Age","Sex","SibSp","Parch","Fare"]
        info_gains=[information_gain(x_train,fx,x_train[fx].mean()) for fx in features]
        
        self.fkey=features[np.argmax(info_gains)]
        self.fval=x_train[self.fkey].mean()
        
        data_left,data_right=divide_data(x_train,self.fkey,self.fval)
        data_left=data_left.reset_index(drop=True)
        data_right=data_right.reset_index(drop=True)
        print("Making tree Features is",self.fkey)
        
        if data_left.shape[0]==0 or data_right.shape[0]==0:
            if x_train.Survived.mean() >= 0.5:
                self.target="Survived"
            else:
                self.target="Dead"
            return
                
        if self.depth >= self.max_depth:
            if x_train.Survived.mean()>=0.5:
                self.target="Survived"
            else:
                self.target="Dead"
            return
                
        # recursion
        
        self.left=DecisionTree(depth=self.depth+1,max_depth=self.max_depth)
        self.left.train(data_left)
        
        self.right=DecisionTree(depth=self.depth+1,max_depth=self.max_depth)
        self.right.train(data_right)
        
        
        if x_train.Survived.mean() >= 0.5:
            self.target="Survived"
        else:
            self.target="Dead"
        return
    
    def predict(self,test):
        if test[self.fkey]>self.fval:
            if self.right is None:
                return self.target
            return self.right.predict(test)
        else:
            if self.left is None:
                return self.target
            return self.left.predict(test)
        
        
d=DecisionTree()
d.train(data_clean)
features=["Pclass","Age","Sex","SibSp","Parch","Fare"]
info_gains=[information_gain(data_clean,fx,data_clean[fx].mean()) for fx in features]
print(info_gains)
p=features[np.argmax(info_gains)]
print(p)
print(data_clean[p].mean())
test_data=pd.read_csv("./titanic/test.csv")
test_data.head()
test_cleaned=test_data.drop(columns=columns_to_drop,axis=0)
test_cleaned["Sex"]=le.fit_transform(test_cleaned["Sex"])
test_cleaned.info()

#from sklearn.preprocessing import Imputer 
#im=Imputer()
test_cleaned["Age"]=test_cleaned["Age"].fillna(test_cleaned["Age"].mean())
test_cleaned["Fare"]=test_cleaned["Fare"].fillna(test_cleaned["Fare"].mean())
test_clean=test_cleaned
test_cleaned.info()
y_pred=[]

for ix in range(test_cleaned.shape[0]):
    y_pred.append(d.predict(test_cleaned.loc[ix]))
print(y_pred)
le=LabelEncoder()
ypredd=le.fit_transform(y_pred)
passengersid=test_data.PassengerId.values
print(passengersid)
sub=[passengersid,ypredd]
submission=np.matrix(sub)
print(submission)
result=pd.DataFrame(ypredd,columns=["Survived"])
result.index.name="PassengerId"
result.index+=892

result.to_csv("submission.csv")
#### using naive bayes ###
ytrain=data_clean.Survived.values
xtrain=data_clean.values
Xtrain=xtrain[:,1:]
print(Xtrain)
from sklearn.naive_bayes import MultinomialNB
mnb=MultinomialNB()
mnb.fit(Xtrain,ytrain)
sol=mnb.predict(test_cleaned)
print(sol)
ans=pd.DataFrame(sol,columns=["Survived"])
ans.index.name="PassengerId"
ans.index+=892
ans.to_csv("Answer.csv")<jupyter_output><empty_output><jupyter_text>Using Sklern<jupyter_code>from sklearn.tree import DecisionTreeClassifier
sk_tree=DecisionTreeClassifier(criterion='entropy',max_depth=5)
sk_tree.fit(data_clean[features],data_clean["Survived"])
ans=sk_tree.predict(test_clean[features])
print(ans)
ans=pd.DataFrame(ans,columns=["Survived"])
ans.index.name="PassengerId"
ans.index+=892
ans.to_csv("Answer1.csv")

sk_tree1=DecisionTreeClassifier(max_depth=5)
sk_tree1.fit(data_clean[features],data_clean["Survived"])
ans1=sk_tree1.predict(test_cleaned[features])
ans=pd.DataFrame(ans1,columns=["Survived"])
ans.index.name="PassengerId"
ans.index+=892
ans.to_csv("Answer2.csv")<jupyter_output><empty_output><jupyter_text>Visualize a Decision tree<jupyter_code>import pydotplus

from sklearn.externals.six import StringIO
from IPython.display import Image
from sklearn.tree import export_graphviz
dot_data=StringIO()
export_graphviz(sk_tree,out_file=dot_data,filled=True,rounded=True)

graph=pydotplus.graph_from_dot_data(dot_data.getvalue())
Image(graph.create_png())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14135973212985878, 'fraction_numerical': 0.011209783083418256, 'mean_word_length': 4.169300225733634, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '346306', 'license': 'MIT', 'n_tokens_mistral': 2577, 'n_tokens_neox': 2511, 'n_words': 313, 'path': 'MachineLearning/DecisionTree/DecisionTree.ipynb', 'repo_name': 'pulkit10251/cbData-Science-Master', 'size': '6869'}