Binary Classification results in a decision that is either true or false.
Binary classification Examples:
Multi-class classification categorizes examples in one of several potential categories (always three or more).
Multi-class classification Examples:
Cross-entropy is a score that summarizes the average difference between the actual and predicted probability distributions for all classes. In a classification model, the goal is to minimize the score, with a perfect cross-entropy value is 0.
We can calculate cross-entropy loss by using the
log_loss() function in scikit-learn.
# example implementation of cross-entropy losstrue_labels = [1, 0, 0]predicted_labels = [0.7, 0.2, 0.1]print(log_loss(true_labels, predicted_labels))
To prepare data for cross-entropy loss analysis, you can use the
to_categorical() function in TensorFlow’s Keras API to convert labels into one-hot-encodings.
updated_y_train = tensorflow.keras.utils.to_categorical(y_train, dtype = 'int64')updated_y_test = tensorflow.keras.utils.to_categorical(y_test, dtype = 'int64')
When performing a deep learning classification model, one common
loss parameter is
categorical_crossentropy. Another loss parameter one can use for deep learning classification models is
sparse_categorical_crossentropy, which is a computationally modified categorical cross-entropy loss that allows integer labels to be left as they are to avoid the procedure of encoding.
We can set a model’s loss parameter in the Keras API with TensorFlow as depicted in the code block.
# categorical cross-entropymy_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])# sparse categorical cross-entropymodel.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
In a deep learning classification model, an F1-score can be used to evaluate how our model performs based on how poorly it makes false negative mistakes.
In the code snippet shown, we do the following:
my_testusing the scikit-learn
.predict()method and assign the result to the
my_test_labelsinto the index of the class the sample belongs to using
.argmax()from the NumPy library. The index corresponds to our class encoded as an integer.
.classification_report()method from the scikit-learn library to calculate all the metrics.
import numpy as npfrom sklearn.metrics import classification_reportyhat_classes = np.argmax(my_model.predict(my_test), axis = -1)y_true = np.argmax(my_test_labels, axis=1)print(classification_report(y_true, yhat_classes))