Repository navigation
Expand file tree
/
Copy pathmlp_no_pca.py
More file actions
97 lines (82 loc) · 3.53 KB
/
Copy pathmlp_no_pca.py
File metadata and controls
97 lines (82 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
# 1. Load Local Dataset
excel_path = r"C:\Users\bhave\OneDrive\Bhaveshya\bio glass research\Book2_grouped_train_test_split.xlsx"
df = pd.read_excel(excel_path, sheet_name='Sheet1')
# 2. Convert specified columns to categorical (string) format
CAT_COLS = [
'SBF_pH', 'Post_Ca_P_Ratio', 'Cell_Viability_Pct',
'Soaking_Period_Days', 'Cell_Extract_uL'
]
for col in CAT_COLS:
if col in df.columns:
df[col] = df[col].astype(str)
# 3. One-Hot Encoding
# Use the full dataset before train/val split to keep consistent feature columns
# across all targets when the same preprocessing is applied.
df_encoded = pd.get_dummies(df, drop_first=True)
# 4. Target List & General Exclusions
TARGETS = ['Density_kg_m3', 'Microhardness_GPa', 'Youngs_Modulus_GPa', 'Bulk_Modulus_GPa']
EXCLUDE_COLS = [
'Sample_ID', 'Paper_Series', 'Formulation_Group', 'Composition_Group', 'Data_Split',
'Density_kg_m3', 'Microhardness_GPa', 'Youngs_Modulus_GPa', 'Bulk_Modulus_GPa'
]
# Run MLP Regressor for each target
for TARGET in TARGETS:
print(f"\n==========================================")
print(f" MLP Regressor (No PCA) - Target: {TARGET}")
print(f"==========================================")
feature_cols = [
col for col in df_encoded.columns
if col not in EXCLUDE_COLS and not any(col.startswith(ex) for ex in EXCLUDE_COLS)
]
train_data = df_encoded[df['Data_Split'] == 'Training'].copy()
X = train_data[feature_cols]
y = train_data[TARGET]
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.10, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_val_scaled = scaler.transform(X_val)
model = MLPRegressor(
hidden_layer_sizes=(64, 32),
activation='relu',
solver='adam',
learning_rate_init=0.01,
max_iter=1,
warm_start=True,
random_state=42,
)
epochs = 150
train_loss, val_loss = [], []
for _ in range(epochs):
model.fit(X_train_scaled, y_train)
train_pred = model.predict(X_train_scaled)
val_pred = model.predict(X_val_scaled)
train_loss.append(mean_squared_error(y_train, train_pred))
val_loss.append(mean_squared_error(y_val, val_pred))
y_val_pred = model.predict(X_val_scaled)
val_mse = mean_squared_error(y_val, y_val_pred)
val_mae = mean_absolute_error(y_val, y_val_pred)
val_r2 = r2_score(y_val, y_val_pred)
val_rmse = np.sqrt(val_mse)
print(f"Validation MSE: {val_mse:.4f}")
print(f"Validation MAE: {val_mae:.4f}")
print(f"Validation RMSE: {val_rmse:.4f}")
print(f"Validation R²: {val_r2:.4f}")
plt.figure(figsize=(8, 5))
plt.plot(range(1, epochs + 1), train_loss, label='Train Loss (MSE)', color='blue', linewidth=2)
plt.plot(range(1, epochs + 1), val_loss, label='Validation Loss (MSE)', color='orange', linewidth=2, linestyle='--')
plt.title(f'MLP Regressor (No PCA): Train vs Validation Loss ({TARGET})', fontsize=12, fontweight='bold')
plt.xlabel('Epochs', fontsize=10)
plt.ylabel('Loss (MSE)', fontsize=10)
plt.legend(fontsize=10)
plt.grid(True, linestyle=':', alpha=0.6)
plt.tight_layout()
plt.savefig(f'{TARGET}_mlp_no_pca_loss.png')
plt.show()
plt.close()