1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Deep Learning in Python

Connected

Exercise

Last steps in classification models

You'll now create a classification model using the titanic dataset, which has been pre-loaded into a DataFrame called df. You'll take information about the passengers and predict which ones survived.

The predictive variables are stored in a NumPy array predictors. The target to predict is in df.survived, though you'll have to manipulate it for Keras. The number of predictive features is stored in n_cols.

Here, you'll use the 'sgd' optimizer, which stands for Stochastic Gradient Descent. You'll learn more about this in the next chapter!

Instructions

100 XP
  • Convert df.survived to a categorical variable using the to_categorical() function.
  • Specify a Sequential model called model.
  • Add a Dense layer with 32 nodes. Use 'relu' as the activation and (n_cols,) as the input_shape.
  • Add the Dense output layer. Because there are two outcomes, it should have 2 units, and because it is a classification model, the activation should be 'softmax'.
  • Compile the model, using 'sgd' as the optimizer, 'categorical_crossentropy' as the loss function, and metrics=['accuracy'] to see the accuracy (what fraction of predictions were correct) at the end of each epoch.
  • Fit the model using the predictors and the target.