liveBook · Manning

Precision

As explained in Data-Driven Cybersecurity

Precision is the proportion of instances predicted as a class that actually belong to that class.

Precision measures how many instances predicted as a class actually belong to that class. For a high-risk classifier, it answers: how many high-risk alerts are correct? section 12.4.1

Use precision to judge the reliability of model alerts. Higher precision means fewer false positives, reducing unnecessary investigations; interpret it alongside recall, F1-score, accuracy, and the confusion matrix. section 12.4.1 section 12.5.1

Cybersecurity article classification

If precision for high-risk articles is 80%, four out of every five articles predicted as high-risk are actually high-risk. A classification report can expose different precision for low-risk and high-risk classes; one example reports 1.00 for class 0 and 0.50 for class 1. section 12.4.1

Listing 12.4 NLP exercise
# Import required libraries
import pandas as pd  #1
from sklearn.feature_extraction.text import #1
    TfidfVectorizer   #1
from sklearn.model_selection import train_test_split   #1
from sklearn.linear_model import LogisticRegression   #1
from sklearn.metrics import classification_report,  #1
    confusion_matrix   #1
import seaborn as sns   #1
import matplotlib.pyplot as plt   #1
 #1
# Step 1: Generate a balanced dataset #1
data = {  #2
    'Article': [ #2
        "Ransomware targets financial institutions",   #2
        "Security patch released for major software vulnerability",   #2
        "Hackers steal millions of customer data in breach",   #2
        "Low-risk malware detected in corporate emails",   #2
        "Phishing campaign aims to steal credentials",   #2
        "Database vulnerability in hospital systems",   #2
        "Encryption policies updated to prevent breaches",   #2
        "Zero-day vulnerability in common software",   #2
        "Mandatory security training after an attack",   #2
        "Phishing attacks target large organizations"   #2
    ] * 2,  # Duplicate entries to balance classes #2
    'Label': ['high-risk', 'low-risk', 'high-risk', #2
              'low-risk', 'high-risk', #2
              'low-risk', 'low-risk', 'high-risk', 'low-risk',
              'high-risk'] * 2  # Balanced labels
}

df = pd.DataFrame(data)  #3

# Step 2: Convert labels to numeric
df['Label'] = df['Label'].apply(lambda x:
    1 if x == 'high-risk' else 0)  #4

# Step 3: Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
    df['Article'], df['Label'], test_size=0.3,
    random_state=42)  #5

# Step 4: Apply TF-IDF and Logistic Regression
vectorizer = TfidfVectorizer(stop_words='english')  #6
X_train_tfidf = vectorizer.fit_transform(X_train)  #7
X_test_tfidf = vectorizer.transform(X_test)   #7
model = LogisticRegression()  #8
model.fit(X_train_tfidf, y_train)   #8

# Step 5: Make predictions
y_pred = model.predict(X_test_tfidf)  #9

# Step 6: Evaluate the model
print("Classification Report:\n",
      classification_report(y_test, y_pred))  #10
conf_matrix = confusion_matrix(y_test, y_pred)  #11

# Step 7: Plot the confusion matrix (Grayscale)
plt.figure(figsize=(6, 4))  #12
sns.heatmap(conf_matrix, annot=True, cmap='Greys', #12
            fmt='d',   #12
            xticklabels=['Low-Risk', 'High-Risk'], #12
            yticklabels=['Low-Risk', 'High-Risk'])   #12
plt.title('Confusion Matrix (Grayscale)')   #12
plt.xlabel('Predicted Labels')   #12
plt.ylabel('True Labels')   #12
plt.show()   #12

Trace precision through a TF-IDF and logistic-regression exercise that classifies cybersecurity articles and produces a classification report and confusion matrix. section 12.4.1

How Data-Driven Cybersecurity covers it

How deep it goes
Defines class-specific correctness and interprets reports, but stops short of presenting a mathematical formula or threshold-selection procedure.
Examples
Uses high- and low-risk news articles, network-traffic alerts, classification reports, and confusion-matrix heatmaps.
What it assumes
Assumes labeled classification predictions and introduces precision beside recall, F1-score, accuracy, and confusion-matrix outcomes.
What you can do after
Evaluate model alerts by reading class-specific precision and relating false-positive costs to operational cybersecurity requirements.
How it connects
Connects precision with recall, F1-score, accuracy, confusion matrices, false positives, and false negatives.

Also explained in

Compare how 20 books explain precision →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Driven Cybersecurity. The section links point to the parts of the book it draws on.