Alvoradozerouno commited on
Commit
821cc4b
·
verified ·
1 Parent(s): bcc5922

[GENESIS] Upload risk_ml.py

Browse files
Files changed (1) hide show
  1. risk_ml.py +124 -0
risk_ml.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ GENESIS Risk ML Engine v10.1
4
+ Predictive risk scoring based on infrastructure metrics.
5
+
6
+ Features:
7
+ - Multi-feature risk prediction (CPU, Memory, Network, Disk, Error Rate)
8
+ - Gradient Boosted model for non-linear risk patterns
9
+ - Confidence intervals for regulatory reporting
10
+ - JSON output for audit trail integration
11
+ """
12
+
13
+ import json
14
+ import sys
15
+ from datetime import datetime, timezone
16
+
17
+ try:
18
+ import numpy as np
19
+ from sklearn.ensemble import GradientBoostingRegressor
20
+ from sklearn.model_selection import cross_val_score
21
+ except ImportError:
22
+ print("Installing dependencies...")
23
+ import subprocess
24
+ subprocess.check_call([sys.executable, "-m", "pip", "install",
25
+ "numpy", "scikit-learn", "pandas"])
26
+ import numpy as np
27
+ from sklearn.ensemble import GradientBoostingRegressor
28
+ from sklearn.model_selection import cross_val_score
29
+
30
+ TRAINING_DATA = {
31
+ "cpu": [20, 30, 40, 50, 55, 60, 65, 70, 75, 80, 85, 90, 92, 95, 98],
32
+ "memory": [15, 25, 30, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95],
33
+ "network_io": [10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 65, 70, 75, 80, 90],
34
+ "disk_usage": [20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 92],
35
+ "error_rate": [ 0, 1, 2, 3, 4, 5, 7, 9, 12, 15, 20, 30, 40, 60, 80],
36
+ "risk_score": [ 5, 8, 12, 18, 22, 28, 35, 42, 52, 65, 72, 82, 88, 94, 99],
37
+ }
38
+
39
+ X = np.array([
40
+ TRAINING_DATA["cpu"],
41
+ TRAINING_DATA["memory"],
42
+ TRAINING_DATA["network_io"],
43
+ TRAINING_DATA["disk_usage"],
44
+ TRAINING_DATA["error_rate"],
45
+ ]).T
46
+
47
+ y = np.array(TRAINING_DATA["risk_score"])
48
+
49
+ model = GradientBoostingRegressor(
50
+ n_estimators=100,
51
+ max_depth=4,
52
+ learning_rate=0.1,
53
+ random_state=42
54
+ )
55
+ model.fit(X, y)
56
+
57
+ cv_scores = cross_val_score(model, X, y, cv=3, scoring="r2")
58
+
59
+ current_metrics = {
60
+ "cpu": float(sys.argv[1]) if len(sys.argv) > 1 else 75.0,
61
+ "memory": float(sys.argv[2]) if len(sys.argv) > 2 else 65.0,
62
+ "network_io": float(sys.argv[3]) if len(sys.argv) > 3 else 50.0,
63
+ "disk_usage": float(sys.argv[4]) if len(sys.argv) > 4 else 60.0,
64
+ "error_rate": float(sys.argv[5]) if len(sys.argv) > 5 else 12.0,
65
+ }
66
+
67
+ input_vector = np.array([[
68
+ current_metrics["cpu"],
69
+ current_metrics["memory"],
70
+ current_metrics["network_io"],
71
+ current_metrics["disk_usage"],
72
+ current_metrics["error_rate"],
73
+ ]])
74
+
75
+ risk_score = float(np.clip(model.predict(input_vector)[0], 0, 100))
76
+
77
+ feature_importance = dict(zip(
78
+ ["cpu", "memory", "network_io", "disk_usage", "error_rate"],
79
+ [round(float(x), 4) for x in model.feature_importances_]
80
+ ))
81
+
82
+ if risk_score >= 80:
83
+ risk_level = "CRITICAL"
84
+ elif risk_score >= 60:
85
+ risk_level = "HIGH"
86
+ elif risk_score >= 40:
87
+ risk_level = "MEDIUM"
88
+ elif risk_score >= 20:
89
+ risk_level = "LOW"
90
+ else:
91
+ risk_level = "MINIMAL"
92
+
93
+ result = {
94
+ "timestamp": datetime.now(timezone.utc).isoformat(),
95
+ "version": "10.1.0",
96
+ "engine": "GradientBoostingRegressor",
97
+ "input_metrics": current_metrics,
98
+ "risk_score": round(risk_score, 2),
99
+ "risk_level": risk_level,
100
+ "confidence": {
101
+ "cv_r2_mean": round(float(cv_scores.mean()), 4),
102
+ "cv_r2_std": round(float(cv_scores.std()), 4),
103
+ },
104
+ "feature_importance": feature_importance,
105
+ "model_params": {
106
+ "n_estimators": 100,
107
+ "max_depth": 4,
108
+ "training_samples": len(y),
109
+ },
110
+ "recommendation": (
111
+ "IMMEDIATE ACTION REQUIRED" if risk_level == "CRITICAL" else
112
+ "Escalate to operations team" if risk_level == "HIGH" else
113
+ "Monitor closely" if risk_level == "MEDIUM" else
114
+ "Normal operations"
115
+ ),
116
+ }
117
+
118
+ with open("risk_score.json", "w") as f:
119
+ json.dump(result, f, indent=2)
120
+
121
+ with open("risk_score.txt", "w") as f:
122
+ f.write(str(round(risk_score)))
123
+
124
+ print(json.dumps(result, indent=2))