# XGBoost
import pandas as pd
import numpy as np
import seaborn as sns
import [Link] as plt
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import accuracy_score, confusion_matrix,
classification_report
from collections import Counter
import xgboost as xgb
from imblearn.over_sampling import SMOTE
# Load dataset
file_path = "c:/Users/TATA User/Downloads/HTB Data (1).xlsx"
df = pd.read_excel(file_path, sheet_name="Data")
# Select main parameters only
main_params = [
'P_Luboil', # Oil pressure higher
'Eng SFC', # SFC
'Lambda', # Lambda
'EngPower', # Power
'E_Torque', # Torque
'BlowBy', # Blow by pressure
'P_Cr_Case', # Crank case pressure
'T_LubOil', # Oil temperature idle
'T_Heater', # Oil temperature higher
'Status' # Target variable
]
df = df[main_params]
# Clean and convert target variable to binary (OK = 1, Nok = 0)
df['Status'] = df['Status'].astype(str).[Link]().replace({'OK': 1,
'Ok': 1, 'Nok': 0, 'NOK': 0})
# Check unique values and distribution
print("Unique values in 'Status':", df['Status'].unique())
print("Class distribution:\n", df['Status'].value_counts())
# Handle missing values (fill with mean for numeric columns)
for col in [Link]:
if col != 'Status':
df[col] = df[col].fillna(df[col].mean())
# Ensure no NaN values in Status
df['Status'] = df['Status'].fillna(df['Status'].mode()[0])
# Define features and target
X = [Link](columns=['Status'])
y = df['Status'].astype(int) # Ensure target is integer
# Standardize numerical features
scaler = StandardScaler()
X = scaler.fit_transform(X)
# Apply SMOTE to handle class imbalance
smote = SMOTE(random_state=42)
X_resampled, y_resampled = smote.fit_resample(X, y)
# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X_resampled,
y_resampled, test_size=0.2, random_state=42, stratify=y_resampled)
# Define and train optimized XGBoost model
xgb_model = [Link](
objective="binary:logistic",
eval_metric="logloss",
use_label_encoder=False,
n_estimators=300, # Increased trees for better learning
learning_rate=0.03, # Slower learning for better generalization
max_depth=8, # Increased depth for capturing complex
patterns
min_child_weight=3, # Prevents overfitting on noisy data
gamma=0.1, # Helps in pruning unnecessary splits
subsample=0.85, # Helps with generalization
colsample_bytree=0.9, # Uses more features per tree
scale_pos_weight=len(y_train[y_train == 0]) / len(y_train[y_train
== 1]), # Handles class imbalance
random_state=42
)
xgb_model.fit(X_train, y_train)
# Predict probabilities for threshold tuning
y_pred_probs = xgb_model.predict_proba(X_test)[:, 1]
# **Optimized threshold tuning**
optimal_threshold = 0.6 # Adjust threshold to reduce FP & FN
y_pred_xgb = (y_pred_probs > optimal_threshold).astype(int)
# Evaluate the model
acc_xgb = accuracy_score(y_test, y_pred_xgb)
print(f"XGBoost Model - Accuracy: {acc_xgb:.4f}")
print(classification_report(y_test, y_pred_xgb, zero_division=1))
# **Improved Confusion Matrix Visualization**
# Compute confusion matrix
cm = confusion_matrix(y_test, y_pred_xgb)
total_predictions = [Link](cm) # Total number of samples
[Link](figsize=(6,5))
[Link](cm, annot=True, fmt='d', cmap='Blues',
xticklabels=['Engine Not OK (0)', 'Engine OK (1)'],
yticklabels=['Engine Not OK (0)', 'Engine OK (1)'])
# Add total predictions in the title
[Link]('Predicted')
[Link]('Actual')
[Link](f'Optimized Confusion Matrix - XGBoost Model\nTotal
Predictions: {total_predictions}')
[Link]()
XGBoost Model - Accuracy: 0.9744
precision recall f1-score support
0 0.97 0.98 0.97 430
1 0.98 0.97 0.97 429
accuracy 0.97 859
macro avg 0.97 0.97 0.97 859
weighted avg 0.97 0.97 0.97 859
CNN
# CNN
import pandas as pd
import numpy as np
import seaborn as sns
import [Link] as plt
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import accuracy_score, confusion_matrix,
classification_report
from collections import Counter
import tensorflow as tf
from tensorflow import keras
from [Link] import Sequential
from [Link] import Dense, Dropout, BatchNormalization
from imblearn.over_sampling import SMOTE
# Load dataset
file_path = "c:/Users/TATA User/Downloads/HTB Data (1).xlsx"
df = pd.read_excel(file_path, sheet_name="Data")
# Select main parameters only
main_params = [
'P_Luboil', # Oil pressure higher
'Eng SFC', # SFC
'Lambda', # Lambda
'EngPower', # Power
'E_Torque', # Torque
'BlowBy', # Blow by pressure
'P_Cr_Case', # Crank case pressure
'T_LubOil', # Oil temperature idle
'T_Heater', # Oil temperature higher
'Status' # Target variable
]
df = df[main_params]
# Clean and convert target variable to binary (OK = 1, Nok = 0)
df['Status'] = df['Status'].astype(str).[Link]().replace({'OK': 1,
'Ok': 1, 'Nok': 0, 'NOK': 0})
# Check unique values and distribution
print("Unique values in 'Status':", df['Status'].unique())
print("Class distribution:\n", df['Status'].value_counts())
# Handle missing values (fill with mean for numeric)
for col in [Link]:
if col != 'Status':
df[col] = df[col].fillna(df[col].mean())
# Ensure no NaN values in Status
df['Status'] = df['Status'].fillna(df['Status'].mode()[0])
# Define features and target
X = [Link](columns=['Status'])
y = df['Status'].astype(int) # Ensure target is integer
# Standardize numerical features
scaler = StandardScaler()
X = scaler.fit_transform(X)
# Apply SMOTE to handle class imbalance
smote = SMOTE(random_state=42)
X_resampled, y_resampled = smote.fit_resample(X, y)
# Ensure dataset contains both classes
if len([Link]()) < 2:
raise ValueError("Error: The dataset must contain both 'OK' and
'Nok' samples!")
# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X_resampled,
y_resampled, test_size=0.2, random_state=42, stratify=y_resampled)
# Define improved CNN model
def build_cnn_model(input_shape):
model = Sequential([
Dense(128, activation='relu', input_shape=(input_shape,)),
BatchNormalization(),
Dropout(0.3),
Dense(64, activation='relu'),
BatchNormalization(),
Dropout(0.3),
Dense(32, activation='relu'),
BatchNormalization(),
Dropout(0.3),
Dense(1, activation='sigmoid')
])
[Link](optimizer=[Link](learning_rate=0.001),
loss='binary_crossentropy', metrics=['accuracy'])
return model
# Train and evaluate CNN model
cnn_model = build_cnn_model(X_train.shape[1])
early_stopping = [Link](monitor='val_loss',
patience=5, restore_best_weights=True)
cnn_model.fit(X_train, y_train, epochs=100, batch_size=32,
validation_data=(X_test, y_test), callbacks=[early_stopping],
verbose=1)
# Evaluate CNN model
y_pred_cnn = (cnn_model.predict(X_test) > 0.5).astype(int)
acc_cnn = accuracy_score(y_test, y_pred_cnn)
print(f"CNN Model - Accuracy: {acc_cnn:.4f}")
print(classification_report(y_test, y_pred_cnn, zero_division=1))
# Plot Confusion Matrix
cm = confusion_matrix(y_test, y_pred_xgb)
total_predictions = [Link](cm) # Total number of samples
[Link](figsize=(6,5))
[Link](cm, annot=True, fmt='d', cmap='Blues',
xticklabels=['Engine Not OK (0)', 'Engine OK (1)'],
yticklabels=['Engine Not OK (0)', 'Engine OK (1)'])
# Add total predictions in the title
[Link]('Predicted')
[Link]('Actual')
[Link](f'Optimized Confusion Matrix - XGBoost Model\nTotal
Predictions: {total_predictions}')
[Link]()
CNN Model - Accuracy: 0.9593
precision recall f1-score support
0 0.94 0.99 0.96 430
1 0.99 0.93 0.96 429
accuracy 0.96 859
macro avg 0.96 0.96 0.96 859
weighted avg 0.96 0.96 0.96 859
Random Forestclassifier and SVM
#RandomForestClassifier and SVM
import pandas as pd
import numpy as np
import seaborn as sns
import [Link] as plt
from sklearn.model_selection import train_test_split, cross_val_score
from [Link] import StandardScaler
from [Link] import RandomForestClassifier
from [Link] import SVC
from [Link] import accuracy_score, confusion_matrix,
classification_report
from collections import Counter
# Load dataset
file_path = "c:/Users/TATA User/Downloads/HTB Data (1).xlsx"
df = pd.read_excel(file_path, sheet_name="Data")
# Select main parameters only
main_params = [
'P_Luboil', # Oil pressure higher
'Eng SFC', # SFC
'Lambda', # Lambda
'EngPower', # Power
'E_Torque', # Torque
'BlowBy', # Blow by pressure
'P_Cr_Case', # Crank case pressure
'T_LubOil', # Oil temperature idle
'T_Heater', # Oil temperature higher
'Status' # Target variable
]
df = df[main_params]
# Convert target variable to binary (OK = 1, NOK = 0)
df['Status'] = df['Status'].astype(str).[Link]().map({'OK': 1,
'Nok': 0})
# Fix: Ensure at least one sample from each class exists in the dataset
if df['Status'].nunique() < 2:
raise ValueError("Error: The dataset must contain both 'OK' and
'NOK' samples!")
# Fix: Handle missing values correctly
df['Status'] = df['Status'].fillna(df['Status'].mode()[0]) # Fill NaN
in target column
# Fill missing values in features with mean
for col in [Link]:
if col != 'Status':
df[col] = df[col].fillna(df[col].mean())
# Define features and target
X = [Link](columns=['Status'])
y = df['Status']
# Standardize numerical features
scaler = StandardScaler()
X = scaler.fit_transform(X)
# Fix: Ensure stratified split has both classes
try:
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
except ValueError as e:
print("Error in stratified split:", e)
print("Falling back to random split (not recommended for imbalanced
data).")
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
# Compute class weights for handling imbalance
class_counts = Counter(y_train)
total_samples = sum(class_counts.values())
class_weights = {cls: total_samples / (len(class_counts) * count) for
cls, count in class_counts.items()}
# Train and Evaluate Models
def evaluate_model(model, name):
[Link](X_train, y_train)
y_pred = [Link](X_test)
acc = accuracy_score(y_test, y_pred)
f1 = cross_val_score(model, X_train, y_train, cv=5,
scoring='f1_macro').mean()
print(f"{name} - Accuracy: {acc:.4f}, F1 Score: {f1:.4f}")
print(classification_report(y_test, y_pred, zero_division=1))
# Plot Confusion Matrix
cm = confusion_matrix(y_test, y_pred)
total_predictions = [Link](cm) # Total number of samples
[Link](cm, annot=True, fmt='d', cmap='Blues',
xticklabels=['Engine Not OK (0)', 'Engine OK (1)'],
yticklabels=['Engine Not OK (0)', 'Engine OK (1)'])
[Link]('Predicted')
[Link]('Actual')
[Link](f'Optimized Confusion Matrix - XGBoost Model\nTotal
Predictions: {total_predictions}')
[Link]()
# Models to test
models = {
"Random Forest": RandomForestClassifier(n_estimators=100,
random_state=42, class_weight=class_weights),
"SVM": SVC(kernel='rbf', probability=True,
class_weight=class_weights)
}
for name, model in [Link]():
evaluate_model(model, name)
Random Forest - Accuracy: 0.9576, F1 Score: 0.5011
precision recall f1-score support
0.0 1.00 0.00 0.00 19
1.0 0.96 1.00 0.98 429
accuracy 0.96 448
macro avg 0.98 0.50 0.49 448
weighted avg 0.96 0.96 0.94 448
SVM - Accuracy: 0.8616, F1 Score: 0.5598
precision recall f1-score support
0.0 0.16 0.53 0.24 19
1.0 0.98 0.88 0.92 429
accuracy 0.86 448
macro avg 0.57 0.70 0.58 448
weighted avg 0.94 0.86 0.89 448