<jupyter_start><jupyter_text>Exploratory Data Analysis<jupyter_code>print(titanic_train.shape)
print(titanic_test.shape)
print(titanic_result.shape)
#Combine train and test data set to cleanup and transform togather
titanic_data= pd.concat([titanic_train,titanic_test], axis=0)
PassengerId=titanic_test['PassengerId']
titanic_data.info()
titanic_data.head()
titanic_data.describe()
sb.countplot(x="Survived",data=titanic_train)
sb.countplot(x="Survived", hue="Sex", data=titanic_train)
sb.countplot(x="Survived", hue="Pclass", data=titanic_train)
titanic_train["Age"].plot.hist()
titanic_train["Fare"].plot.hist(bins=20, figsize=(10,4))
sb.countplot("Survived",hue="Embarked",data=titanic_train)<jupyter_output><empty_output><jupyter_text>Data Wrangling<jupyter_code>titanic_data.isnull().sum()
sb.boxplot(x="Pclass",y="Age", data=titanic_data)
def name_extract(word):
    return word.split(',')[1].split('.')[0].strip()
titanic_data['Salutation']=titanic_data['Name'].apply(name_extract)
titanic_data.groupby('Salutation').PassengerId.count()
def group_salutation(Salutation):
    if(Salutation=="Mr"):
        return "Mr"
    else:
        if(Salutation=="Miss"):
            return "Miss"
        else:
            if(Salutation=="Mrs"):
                return "Mrs"
            else:
                if(Salutation=="Master"):
                    return "Master"
                else:
                    return "Others"
        
titanic_data.Salutation=titanic_data.Salutation.apply(group_salutation)
#meanAge=np.mean(titanic_data.Age)
#titanic_data["Age"]=titanic_data.Age.fillna(meanAge)
missing_ages = titanic_data[titanic_data['Age'].isnull()]
# determine mean age based on Sex and Pclass
mean_ages = titanic_data.groupby(['Sex','Pclass'])['Age'].mean()

def remove_na_ages(df):
    if pd.isnull(df['Age']):
        return mean_ages[df['Sex'],df['Pclass']]
    else:
        return df['Age']

titanic_data['Age'] =titanic_data.apply(remove_na_ages, axis=1)
titanic_data.head()
#Assign value 1 for passengers with cabin
titanic_data.loc[titanic_data['Cabin'].notnull(), 'Cabin'] = 1
#Assign value 0 for passengers without cabin
titanic_data.loc[titanic_data['Cabin'].isnull(), 'Cabin'] = 0
titanic_data.head()
titanic_data=pd.get_dummies(titanic_data, columns=['Sex','Pclass','Embarked','Salutation'])
#sex=pd.get_dummies(titanic_data["Sex"],drop_first=True)
titanic_data.head()<jupyter_output><empty_output><jupyter_text>def Age_trans(age):    if(age<=5.0):        return 'Infant'    elif(age>5.0 and age<13.0):        return 'Toddler'    elif(age >=13.0 and age<18.0):        return 'Teenage'    elif(age >=18.0 and age<45.0):        return 'Adult'    else:        return 'Old' titanic_data['Age']=titanic_data['Age'].apply(Age_trans)<jupyter_code>titanic_data.shape
titanic_data.drop(['Name','Ticket','PassengerId'],axis=1,inplace=True)
#Split the records into train and test.
titanic_test=titanic_data[titanic_data.Source=='test']
titanic_train=titanic_data[titanic_data.Source=='train']
print(titanic_test.shape)
print(titanic_train.shape)
titanic_train=titanic_train.drop('Source',axis=1)
titanic_test=titanic_test.drop('Source',axis=1)
titanic_test=titanic_test.drop('Survived',axis=1)
x=titanic_train.drop(["Survived"],axis=1)

x.head()
y=titanic_train["Survived"]
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.2,random_state=22)
decision_tree=tree.DecisionTreeClassifier()
decision_tree.fit(x_train,y_train)
y_pred=decision_tree.predict(x_test)
from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_pred)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc
cv=KFold(n_splits=10)
accuracies=list()
max_attributes=len(list(titanic_train))
depth_range=range(1,max_attributes+1)
for depth in depth_range:
    fold_accuracy=[]
    tree_model=tree.DecisionTreeClassifier(max_depth=depth)
    for train_fold,valid_fold in cv.split(titanic_train):
        f_train=titanic_train.loc[train_fold]
        f_valid=titanic_train.loc[valid_fold]
        
        model=tree_model.fit(X=f_train.drop(['Survived'],axis=1),y=f_train['Survived'])
        valid_acc=model.score(X=f_valid.drop(['Survived'],axis=1),y=f_valid['Survived'])
        fold_accuracy.append(valid_acc)
        
    avg=sum(fold_accuracy)/len(fold_accuracy)
    accuracies.append(avg)
        
df=pd.DataFrame({'Max Depth': depth_range,"Average Accuracy": accuracies})  
df=df[['Max Depth','Average Accuracy']]
print(df.to_string(index=False))
y_train1=titanic_train['Survived']
x_train1=titanic_train.drop('Survived',axis=1).values
x_test1=titanic_test.values
decision_tree=tree.DecisionTreeClassifier(max_depth=4)
decision_tree.fit(x_train1,y_train1)
type(y_train.values)
y_pred=decision_tree.predict(x_test1)
submission=pd.DataFrame({"PassengerId":PassengerId, "Survived":y_pred})
submission.to_csv('submission.csv',index=False)
with open('tree1.dot','w') as f:
    f=tree.export_graphviz(decision_tree,
                          out_file=f,
                          max_depth=4,
                          impurity=True,
                          feature_names=list(titanic_train.drop(['Survived'],axis=1)),
                          class_names=['Died','Survived'],
                          rounded=True,
                          filled=True)
