<jupyter_start><jupyter_text>Assignment 2: **Machine learning with tree based models**  In this assignment, you will work on the **Titanic** dataset and use machine learning to create a model that predicts which passengers survived the **Titanic** shipwreck.  --- About the dataset:---* The column named  `Survived` is the label and the remaining columns are features. * The features can be described as given below:              Variable      Definition                   pclass      Ticket class	              SibSp      Number of siblings / spouses aboard the Titanic              Parch      Number of parents / children aboard the Titanic              Ticket      Ticket number              Embarked      Port of Embarkation: C = Cherbourg, Q = Queenstown, S = Southampton       	 --- Instructions---* Apply suitable data pre-processing techniques, if needed. * Implement a few classifiers to create your model and compare the performance metrics by plotting the curves like roc_auc, confusion matrix, etc.<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Load the file as a dataframe 
from google.colab import drive 
drive.mount('/content/drive')
titanic_data = pd.read_csv("/content/drive/My Drive/Colab Notebooks/titanic.csv")
df = titanic_data
df.head()
titanic_data.shape
print(titanic_data.isna().sum())<jupyter_output>PassengerId      0
Survived         0
Pclass           0
Name             0
Sex              0
Age            177
SibSp            0
Parch            0
Ticket           0
Fare             0
Cabin          687
Embarked         2
dtype: int64<jupyter_text>Let's clean the dataset<jupyter_code>df = df.drop(['PassengerId','Name','Ticket','Cabin'],axis=1)<jupyter_output><empty_output><jupyter_text>Let's check the distribution of this column to replace with na values<jupyter_code>plt.hist(df.Age)
df['Age'].fillna(df['Age'].mean(), inplace=True)
df['Embarked'].fillna(df['Embarked'].mode()[0], inplace=True)
print(df.isna().sum())<jupyter_output>Survived    0
Pclass      0
Sex         0
Age         0
SibSp       0
Parch       0
Fare        0
Embarked    0
dtype: int64<jupyter_text>'Applying ML models' one by one<jupyter_code>def create_one_hot(df, column_name):
    """One-hot encode column values. Takes the df and column name as
    input and return the df with one-hot encoded columns as output.
    """
    df[column_name] = pd.Categorical(df[column_name])
    one_hot = pd.get_dummies(df[column_name], prefix = column_name, drop_first=True)
    # add dummies to original df:
    df = df.drop(column_name,axis=1)
    df = pd.concat([one_hot, df], axis = 1)
    return df

df = create_one_hot(df, 'Embarked')
df = create_one_hot(df, 'Sex')
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import f1_score, precision_score, confusion_matrix, recall_score, accuracy_score,roc_auc_score,confusion_matrix

class classification:
  
  def __init__(self, X, y):
    
    self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    self.labels = np.unique(y).tolist()

  def confusion_matrix(self):

    cm = confusion_matrix(self.y_test, self.pred, self.labels)
    print(" The confusion matrix is :", '\n' , cm)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    cax = ax.matshow(cm)
    plt.title('Confusion matrix of the classifier')
    fig.colorbar(cax)
    ax.set_xticklabels([''] + self.labels)
    ax.set_yticklabels([''] + self.labels)
    plt.xlabel('Predicted')
    plt.ylabel('True')
    plt.show()


  def calc_metrics_class(self):

    precision = precision_score(self.pred, self.y_test)
    recall = recall_score(self.pred,self.y_test)
    f1 = f1_score(self.pred,self.y_test)
    accuracy = accuracy_score(self.pred,self.y_test)
    roc = roc_auc_score(self.y_test, self.pred)
    print("The precision for the model is :", precision, '\n', "The recall for the model is : ", recall, '\n', "The f1 score of the model is :", f1, '\n', 
          "The accuracy of the model is : ", accuracy, '\n', "The ROC curve of the model is : ", roc )

  def gradient_boost(self):
      print("Performing modelling for Gradient boost")
      GradBoostClasCV = GradientBoostingClassifier(random_state=42)
      model_params = {
                        "max_depth": [20],
                        "subsample": [ 0.5],
                        "n_estimators":[150],
                        "learning_rate": [0.01],
                        "criterion": ['mae']
                      }
      grid_model = GridSearchCV(estimator=GradBoostClasCV, param_grid=model_params, cv=5, n_jobs=-1)
      grid_model.fit(self.X_train,self.y_train)
      print("Best parameters =", grid_model.best_params_)
      model_clf = GradBoostClasCV.set_params(**grid_model.best_params_)
      model_clf.fit(self.X_train, self.y_train)
      self.pred = model_clf.predict(self.X_test)
      self.calc_metrics_class()
      self.confusion_matrix()

  def random_forest(self):
      print("Performing modelling for Random forest")
      rf_model = RandomForestClassifier(random_state=1)
      param_grid = {
                    'n_estimators': [150],
                    'max_features': ['auto'],
                    'min_samples_split': [2]
                    }
      grid_model = GridSearchCV(estimator=rf_model, param_grid=param_grid, cv = 3, verbose=2, n_jobs=-1)
      grid_model.fit(self.X_train,self.y_train)
      print("Best parameters =", grid_model.best_params_)
      model_clf = rf_model.set_params(**grid_model.best_params_)
      model_clf.fit(self.X_train, self.y_train)
      self.pred = model_clf.predict(self.X_test)
      self.calc_metrics_class()
      self.confusion_matrix()

  def decision_tree(self):
      print("Performing modelling for decision tree")
      #create a dictionary of all values we want to test
      param_grid = { 'criterion':['gini'],'max_depth': [10]}
      # decision tree model
      dtree_model=DecisionTreeClassifier()
      #use gridsearch to test all values
      grid_model = GridSearchCV(dtree_model, param_grid, cv=5, n_jobs=-1)
      grid_model.fit(self.X_train,self.y_train)
      print("Best parameters =", grid_model.best_params_)
      model_clf = dtree_model.set_params(**grid_model.best_params_)
      model_clf.fit(self.X_train, self.y_train)
      self.pred = model_clf.predict(self.X_test)
      self.calc_metrics_class()
      self.confusion_matrix()<jupyter_output><empty_output><jupyter_text>Applied models and got accuracy one by one<jupyter_code>X = df.drop('Survived',axis=1)
y = df['Survived']
p1 = classification(X,y)
p1.random_forest()
p1.gradient_boost()
p1.decision_tree()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup