Confusion matrix with custom color and custom font family in google colab


## **Confusion Matrix with cmap = 'OrRd'**
 
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
 
# Define your values
TP = 219333
TN = 52
FP = 0
FN = 0
 
# Create a confusion matrix manually
cm = [[TN, FP],
      [FN, TP]]
 
# Convert the confusion matrix to a Pandas DataFrame for tabular display
cm_df = pd.DataFrame(cm, index=['Actual Negative', 'Actual Positive'], columns=['Predicted Negative', 'Predicted Positive'])
 
# Print the confusion matrix in table format
print("\nConfusion Matrix:")
print(cm_df)
 
 
# Specify the font family
font = {'family': 'serif', 'color':  'darkred', 'weight': 'normal', 'size': 12}
 
# Create a custom colormap with specified colors
#cmap = sns.color_palette(['#d0afc6', '#537162', '#537162', '#013220'])  # Custom colors for FP, TN, TP, FN
 
# Plot confusion matrix using heatmap with custom font and colormap
sns.heatmap(cm, annot=True, cmap = 'OrRd', fmt='g', annot_kws={"size": 12, "fontweight": "bold", "fontfamily": "serif"})
plt.title('Confusion Matrix', fontdict=font)  # Set font family for the title
plt.xlabel('Predicted', fontdict=font)         # Set font family for the x-axis label
plt.ylabel('Actual', fontdict=font)            # Set font family for the y-axis label
 
# Show the plot
plt.show()
 
 


## **Confusion Matrix with cmap = 'YlGnBu'**
 
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
 
# Define your values
TP = 250377
TN = 48
FP = 0
FN = 0
 
# Create a confusion matrix manually
cm = [[TN, FP],
      [FN, TP]]
 
# Convert the confusion matrix to a Pandas DataFrame for tabular display
cm_df = pd.DataFrame(cm, index=['Actual Negative', 'Actual Positive'], columns=['Predicted Negative', 'Predicted Positive'])
 
# Print the confusion matrix in table format
print("\nConfusion Matrix:")
print(cm_df)
 
 
# Specify the font family
font = {'family': 'serif', 'color':  'darkred', 'weight': 'normal', 'size': 12}
 
# Create a custom colormap with specified colors
#cmap = sns.color_palette(['#d0afc6', '#537162', '#537162', '#013220'])  # Custom colors for FP, TN, TP, FN
 
# Plot confusion matrix using heatmap with custom font and colormap
sns.heatmap(cm, annot=True, cmap = 'YlGnBu', fmt='g', annot_kws={"size": 12, "fontweight": "bold", "fontfamily": "serif"})
plt.title('Confusion Matrix', fontdict=font)  # Set font family for the title
plt.xlabel('Predicted', fontdict=font)         # Set font family for the x-axis label
plt.ylabel('Actual', fontdict=font)            # Set font family for the y-axis label
 
# Show the plot
plt.show()
 




  ## **Confusion Matrix with cmap = 'RdPu'**   from sklearn.metrics import confusion_matrix import seaborn as sns import matplotlib.pyplot as plt import pandas as pd   # Define your values TP = 219333 TN = 127 FP = 0 FN = 0   # Create a confusion matrix manually cm = [[TN, FP], [FN, TP]]   # Convert the confusion matrix to a Pandas DataFrame for tabular display cm_df = pd.DataFrame(cm, index=['Actual Negative', 'Actual Positive'], columns=['Predicted Negative', 'Predicted Positive'])   # Print the confusion matrix in table format print("\nConfusion Matrix:") print(cm_df)     # Specify the font family font = {'family': 'serif', 'color': 'darkred', 'weight': 'normal', 'size': 12}   # Create a custom colormap with specified colors #cmap = sns.color_palette(['#d0afc6', '#537162', '#537162', '#013220']) # Custom colors for FP, TN, TP, FN   # Plot confusion matrix using heatmap with custom font and colormap sns.heatmap(cm, annot=True, cmap = 'RdPu', fmt='g', annot_kws={"size": 12, "fontweight": "bold", "fontfamily": "serif"}) plt.title('Confusion Matrix', fontdict=font) # Set font family for the title plt.xlabel('Predicted', fontdict=font) # Set font family for the x-axis label plt.ylabel('Actual', fontdict=font) # Set font family for the y-axis label   # Show the plot plt.show()    


## **Confusion Matrix with cmap = 'YlGn'**
 
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
 
# Define your values
TP = 182608
TN = 48
FP = 0
FN = 0
 
# Create a confusion matrix manually
cm = [[TN, FP],
      [FN, TP]]
 
# Convert the confusion matrix to a Pandas DataFrame for tabular display
cm_df = pd.DataFrame(cm, index=['Actual Negative', 'Actual Positive'], columns=['Predicted Negative', 'Predicted Positive'])
 
# Print the confusion matrix in table format
print("\nConfusion Matrix:")
print(cm_df)
 
 
# Specify the font family
font = {'family': 'serif', 'color':  'darkred', 'weight': 'normal', 'size': 12}
 
# Create a custom colormap with specified colors
#cmap = sns.color_palette(['#d0afc6', '#537162', '#537162', '#013220'])  # Custom colors for FP, TN, TP, FN
 
# Plot confusion matrix using heatmap with custom font and colormap
sns.heatmap(cm, annot=True, cmap = 'YlGn', fmt='g', annot_kws={"size": 12, "fontweight": "bold", "fontfamily": "serif"})
plt.title('Confusion Matrix', fontdict=font)  # Set font family for the title
plt.xlabel('Predicted', fontdict=font)         # Set font family for the x-axis label
plt.ylabel('Actual', fontdict=font)            # Set font family for the y-axis label
 
# Show the plot
plt.show()
 
 

## **Confusion Matrix with cmap = 'Blues'**
 
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
 
# Define your values
TP = 373521
TN = 523
FP = 0
FN = 0
 
# Create a confusion matrix manually
cm = [[TN, FP],
      [FN, TP]]
 
# Convert the confusion matrix to a Pandas DataFrame for tabular display
cm_df = pd.DataFrame(cm, index=['Actual Negative', 'Actual Positive'], columns=['Predicted Negative', 'Predicted Positive'])
 
# Print the confusion matrix in table format
print("\nConfusion Matrix:")
print(cm_df)
 
 
# Specify the font family
font = {'family': 'serif', 'color':  'darkred', 'weight': 'normal', 'size': 12}
 
# Create a custom colormap with specified colors
#cmap = sns.color_palette(['#d0afc6', '#537162', '#537162', '#013220'])  # Custom colors for FP, TN, TP, FN
 
# Plot confusion matrix using heatmap with custom font and colormap
sns.heatmap(cm, annot=True, cmap = 'Blues', fmt='g', annot_kws={"size": 12, "fontweight": "bold", "fontfamily": "serif"})
plt.title('Confusion Matrix', fontdict=font)  # Set font family for the title
plt.xlabel('Predicted', fontdict=font)         # Set font family for the x-axis label
plt.ylabel('Actual', fontdict=font)            # Set font family for the y-axis label
 
# Show the plot
plt.show()
 
 


# **Confusion matrix with cmap = 'YlOrBr'**
 
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
 
# Define your values
TP = 201932
TN = 271
FP = 0
FN = 0
 
# Create a confusion matrix manually
cm = [[TN, FP],
      [FN, TP]]
 
# Convert the confusion matrix to a Pandas DataFrame for tabular display
cm_df = pd.DataFrame(cm, index=['Actual Negative', 'Actual Positive'], columns=['Predicted Negative', 'Predicted Positive'])
 
# Print the confusion matrix in table format
print("\nConfusion Matrix:")
print(cm_df)
 
 
# Specify the font family
font = {'family': 'serif', 'color':  'darkred', 'weight': 'normal', 'size': 12}
 
# Create a custom colormap with specified colors
#cmap = sns.color_palette(['#d0afc6', '#537162', '#537162', '#013220'])  # Custom colors for FP, TN, TP, FN
 
# Plot confusion matrix using heatmap with custom font and colormap
sns.heatmap(cm, annot=True, cmap = 'YlOrBr', fmt='g', annot_kws={"size": 12, "fontweight": "bold", "fontfamily": "serif"})
plt.title('Confusion Matrix', fontdict=font)  # Set font family for the title
plt.xlabel('Predicted', fontdict=font)         # Set font family for the x-axis label
plt.ylabel('Actual', fontdict=font)            # Set font family for the y-axis label
 
# Show the plot
plt.show()
 


 
# **Confusion Matrix with cmap = 'Purples'**
 
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
 
# Define your values
TP = 206395
TN = 46
FP = 0
FN = 0
 
# Create a confusion matrix manually
cm = [[TN, FP],
      [FN, TP]]
 
# Convert the confusion matrix to a Pandas DataFrame for tabular display
cm_df = pd.DataFrame(cm, index=['Actual Negative', 'Actual Positive'], columns=['Predicted Negative', 'Predicted Positive'])
 
# Print the confusion matrix in table format
print("\nConfusion Matrix:")
print(cm_df)
 
 
# Specify the font family
font = {'family': 'serif', 'color':  'darkred', 'weight': 'normal', 'size': 12}
 
# Create a custom colormap with specified colors
#cmap = sns.color_palette(['#d0afc6', '#537162', '#537162', '#013220'])  # Custom colors for FP, TN, TP, FN
 
# Plot confusion matrix using heatmap with custom font and colormap
sns.heatmap(cm, annot=True, cmap = 'Purples', fmt='g', annot_kws={"size": 12, "fontweight": "bold", "fontfamily": "serif"})
plt.title('Confusion Matrix', fontdict=font)  # Set font family for the title
plt.xlabel('Predicted', fontdict=font)         # Set font family for the x-axis label
plt.ylabel('Actual', fontdict=font)            # Set font family for the y-axis label
 
# Show the plot
plt.show()
 
 


# **Confusion Matrix with cmap = 'Oranges'**
 
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
 
# Define your values
TP = 193064
TN = 46
FP = 0
FN = 0
 
# Create a confusion matrix manually
cm = [[TN, FP],
      [FN, TP]]
 
# Convert the confusion matrix to a Pandas DataFrame for tabular display
cm_df = pd.DataFrame(cm, index=['Actual Negative', 'Actual Positive'], columns=['Predicted Negative', 'Predicted Positive'])
 
# Print the confusion matrix in table format
print("\nConfusion Matrix:")
print(cm_df)
 
 
# Specify the font family
font = {'family': 'serif', 'color':  'darkred', 'weight': 'normal', 'size': 12}
 
# Create a custom colormap with specified colors
cmap = sns.color_palette(['#CD5C5C', '#541E1B'])  # Custom colors for FP, TN, TP, FN
 
# Plot confusion matrix using heatmap with custom font and colormap
sns.heatmap(cm, annot=True, cmap = 'Oranges', fmt='g', annot_kws={"size": 12, "fontweight": "bold", "fontfamily": "serif"})
plt.title('Confusion Matrix', fontdict=font)  # Set font family for the title
plt.xlabel('Predicted', fontdict=font)         # Set font family for the x-axis label
plt.ylabel('Actual', fontdict=font)            # Set font family for the y-axis label
 
# Show the plot
plt.show()
 




# **Confusion Matrix with cmap = 'Greens'**
 
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
 
# Define your values
TP = 180296
TN = 62
FP = 0
FN = 0
 
# Create a confusion matrix manually
cm = [[TN, FP],
      [FN, TP]]
 
# Convert the confusion matrix to a Pandas DataFrame for tabular display
cm_df = pd.DataFrame(cm, index=['Actual Negative', 'Actual Positive'], columns=['Predicted Negative', 'Predicted Positive'])
 
# Print the confusion matrix in table format
print("\nConfusion Matrix:")
print(cm_df)
 
 
# Specify the font family
font = {'family': 'serif', 'color':  'darkred', 'weight': 'normal', 'size': 12}
 
# Create a custom colormap with specified colors
cmap = sns.color_palette(['#CD5C5C', '#541E1B'])  # Custom colors for FP, TN, TP, FN
 
# Plot confusion matrix using heatmap with custom font and colormap
sns.heatmap(cm, annot=True, cmap = 'Greens', fmt='g', annot_kws={"size": 12, "fontweight": "bold", "fontfamily": "serif"})
plt.title('Confusion Matrix', fontdict=font)  # Set font family for the title
plt.xlabel('Predicted', fontdict=font)         # Set font family for the x-axis label
plt.ylabel('Actual', fontdict=font)            # Set font family for the y-axis label
 
# Show the plot
plt.show()
 


# **Confusion Matrix with cmap = 'YlOrRd'**
 
from sklearn.metrics import confusion_matrix
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
 
# Define your values
TP = 128593
TN = 74
FP = 0
FN = 1
 
# Create a confusion matrix manually
cm = [[TN, FP],
      [FN, TP]]
 
# Convert the confusion matrix to a Pandas DataFrame for tabular display
cm_df = pd.DataFrame(cm, index=['Actual Negative', 'Actual Positive'], columns=['Predicted Negative', 'Predicted Positive'])
 
# Print the confusion matrix in table format
print("\nConfusion Matrix:")
print(cm_df)
 
 
# Specify the font family
font = {'family': 'serif', 'color':  'darkred', 'weight': 'normal', 'size': 12}
 
# Create a custom colormap with specified colors
cmap = sns.color_palette(['#CD5C5C', '#541E1B'])  # Custom colors for FP, TN, TP, FN
 
# Plot confusion matrix using heatmap with custom font and colormap
sns.heatmap(cm, annot=True, cmap = 'YlOrRd', fmt='g', annot_kws={"size": 12, "fontweight": "bold", "fontfamily": "serif"})
plt.title('Confusion Matrix', fontdict=font)  # Set font family for the title
plt.xlabel('Predicted', fontdict=font)         # Set font family for the x-axis label
plt.ylabel('Actual', fontdict=font)            # Set font family for the y-axis label
 
# Show the plot
plt.show()
 


 
# **Multiple confusion matrix with different color**
 
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import seaborn as sns
import pandas as pd
 
# Specify the font family
font = {'family': 'serif', 'color':  'darkred', 'weight': 'normal', 'size': 10}
font1 = {'family': 'serif', 'color':  'black', 'weight': 'bold', 'size': 10}
 
# Define your values for all 14 confusion matrices
confusion_matrices = [
    {
        'name': 'Confusion Matrix for MSSLQ',#1
        'TP': 219333,
        'TN': 52,
        'FP': 0,
        'FN': 0,
        'colormap': 'OrRd',
    },
    {
        'name': 'Confusion Matrix for SSDP',#2
        'TP': 250377,
        'TN': 48,
        'FP': 0,
        'FN': 0,
        'colormap': 'YlGn',
    },
    {
        'name': 'Confusion Matrix for LDAP',#3
        'TP': 219333,
        'TN': 127,
        'FP': 0,
        'FN': 0,
        'colormap': 'OrRd',
    },
    {
        'name': 'Confusion Matrix for NetBIOS',#4
        'TP': 182608,
        'TN': 48,
        'FP': 0,
        'FN': 0,
        'colormap': 'OrRd',
    },
    {
        'name': 'Confusion Matrix for NTP',#5
        'TP': 201932,
        'TN': 271,
        'FP': 0,
        'FN': 0,
        'colormap': 'YlGn',
    },
    {
        'name': 'Confusion Matrix for SNMP',#6
        'TP': 373521,
        'TN': 523,
        'FP': 0,
        'FN': 0,
        'colormap': 'OrRd',
    },
    {
        'name': 'Confusion Matrix for UDP Flood',#7
        'TP': 206395,
        'TN': 46,
        'FP': 0,
        'FN': 0,
        'colormap': 'OrRd',
    },
    {
        'name': 'Confusion Matrix for Syn Flood',#8
        'TP': 193064,
        'TN': 46,
        'FP': 0,
        'FN': 0,
        'colormap': 'YlGn',
    },
    {
        'name': 'Confusion Matrix for TFTP',#9
        'TP': 180296,
        'TN': 62,
        'FP': 0,
        'FN': 0,
        'colormap': 'OrRd',
    },
    {
        'name': 'Confusion Matrix for UDPLag',#10
        'TP': 128593,
        'TN': 74,
        'FP': 0,
        'FN': 1,
        'colormap': 'OrRd',
    },
    {
        'name': 'Confusion Matrix for DDoS_SDN',#11
        'TP': 7577,
        'TN': 12173,
        'FP': 0,
        'FN': 0,
        'colormap': 'YlGn',
    },
    {
        'name': 'Confusion Matrix for Botnet DDoS',#12
        'TP': 99,
        'TN': 342726,
        'FP': 0,
        'FN': 1,
        'colormap': 'OrRd',
    },
    {
        'name': 'Confusion Matrix for APA_DDoS',#13
        'TP': 15169,
        'TN': 15071,
        'FP': 0,
        'FN': 0,
        'colormap': 'OrRd',
    },
    {
        'name': 'Confusion Matrix for CIC-IDS2018(DDoS)',#14
        'TP': 71892,
        'TN': 134311,
        'FP': 1,
        'FN': 0,
        'colormap': 'YlGn',
    },
]
 
# Create a 4x3 grid layout for the 10 confusion matrices
fig = plt.figure(figsize=(16, 20), dpi=300)
gs = gridspec.GridSpec(5, 3, wspace=0.2, hspace=0.4)
 
for i, cm_values in enumerate(confusion_matrices):
    TP = cm_values['TP']
    TN = cm_values['TN']
    FP = cm_values['FP']
    FN = cm_values['FN']
    name = cm_values['name']
    colormap = cm_values['colormap']
 
    cm = np.array([[TN, FP], [FN, TP]])
 
    ax = plt.subplot(gs[i])
 
    # Plot confusion matrix using heatmap
    sns.heatmap(cm, annot=True, cmap=colormap, fmt='g', annot_kws={"size": 10, "fontweight": "bold", "fontfamily": "serif"})
    ax.set_title(name, fontdict=font1)  # Set the name as the title
    ax.set_xlabel('Predicted', fontdict=font)
    ax.set_ylabel('Actual', fontdict=font)
 
 
 
# Save the figure in 4k resolution
plt.savefig('confusion_matrices_4k.png', dpi=400)
 
# Show the plot (optional)
plt.show()
 
 
# **Thank You**



No comments:

Post a Comment