Introduction
In 2025, the cybersecurity landscape witnessed a significant shift with the emergence of governed AI systems that are transforming how organizations detect and respond to threats. This tutorial will guide you through building a simple yet effective cybersecurity AI monitoring system using Python and machine learning concepts. You'll learn to create a system that can detect anomalous network behavior patterns, which is a fundamental component of the governed AI systems that are becoming increasingly prevalent in 2026.
Prerequisites
- Basic Python programming knowledge
- Understanding of machine learning concepts (particularly clustering and anomaly detection)
- Python libraries: scikit-learn, pandas, numpy, matplotlib
- Basic understanding of network traffic data
Step-by-Step Instructions
1. Setting Up Your Environment
1.1 Install Required Libraries
First, we need to install the necessary Python libraries. The code below demonstrates how to set up your environment:
pip install scikit-learn pandas numpy matplotlib seaborn
Why this step? We need these libraries to handle data processing, machine learning algorithms, and visualization of network traffic patterns.
1.2 Create Project Structure
Create a directory structure for your project:
cybersecurity_ai/
├── data/
├── models/
├── src/
│ ├── __init__.py
│ ├── anomaly_detector.py
│ └── network_monitor.py
└── main.py
Why this step? Organizing your code properly makes it maintainable and scalable, which is crucial for governed AI systems.
2. Data Preparation and Simulation
2.1 Generate Sample Network Traffic Data
Let's create a script to generate realistic network traffic data that we can use for our anomaly detection system:
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
# Generate sample network traffic data
np.random.seed(42)
# Create timestamps
start_time = datetime.now() - timedelta(days=30)
timestamps = [start_time + timedelta(hours=i) for i in range(24 * 30)]
# Generate network traffic data
data = {
'timestamp': timestamps,
'bytes_sent': np.random.normal(1000000, 200000, len(timestamps)),
'bytes_received': np.random.normal(1200000, 250000, len(timestamps)),
'connections': np.random.poisson(50, len(timestamps)),
'active_sessions': np.random.poisson(25, len(timestamps))
}
# Add some anomalies
for i in range(0, len(timestamps), 100):
data['bytes_sent'][i] = np.random.normal(5000000, 500000)
data['bytes_received'][i] = np.random.normal(6000000, 600000)
# Create DataFrame
df = pd.DataFrame(data)
df.to_csv('data/network_traffic.csv', index=False)
print("Sample data generated and saved to data/network_traffic.csv")
Why this step? Real-world network data is crucial for training our anomaly detection system. We're simulating realistic patterns with intentional anomalies to test our detection capabilities.
3. Building the Anomaly Detection System
3.1 Create Anomaly Detector Module
Now, let's build our core anomaly detection functionality:
import pandas as pd
import numpy as np
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
import seaborn as sns
class AnomalyDetector:
def __init__(self, contamination=0.1):
self.model = IsolationForest(contamination=contamination, random_state=42)
self.scaler = StandardScaler()
self.is_fitted = False
def prepare_features(self, df):
# Select numerical features for anomaly detection
features = ['bytes_sent', 'bytes_received', 'connections', 'active_sessions']
return df[features]
def fit(self, df):
# Prepare features
X = self.prepare_features(df)
# Scale features
X_scaled = self.scaler.fit_transform(X)
# Fit the model
self.model.fit(X_scaled)
self.is_fitted = True
print("Model fitted successfully")
def predict(self, df):
if not self.is_fitted:
raise ValueError("Model must be fitted before prediction")
# Prepare features
X = self.prepare_features(df)
# Scale features
X_scaled = self.scaler.transform(X)
# Predict anomalies
predictions = self.model.predict(X_scaled)
# Convert -1 to 1 (anomaly) and 1 to 0 (normal)
anomalies = np.where(predictions == -1, 1, 0)
return anomalies
def evaluate(self, df, true_anomalies):
predictions = self.predict(df)
print(classification_report(true_anomalies, predictions))
Why this step? We're implementing an Isolation Forest algorithm, which is excellent for anomaly detection. It's particularly suitable for cybersecurity applications as it can identify unusual patterns without requiring labeled data.
3.2 Create Network Monitor Module
Let's build a monitoring module that can continuously analyze network traffic:
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import time
class NetworkMonitor:
def __init__(self, detector):
self.detector = detector
self.alert_threshold = 0.1 # 10% of data
def analyze_traffic(self, df):
# Detect anomalies
anomalies = self.detector.predict(df)
# Add anomaly column to dataframe
df['is_anomaly'] = anomalies
# Calculate anomaly rate
anomaly_rate = np.mean(anomalies)
return df, anomaly_rate
def generate_alert(self, df, anomaly_rate):
if anomaly_rate > self.alert_threshold:
print(f"⚠️ SECURITY ALERT: High anomaly rate detected ({anomaly_rate:.2%})")
# Get timestamps of anomalies
anomaly_timestamps = df[df['is_anomaly'] == 1]['timestamp'].tolist()
print(f"Anomalies detected at: {anomaly_timestamps[:5]}... (showing first 5)\n")
else:
print(f"✅ Normal traffic pattern detected (anomaly rate: {anomaly_rate:.2%})")
return anomaly_rate
Why this step? A real-world monitoring system needs to continuously analyze traffic and generate alerts. This module provides the framework for ongoing security monitoring.
4. Main Application Integration
4.1 Create Main Application Script
Let's put everything together in our main application:
import pandas as pd
from src.anomaly_detector import AnomalyDetector
from src.network_monitor import NetworkMonitor
def main():
# Load data
df = pd.read_csv('data/network_traffic.csv')
# Initialize detector
detector = AnomalyDetector(contamination=0.1)
# Fit the model
detector.fit(df)
# Initialize monitor
monitor = NetworkMonitor(detector)
# Analyze traffic
df_with_anomalies, anomaly_rate = monitor.analyze_traffic(df)
# Generate alert
monitor.generate_alert(df_with_anomalies, anomaly_rate)
# Visualize results
visualize_results(df_with_anomalies)
print("\n✅ Cybersecurity AI monitoring system completed")
def visualize_results(df):
plt.figure(figsize=(12, 8))
# Plot 1: Bytes sent over time
plt.subplot(2, 2, 1)
plt.plot(df['timestamp'], df['bytes_sent'], alpha=0.7)
plt.scatter(df[df['is_anomaly'] == 1]['timestamp'], df[df['is_anomaly'] == 1]['bytes_sent'],
color='red', s=50, label='Anomalies')
plt.title('Bytes Sent Over Time')
plt.xticks(rotation=45)
# Plot 2: Bytes received over time
plt.subplot(2, 2, 2)
plt.plot(df['timestamp'], df['bytes_received'], alpha=0.7)
plt.scatter(df[df['is_anomaly'] == 1]['timestamp'], df[df['is_anomaly'] == 1]['bytes_received'],
color='red', s=50, label='Anomalies')
plt.title('Bytes Received Over Time')
plt.xticks(rotation=45)
# Plot 3: Connections over time
plt.subplot(2, 2, 3)
plt.plot(df['timestamp'], df['connections'], alpha=0.7)
plt.scatter(df[df['is_anomaly'] == 1]['timestamp'], df[df['is_anomaly'] == 1]['connections'],
color='red', s=50, label='Anomalies')
plt.title('Network Connections Over Time')
plt.xticks(rotation=45)
# Plot 4: Active sessions over time
plt.subplot(2, 2, 4)
plt.plot(df['timestamp'], df['active_sessions'], alpha=0.7)
plt.scatter(df[df['is_anomaly'] == 1]['timestamp'], df[df['is_anomaly'] == 1]['active_sessions'],
color='red', s=50, label='Anomalies')
plt.title('Active Sessions Over Time')
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig('data/cybersecurity_analysis.png')
plt.show()
if __name__ == "__main__":
main()
Why this step? This integration combines all our components into a complete monitoring system that demonstrates how governed AI can be applied to cybersecurity. The visualization helps security analysts quickly identify potential threats.
5. Running Your System
5.1 Execute the Application
Run your application with the following command:
python main.py
Why this step? This executes our complete cybersecurity AI monitoring system and demonstrates how it processes network traffic data to detect potential threats.
5.2 Understanding the Output
After running the application, you should see:
- Model fitting confirmation
- Analysis results showing detected anomalies
- Security alert generation based on anomaly rate
- Visualizations of network traffic patterns with anomalies highlighted
Why this step? Understanding the output helps you interpret how your AI system is working and how it might be used in real-world security operations.
Summary
In this tutorial, we've built a foundational cybersecurity AI monitoring system that demonstrates the core concepts behind the governed AI systems that are becoming increasingly prevalent in 2026. We've created a system that can detect anomalous network behavior patterns using machine learning algorithms, specifically Isolation Forest, which is well-suited for cybersecurity applications.
The system we've built includes data preparation, anomaly detection, continuous monitoring, and visualization capabilities. This represents a key component of the next-generation cybersecurity approaches that emphasize automation and intelligent threat detection.
While this is a simplified demonstration, it shows how organizations can implement governed AI systems to reduce detection timelines and improve overall security posture, aligning with the trends highlighted in the IBM report about decreasing data breach costs through AI and automation.