check_call(['dot','-Tpng','tree1.dot','-o','tree1.png'])
img=Image.open("tree1.png")
draw=ImageDraw.Draw(img)
draw.text((10,0),
         '"Title<=1.5" corresponds to "Mr."Title',
         (0,0,255)
         )
img.save('sample-out.png')
PImage("sample-out.png")
y_pred=decision_tree.predict(x_test)
from sklearn.metrics import roc_curve, auc
false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_pred)
roc_auc = auc(false_positive_rate, true_positive_rate)
roc_auc
from sklearn.tree import DecisionTreeClassifier
max_depths = np.linspace(1, len(titanic_train.columns), len(titanic_train.columns), endpoint=True)
train_results = []
test_results = []
for max_depth in max_depths:
   dt = DecisionTreeClassifier(max_depth=max_depth)
   dt.fit(x_train, y_train)
   train_pred = dt.predict(x_train)
   false_positive_rate, true_positive_rate, thresholds = roc_curve(y_train, train_pred)
   roc_auc = auc(false_positive_rate, true_positive_rate)
   # Add auc score to previous train results
   train_results.append(roc_auc)
   y_pred = dt.predict(x_test)
   false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_pred)
   roc_auc = auc(false_positive_rate, true_positive_rate)
   # Add auc score to previous test results
   test_results.append(roc_auc)
from matplotlib.legend_handler import HandlerLine2D
line1, = plt.plot(max_depths, train_results, 'b', label='Train AUC')
line2, = plt.plot(max_depths, test_results, 'r', label='Test AUC')
plt.legend(handler_map={line1: HandlerLine2D(numpoints=2)})
plt.ylabel('AUC score')
plt.xlabel('Tree depth')
plt.show()
min_samples_splits = np.linspace(0.1, 1.0, 10, endpoint=True)
train_results = []
test_results = []
for min_samples_split in min_samples_splits:
   dt = DecisionTreeClassifier(min_samples_split=min_samples_split)
   dt.fit(x_train, y_train)
   train_pred = dt.predict(x_train)
   false_positive_rate, true_positive_rate, thresholds =    roc_curve(y_train, train_pred)
   roc_auc = auc(false_positive_rate, true_positive_rate)
   train_results.append(roc_auc)
   y_pred = dt.predict(x_test)
   false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_pred)
   roc_auc = auc(false_positive_rate, true_positive_rate)
   test_results.append(roc_auc)
from matplotlib.legend_handler import HandlerLine2D
line1, = plt.plot(min_samples_splits, train_results, 'b', label='Train AUC')
line2, = plt.plot(min_samples_splits, test_results, 'r', label='Test AUC')
plt.legend(handler_map={line1: HandlerLine2D(numpoints=2)})
plt.ylabel('AUC score')
plt.xlabel('min samples split')
plt.show()
min_samples_leafs = np.linspace(0.1, 0.5, 5, endpoint=True)
train_results = []
test_results = []
for min_samples_leaf in min_samples_leafs:
   dt = DecisionTreeClassifier(min_samples_leaf=min_samples_leaf)
   dt.fit(x_train, y_train)
   train_pred = dt.predict(x_train)
   false_positive_rate, true_positive_rate, thresholds = roc_curve(y_train, train_pred)
   roc_auc = auc(false_positive_rate, true_positive_rate)
   train_results.append(roc_auc)
   y_pred = dt.predict(x_test)
   false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_pred)
   roc_auc = auc(false_positive_rate, true_positive_rate)
   test_results.append(roc_auc)
from matplotlib.legend_handler import HandlerLine2D
line1, = plt.plot(min_samples_leafs, train_results, 'b', label='Train AUC')
line2, = plt.plot(min_samples_leafs, test_results, 'r',label='Test AUC')
plt.legend(handler_map={line1: HandlerLine2D(numpoints=2)})
plt.ylabel('AUC score')
plt.xlabel('min samples leaf')
plt.show()
max_features = list(range(1,titanic_train.shape[1]))
train_results = []
test_results = []
for max_feature in max_features:
   dt = DecisionTreeClassifier(max_features=max_feature)
   dt.fit(x_train, y_train)
   train_pred = dt.predict(x_train)
   false_positive_rate, true_positive_rate, thresholds = roc_curve(y_train, train_pred)
   roc_auc = auc(false_positive_rate, true_positive_rate)
   train_results.append(roc_auc)
   y_pred = dt.predict(x_test)
   false_positive_rate, true_positive_rate, thresholds = roc_curve(y_test, y_pred)
   roc_auc = auc(false_positive_rate, true_positive_rate)
   test_results.append(roc_auc)
from matplotlib.legend_handler import HandlerLine2D
line1, = plt.plot(max_features, train_results, 'b', label='Train AUC')
line2, = plt.plot(max_features, test_results, 'r', label='Test AUC')
plt.legend(handler_map={line1: HandlerLine2D(numpoints=2)})
plt.ylabel('AUC score')
plt.xlabel('max features')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1319269203593419, 'fraction_numerical': 0.011002321590794388, 'mean_word_length': 5.875780707841777, 'pattern_counts': {'":': 3, '<': 18, '<?xml version=': 0, '>': 16, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '453197', 'license': 'FTL', 'n_tokens_mistral': 3849, 'n_tokens_neox': 3637, 'n_words': 520, 'path': 'notebooks/Decision Tree.ipynb', 'repo_name': 'amrendra1002/ML-DL', 'size': '9907'}