Upload NN_Classification_of_3D_Double_Helix_V0.1.py
Browse filesV0.1 (manually debugged)
The only significant change is fixing the test_size parameter in the two train_test_split calls. I have also added comments to highlight the fix.
The Bug:
In the V0.0 train_test_split function calls, the parameter was set as:
test_size = 1 - VALIDATION_SPLIT
With VALIDATION_SPLIT = 0.2, this meant test_size = 0.8.
This single line caused two major problems:
It inverted the dataset split. Instead of training on 80% of the data and testing on 20%, the model was being trained on a tiny 20% of the data and tested on 80%.
It starved the model. While the "Informed" model is simple and should learn quickly, giving it such a small portion of the data can, with an unlucky random initialization of weights, cause the optimizer to converge to a completely wrong solution (like predicting the opposite class). The extreme learning seen in the loss graph combined with the abysmal accuracy is a classic symptom of this. The model found a "perfect" solution for the tiny training set, but that solution was perfectly wrong for the general problem.
The original issue was not with the concept, but a simple (and easy to make!) bug in the implementation.
Now:
#VALIDATION_SPLIT = 0.2 #commented out
TEST_SET_SIZE = 0.2 # Use 20% of the data for testing/validation
X_train_i, X_test_i, y_train_i, y_test_i = train_test_split(X_informed, y, test_size=TEST_SET_SIZE, random_state=RANDOM_STATE)
X_train_n, X_test_n, y_train_n, y_test_n = train_test_split(X, y, test_size=TEST_SET_SIZE, random_state=RANDOM_STATE)
# Train the informed model
history_informed = model_informed.fit(X_train_i, y_train_i,
epochs=EPOCHS,
batch_size=BATCH_SIZE,
validation_data=(X_test_i, y_test_i),
verbose=1)
# Train the naive model
history_naive = model_naive.fit(X_train_n, y_train_n,
epochs=EPOCHS,
batch_size=BATCH_SIZE,
validation_data=(X_test_n, y_test_n),
verbose=1)
|
@@ -0,0 +1,522 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =============================================================================
|
| 2 |
+
#
|
| 3 |
+
# Neural Network Classification of a 3D Double Helix
|
| 4 |
+
# Proposed by Martial Terran of https huggingface.co MartialTerran
|
| 5 |
+
#
|
| 6 |
+
# This script demonstrates a key concept in machine learning: the power of
|
| 7 |
+
# feature engineering. It tackles a 3D classification problem where data
|
| 8 |
+
# is arranged in two intertwining helices.
|
| 9 |
+
#
|
| 10 |
+
# We will compare two models:
|
| 11 |
+
# 1. The "Naive" Model: A standard Multi-Layer Perceptron (MLP) that receives
|
| 12 |
+
# raw (x, y, z) coordinates. It struggles to learn the rotational
|
| 13 |
+
# geometry.
|
| 14 |
+
# 2. The "Informed" Model: A very simple network that receives engineered
|
| 15 |
+
# features. We transform the (x, y, z) coordinates into the distances
|
| 16 |
+
# from the point to the center of each helix at that point's z-level.
|
| 17 |
+
# This "unrolls" the problem, making it trivially easy to solve.
|
| 18 |
+
#
|
| 19 |
+
#
|
| 20 |
+
#=============================================================================
|
| 21 |
+
"""
|
| 22 |
+
V0.1 (manually debugged)
|
| 23 |
+
The only significant change is fixing the test_size parameter in the two train_test_split calls. I have also added comments to highlight the fix.
|
| 24 |
+
The Bug:
|
| 25 |
+
In the V0.0 train_test_split function calls, the parameter was set as:
|
| 26 |
+
test_size = 1 - VALIDATION_SPLIT
|
| 27 |
+
With VALIDATION_SPLIT = 0.2, this meant test_size = 0.8.
|
| 28 |
+
This single line caused two major problems:
|
| 29 |
+
It inverted the dataset split. Instead of training on 80% of the data and testing on 20%, the model was being trained on a tiny 20% of the data and tested on 80%.
|
| 30 |
+
It starved the model. While the "Informed" model is simple and should learn quickly, giving it such a small portion of the data can, with an unlucky random initialization of weights, cause the optimizer to converge to a completely wrong solution (like predicting the opposite class). The extreme learning seen in the loss graph combined with the abysmal accuracy is a classic symptom of this. The model found a "perfect" solution for the tiny training set, but that solution was perfectly wrong for the general problem.
|
| 31 |
+
The original issue was not with the concept, but a simple (and easy to make!) bug in the implementation.
|
| 32 |
+
|
| 33 |
+
Now:
|
| 34 |
+
#VALIDATION_SPLIT = 0.2 #commented out
|
| 35 |
+
TEST_SET_SIZE = 0.2 # Use 20% of the data for testing/validation
|
| 36 |
+
|
| 37 |
+
X_train_i, X_test_i, y_train_i, y_test_i = train_test_split(X_informed, y, test_size=TEST_SET_SIZE, random_state=RANDOM_STATE)
|
| 38 |
+
|
| 39 |
+
X_train_n, X_test_n, y_train_n, y_test_n = train_test_split(X, y, test_size=TEST_SET_SIZE, random_state=RANDOM_STATE)
|
| 40 |
+
|
| 41 |
+
# Train the informed model
|
| 42 |
+
history_informed = model_informed.fit(X_train_i, y_train_i,
|
| 43 |
+
epochs=EPOCHS,
|
| 44 |
+
batch_size=BATCH_SIZE,
|
| 45 |
+
validation_data=(X_test_i, y_test_i),
|
| 46 |
+
verbose=1)
|
| 47 |
+
|
| 48 |
+
# Train the naive model
|
| 49 |
+
history_naive = model_naive.fit(X_train_n, y_train_n,
|
| 50 |
+
epochs=EPOCHS,
|
| 51 |
+
batch_size=BATCH_SIZE,
|
| 52 |
+
validation_data=(X_test_n, y_test_n),
|
| 53 |
+
verbose=1)
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
print("# start loading libraries--- Imports ---")
|
| 58 |
+
import os
|
| 59 |
+
import sys
|
| 60 |
+
import zipfile
|
| 61 |
+
import numpy as np
|
| 62 |
+
import tensorflow as tf
|
| 63 |
+
from tensorflow import keras
|
| 64 |
+
from tensorflow.keras import layers
|
| 65 |
+
import matplotlib.pyplot as plt
|
| 66 |
+
from mpl_toolkits.mplot3d import Axes3D
|
| 67 |
+
from sklearn.model_selection import train_test_split
|
| 68 |
+
from sklearn.metrics import classification_report, confusion_matrix
|
| 69 |
+
print("done loading libraries")
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# --- Check for Google Colab Environment for Zipping Results ---
|
| 73 |
+
try:
|
| 74 |
+
import google.colab
|
| 75 |
+
IN_COLAB = True
|
| 76 |
+
print(" Colab detected: IN_COLAB = True")
|
| 77 |
+
except ImportError:
|
| 78 |
+
IN_COLAB = False
|
| 79 |
+
|
| 80 |
+
# ==============================================================================
|
| 81 |
+
# === HYPERPARAMETERS & SETUP ===
|
| 82 |
+
# ==============================================================================
|
| 83 |
+
# --- Data Generation ---
|
| 84 |
+
N_POINTS_PER_BIN = 25 # Number of data points per vertical Z-bin
|
| 85 |
+
Z_BINS = 100 # Number of Z-bins to generate data in (controls length of helix)
|
| 86 |
+
HELIX_RADIUS = 5.0 # The radius of the central helix path
|
| 87 |
+
DATA_CLOUD_RADIUS = 1.5 # The radius of the data cloud around each helix point
|
| 88 |
+
GAP_FACTOR = 1.2 # A factor > 1 to create a gap between class boundaries
|
| 89 |
+
Z_CYCLES = 2.5 # Number of full 360-degree cycles the helices should make
|
| 90 |
+
NOISE_LEVEL = 0.1 # A small amount of random noise to add to all coordinates
|
| 91 |
+
|
| 92 |
+
# --- Model & Training ---
|
| 93 |
+
EPOCHS = 40
|
| 94 |
+
BATCH_SIZE = 32
|
| 95 |
+
#VALIDATION_SPLIT = 0.2
|
| 96 |
+
TEST_SET_SIZE = 0.2 # Use 20% of the data for testing/validation
|
| 97 |
+
RANDOM_STATE = 42 # For reproducible train/test splits
|
| 98 |
+
|
| 99 |
+
# --- File & Folder Management ---
|
| 100 |
+
DATASET_FOLDER = "dataset"
|
| 101 |
+
PLOTS_FOLDER = "plots"
|
| 102 |
+
DATASET_FILENAME = "double_helix_data.npz"
|
| 103 |
+
DATASET_PATH = os.path.join(DATASET_FOLDER, DATASET_FILENAME)
|
| 104 |
+
|
| 105 |
+
# Create output directories if they don't exist
|
| 106 |
+
os.makedirs(DATASET_FOLDER, exist_ok=True)
|
| 107 |
+
os.makedirs(PLOTS_FOLDER, exist_ok=True)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# ==============================================================================
|
| 111 |
+
# === PART 1: DATA GENERATION & LOADING ===
|
| 112 |
+
# ==============================================================================
|
| 113 |
+
|
| 114 |
+
def generate_double_helix_data():
|
| 115 |
+
"""Generates the synthetic 3D double helix dataset."""
|
| 116 |
+
print("Generating new double helix dataset...")
|
| 117 |
+
points = []
|
| 118 |
+
labels = []
|
| 119 |
+
|
| 120 |
+
# Radius boundaries for each class
|
| 121 |
+
radius_class_0_max = DATA_CLOUD_RADIUS
|
| 122 |
+
radius_class_1_min = DATA_CLOUD_RADIUS * GAP_FACTOR
|
| 123 |
+
radius_class_1_max = DATA_CLOUD_RADIUS * (GAP_FACTOR + 1.0)
|
| 124 |
+
|
| 125 |
+
z_values = np.linspace(0, Z_BINS, Z_BINS)
|
| 126 |
+
|
| 127 |
+
for z in z_values:
|
| 128 |
+
for _ in range(N_POINTS_PER_BIN):
|
| 129 |
+
# Angular position along the helix
|
| 130 |
+
angle_rad = 2 * np.pi * Z_CYCLES * z / Z_BINS
|
| 131 |
+
|
| 132 |
+
# Centroid of Helix 1 (Class 0)
|
| 133 |
+
x1_c = HELIX_RADIUS * np.cos(angle_rad)
|
| 134 |
+
y1_c = HELIX_RADIUS * np.sin(angle_rad)
|
| 135 |
+
|
| 136 |
+
# Centroid of Helix 2 (Class 1) - 180 degrees out of phase
|
| 137 |
+
x2_c = -x1_c
|
| 138 |
+
y2_c = -y1_c
|
| 139 |
+
|
| 140 |
+
# Randomly assign a class
|
| 141 |
+
label = np.random.randint(0, 2)
|
| 142 |
+
|
| 143 |
+
# Generate a point within the class's data cloud
|
| 144 |
+
point_angle = np.random.rand() * 2 * np.pi
|
| 145 |
+
|
| 146 |
+
if label == 0:
|
| 147 |
+
point_radius = np.random.uniform(0, radius_class_0_max)
|
| 148 |
+
cx, cy = x1_c, y1_c
|
| 149 |
+
else: # label == 1
|
| 150 |
+
point_radius = np.random.uniform(radius_class_1_min, radius_class_1_max)
|
| 151 |
+
cx, cy = x2_c, y2_c
|
| 152 |
+
|
| 153 |
+
px = cx + point_radius * np.cos(point_angle)
|
| 154 |
+
py = cy + point_radius * np.sin(point_angle)
|
| 155 |
+
pz = z
|
| 156 |
+
|
| 157 |
+
# Add noise
|
| 158 |
+
noise = np.random.randn(3) * NOISE_LEVEL
|
| 159 |
+
points.append([px + noise[0], py + noise[1], pz + noise[2]])
|
| 160 |
+
labels.append(label)
|
| 161 |
+
|
| 162 |
+
X = np.array(points)
|
| 163 |
+
y = np.array(labels)
|
| 164 |
+
print(f"Dataset generated with {len(X)} points.")
|
| 165 |
+
return X, y
|
| 166 |
+
|
| 167 |
+
# --- Main Data Loading/Generation Logic ---
|
| 168 |
+
if os.path.exists(DATASET_PATH):
|
| 169 |
+
print(f"Loading existing dataset from '{DATASET_PATH}'...")
|
| 170 |
+
with np.load(DATASET_PATH) as data:
|
| 171 |
+
X, y = data['X'], data['y']
|
| 172 |
+
print(f"Dataset loaded with {len(X)} points.")
|
| 173 |
+
else:
|
| 174 |
+
X, y = generate_double_helix_data()
|
| 175 |
+
np.savez(DATASET_PATH, X=X, y=y)
|
| 176 |
+
print(f"Dataset saved to '{DATASET_PATH}'.")
|
| 177 |
+
|
| 178 |
+
# --- Visualize the initial dataset ---
|
| 179 |
+
print("\nVisualizing the 3D dataset...")
|
| 180 |
+
fig = plt.figure(figsize=(10, 8))
|
| 181 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 182 |
+
scatter = ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y, cmap='viridis', marker='.')
|
| 183 |
+
ax.set_xlabel('X Axis')
|
| 184 |
+
ax.set_ylabel('Y Axis')
|
| 185 |
+
ax.set_zlabel('Z Axis')
|
| 186 |
+
ax.set_title('Synthetic Double Helix Dataset')
|
| 187 |
+
legend1 = ax.legend(*scatter.legend_elements(), title="Classes")
|
| 188 |
+
ax.add_artist(legend1)
|
| 189 |
+
plt.savefig(os.path.join(PLOTS_FOLDER, '01_initial_data_3d.png'))
|
| 190 |
+
print("\n You Must Close the popup Visualized the 3D Dataset to continue this script version.")
|
| 191 |
+
plt.show()
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
# ==============================================================================
|
| 195 |
+
# === PART 2: THE "INFORMED" MODEL (WITH HELIX KERNEL FEATURES) ===
|
| 196 |
+
# ==============================================================================
|
| 197 |
+
|
| 198 |
+
def helix_feature_transform(X_data):
|
| 199 |
+
"""
|
| 200 |
+
Transforms (x, y, z) into a feature space based on distance to helix centroids.
|
| 201 |
+
This is the "secret sauce" that makes the problem easy.
|
| 202 |
+
"""
|
| 203 |
+
X_transformed = []
|
| 204 |
+
for point in X_data:
|
| 205 |
+
px, py, pz = point
|
| 206 |
+
|
| 207 |
+
# Calculate the angular position for this Z-level
|
| 208 |
+
angle_rad = 2 * np.pi * Z_CYCLES * pz / Z_BINS
|
| 209 |
+
|
| 210 |
+
# Centroid of Helix 1 at this Z-level
|
| 211 |
+
x1_c = HELIX_RADIUS * np.cos(angle_rad)
|
| 212 |
+
y1_c = HELIX_RADIUS * np.sin(angle_rad)
|
| 213 |
+
|
| 214 |
+
# Centroid of Helix 2 at this Z-level
|
| 215 |
+
x2_c = -x1_c
|
| 216 |
+
y2_c = -y1_c
|
| 217 |
+
|
| 218 |
+
# Calculate Euclidean distance in the XY plane to each centroid
|
| 219 |
+
dist_to_h1 = np.sqrt((px - x1_c)**2 + (py - y1_c)**2)
|
| 220 |
+
dist_to_h2 = np.sqrt((px - x2_c)**2 + (py - y2_c)**2)
|
| 221 |
+
|
| 222 |
+
X_transformed.append([dist_to_h1, dist_to_h2])
|
| 223 |
+
|
| 224 |
+
return np.array(X_transformed)
|
| 225 |
+
|
| 226 |
+
print("\n--- Training Model 1: The 'Informed' Model with Helix Features ---")
|
| 227 |
+
# 1. Transform the features
|
| 228 |
+
X_informed = helix_feature_transform(X)
|
| 229 |
+
|
| 230 |
+
# 2. Split data
|
| 231 |
+
#X_train_i, X_test_i, y_train, y_test = train_test_split( X_informed, y, test_size=1-VALIDATION_SPLIT, random_state=RANDOM_STATE)
|
| 232 |
+
# ***** FIX: Corrected the test_size parameter *****
|
| 233 |
+
# We now correctly use 80% of data for training and 20% for testing.
|
| 234 |
+
X_train_i, X_test_i, y_train_i, y_test_i = train_test_split(
|
| 235 |
+
X_informed, y, test_size=TEST_SET_SIZE, random_state=RANDOM_STATE
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
# 3. Define the simple model
|
| 239 |
+
model_informed = keras.Sequential([
|
| 240 |
+
layers.Input(shape=(2,), name='informed_input'),
|
| 241 |
+
layers.Dense(1, activation='sigmoid', name='output')
|
| 242 |
+
], name="Informed_Model")
|
| 243 |
+
|
| 244 |
+
model_informed.compile(optimizer='adam',
|
| 245 |
+
loss='binary_crossentropy',
|
| 246 |
+
metrics=['accuracy'])
|
| 247 |
+
|
| 248 |
+
model_informed.summary()
|
| 249 |
+
|
| 250 |
+
# 4. Train the model
|
| 251 |
+
history_informed = model_informed.fit(X_train_i, y_train_i,
|
| 252 |
+
epochs=EPOCHS,
|
| 253 |
+
batch_size=BATCH_SIZE,
|
| 254 |
+
validation_data=(X_test_i, y_test_i),
|
| 255 |
+
verbose=1)
|
| 256 |
+
|
| 257 |
+
# ==============================================================================
|
| 258 |
+
# === PART 3: THE "NAIVE" MODEL (STANDARD MLP) ===
|
| 259 |
+
# ==============================================================================
|
| 260 |
+
|
| 261 |
+
print("\n\n--- Training Model 2: The 'Naive' Model with Raw (x, y, z) ---")
|
| 262 |
+
# 1. Split the original, untransformed data
|
| 263 |
+
# We use the same random_state to ensure the splits are comparable
|
| 264 |
+
#X_train_n, X_test_n, y_train, y_test = train_test_split( X, y, test_size=1-VALIDATION_SPLIT, random_state=RANDOM_STATE)
|
| 265 |
+
# ***** FIX: Corrected the test_size parameter and use distinct y-variables *****
|
| 266 |
+
# Using the same random_state ensures the same data points are in each split.
|
| 267 |
+
X_train_n, X_test_n, y_train_n, y_test_n = train_test_split(
|
| 268 |
+
X, y, test_size=TEST_SET_SIZE, random_state=RANDOM_STATE
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
# 2. Define the deeper MLP model
|
| 272 |
+
model_naive = keras.Sequential([
|
| 273 |
+
layers.Input(shape=(3,), name='naive_input'),
|
| 274 |
+
layers.Dense(32, activation='relu'),
|
| 275 |
+
layers.Dense(16, activation='relu'),
|
| 276 |
+
layers.Dense(1, activation='sigmoid', name='output')
|
| 277 |
+
], name="Naive_Model")
|
| 278 |
+
|
| 279 |
+
model_naive.compile(optimizer='adam',
|
| 280 |
+
loss='binary_crossentropy',
|
| 281 |
+
metrics=['accuracy'])
|
| 282 |
+
|
| 283 |
+
model_naive.summary()
|
| 284 |
+
|
| 285 |
+
# 3. Train the model
|
| 286 |
+
history_naive = model_naive.fit(X_train_n, y_train_n,
|
| 287 |
+
epochs=EPOCHS,
|
| 288 |
+
batch_size=BATCH_SIZE,
|
| 289 |
+
validation_data=(X_test_n, y_test_n),
|
| 290 |
+
verbose=1)
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
# ==============================================================================
|
| 294 |
+
# === PART 4: EVALUATION AND COMPARISON ===
|
| 295 |
+
# ==============================================================================
|
| 296 |
+
print("\n\n" + "="*50)
|
| 297 |
+
print("=== MODEL EVALUATION & COMPARISON ===")
|
| 298 |
+
print("="*50)
|
| 299 |
+
|
| 300 |
+
# --- Performance Metrics ---
|
| 301 |
+
print("\n--- Model 1 (Informed) Performance ---")
|
| 302 |
+
loss_i, acc_i = model_informed.evaluate(X_test_i, y_test_i, verbose=0)
|
| 303 |
+
print(f"Test Accuracy: {acc_i:.4f}")
|
| 304 |
+
print(f"Test Loss: {loss_i:.4f}")
|
| 305 |
+
y_pred_i = (model_informed.predict(X_test_i) > 0.5).astype("int32")
|
| 306 |
+
print("\nClassification Report:")
|
| 307 |
+
print(classification_report(y_test_i, y_pred_i))
|
| 308 |
+
print("\nConfusion Matrix:")
|
| 309 |
+
print(confusion_matrix(y_test_i, y_pred_i))
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
print("\n--- Model 2 (Naive) Performance ---")
|
| 313 |
+
loss_n, acc_n = model_naive.evaluate(X_test_n, y_test_n, verbose=0)
|
| 314 |
+
print(f"Test Accuracy: {acc_n:.4f}")
|
| 315 |
+
print(f"Test Loss: {loss_n:.4f}")
|
| 316 |
+
y_pred_n = (model_naive.predict(X_test_n) > 0.5).astype("int32")
|
| 317 |
+
print("\nClassification Report:")
|
| 318 |
+
print(classification_report(y_test_n, y_pred_n))
|
| 319 |
+
print("\nConfusion Matrix:")
|
| 320 |
+
print(confusion_matrix(y_test_n, y_pred_n))
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
# --- Training History Visualization ---
|
| 324 |
+
plt.figure(figsize=(14, 6))
|
| 325 |
+
|
| 326 |
+
plt.subplot(1, 2, 1)
|
| 327 |
+
plt.plot(history_informed.history['accuracy'], label='Informed Train Acc')
|
| 328 |
+
plt.plot(history_informed.history['val_accuracy'], label='Informed Val Acc', linestyle='--')
|
| 329 |
+
plt.plot(history_naive.history['accuracy'], label='Naive Train Acc')
|
| 330 |
+
plt.plot(history_naive.history['val_accuracy'], label='Naive Val Acc', linestyle='--')
|
| 331 |
+
plt.title('Model Accuracy Comparison')
|
| 332 |
+
plt.ylabel('Accuracy')
|
| 333 |
+
plt.xlabel('Epoch')
|
| 334 |
+
plt.legend()
|
| 335 |
+
plt.grid(True)
|
| 336 |
+
|
| 337 |
+
plt.subplot(1, 2, 2)
|
| 338 |
+
plt.plot(history_informed.history['loss'], label='Informed Train Loss')
|
| 339 |
+
plt.plot(history_informed.history['val_loss'], label='Informed Val Loss', linestyle='--')
|
| 340 |
+
plt.plot(history_naive.history['loss'], label='Naive Train Loss')
|
| 341 |
+
plt.plot(history_naive.history['val_loss'], label='Naive Val Loss', linestyle='--')
|
| 342 |
+
plt.title('Model Loss Comparison')
|
| 343 |
+
plt.ylabel('Loss')
|
| 344 |
+
plt.xlabel('Epoch')
|
| 345 |
+
plt.legend()
|
| 346 |
+
plt.grid(True)
|
| 347 |
+
|
| 348 |
+
plt.tight_layout()
|
| 349 |
+
plt.savefig(os.path.join(PLOTS_FOLDER, '02_training_history.png'))
|
| 350 |
+
plt.show()
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
# ==============================================================================
|
| 354 |
+
# === PART 5: DECISION BOUNDARY VISUALIZATION ===
|
| 355 |
+
# ==============================================================================
|
| 356 |
+
|
| 357 |
+
def plot_decision_boundary_slice(model, X_data, y_data, z_value, title, transform_func=None):
|
| 358 |
+
"""
|
| 359 |
+
Visualizes the model's decision boundary on a 2D slice of the 3D space.
|
| 360 |
+
"""
|
| 361 |
+
fig, ax = plt.subplots(figsize=(8, 7))
|
| 362 |
+
|
| 363 |
+
# Create a grid of points in the XY plane
|
| 364 |
+
x_min, x_max = X_data[:, 0].min() - 1, X_data[:, 0].max() + 1
|
| 365 |
+
y_min, y_max = X_data[:, 1].min() - 1, X_data[:, 1].max() + 1
|
| 366 |
+
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 150),
|
| 367 |
+
np.linspace(y_min, y_max, 150))
|
| 368 |
+
|
| 369 |
+
# Create 3D points at the specified Z-level
|
| 370 |
+
grid_points_3d = np.c_[xx.ravel(), yy.ravel(), np.full_like(xx.ravel(), z_value)]
|
| 371 |
+
|
| 372 |
+
# Prepare data for the model (apply transform if necessary)
|
| 373 |
+
if transform_func:
|
| 374 |
+
grid_for_model = transform_func(grid_points_3d)
|
| 375 |
+
else:
|
| 376 |
+
grid_for_model = grid_points_3d
|
| 377 |
+
|
| 378 |
+
# Get model predictions
|
| 379 |
+
Z = model.predict(grid_for_model)
|
| 380 |
+
Z = Z.reshape(xx.shape)
|
| 381 |
+
|
| 382 |
+
# Plot the decision boundary
|
| 383 |
+
ax.contourf(xx, yy, Z, alpha=0.4, cmap='viridis')
|
| 384 |
+
|
| 385 |
+
# Scatter plot the actual data points near this Z-slice
|
| 386 |
+
slice_mask = np.abs(X_data[:, 2] - z_value) < 1.0 # Bins are 1.0 unit thick
|
| 387 |
+
ax.scatter(X_data[slice_mask, 0], X_data[slice_mask, 1], c=y_data[slice_mask],
|
| 388 |
+
s=20, edgecolor='k', cmap='viridis')
|
| 389 |
+
|
| 390 |
+
ax.set_title(title)
|
| 391 |
+
ax.set_xlabel('X Axis')
|
| 392 |
+
ax.set_ylabel('Y Axis')
|
| 393 |
+
plt.savefig(os.path.join(PLOTS_FOLDER, f"03_{title.replace(' ', '_').replace('=', '')}.png"))
|
| 394 |
+
plt.show()
|
| 395 |
+
|
| 396 |
+
print("\nVisualizing Decision Boundaries at different Z-levels...")
|
| 397 |
+
z_slices = [0, Z_BINS * 0.5, Z_BINS * 0.9]
|
| 398 |
+
|
| 399 |
+
for z_slice in z_slices:
|
| 400 |
+
# Model 1 (Informed)
|
| 401 |
+
plot_decision_boundary_slice(model_informed, X, y, z_slice,
|
| 402 |
+
title=f"Informed Model Boundary at Z={z_slice:.1f}",
|
| 403 |
+
transform_func=helix_feature_transform)
|
| 404 |
+
# Model 2 (Naive)
|
| 405 |
+
plot_decision_boundary_slice(model_naive, X, y, z_slice,
|
| 406 |
+
title=f"Naive Model Boundary at Z={z_slice:.1f}")
|
| 407 |
+
|
| 408 |
+
|
| 409 |
+
# ==============================================================================
|
| 410 |
+
# === PART 6: FINAL 3D VISUALIZATION OF CLASSIFICATION RESULTS ===
|
| 411 |
+
# ==============================================================================
|
| 412 |
+
|
| 413 |
+
def plot_3d_classification_results(model, X_test_raw, y_test, title, transform_func=None):
|
| 414 |
+
"""Plots a 3D scatter plot colored by correct/incorrect classification."""
|
| 415 |
+
|
| 416 |
+
# Prepare test data for the given model
|
| 417 |
+
if transform_func:
|
| 418 |
+
X_test_for_model = transform_func(X_test_raw)
|
| 419 |
+
else:
|
| 420 |
+
X_test_for_model = X_test_raw
|
| 421 |
+
|
| 422 |
+
# Get predictions
|
| 423 |
+
y_pred = (model.predict(X_test_for_model) > 0.5).astype("int32").flatten()
|
| 424 |
+
|
| 425 |
+
# Determine correct and incorrect classifications
|
| 426 |
+
correct_mask = (y_pred == y_test)
|
| 427 |
+
|
| 428 |
+
fig = plt.figure(figsize=(12, 10))
|
| 429 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 430 |
+
|
| 431 |
+
# Plot correctly classified points (green)
|
| 432 |
+
ax.scatter(X_test_raw[correct_mask, 0], X_test_raw[correct_mask, 1], X_test_raw[correct_mask, 2],
|
| 433 |
+
c='green', marker='.', alpha=0.5, label='Correct')
|
| 434 |
+
|
| 435 |
+
# Plot incorrectly classified points (red)
|
| 436 |
+
ax.scatter(X_test_raw[~correct_mask, 0], X_test_raw[~correct_mask, 1], X_test_raw[~correct_mask, 2],
|
| 437 |
+
c='red', marker='x', s=50, label='Incorrect')
|
| 438 |
+
|
| 439 |
+
ax.set_xlabel('X Axis')
|
| 440 |
+
ax.set_ylabel('Y Axis')
|
| 441 |
+
ax.set_zlabel('Z Axis')
|
| 442 |
+
ax.set_title(title)
|
| 443 |
+
ax.legend()
|
| 444 |
+
plt.savefig(os.path.join(PLOTS_FOLDER, f"04_{title.replace(' ', '_')}.png"))
|
| 445 |
+
plt.show()
|
| 446 |
+
|
| 447 |
+
print("\nVisualizing final classification results on the test set...")
|
| 448 |
+
|
| 449 |
+
# Use the 'naive' split's raw X_test_n for both plots to compare on the same data
|
| 450 |
+
plot_3d_classification_results(model_informed, X_test_n, y_test_n,
|
| 451 |
+
title="Informed Model Classification Results",
|
| 452 |
+
transform_func=helix_feature_transform)
|
| 453 |
+
|
| 454 |
+
plot_3d_classification_results(model_naive, X_test_n, y_test_n,
|
| 455 |
+
title="Naive Model Classification Results")
|
| 456 |
+
|
| 457 |
+
# ==============================================================================
|
| 458 |
+
# === PART 7: FINAL SUMMARY & CONCLUSION ===
|
| 459 |
+
# ==============================================================================
|
| 460 |
+
|
| 461 |
+
print("\n\n" + "="*50)
|
| 462 |
+
print("=== FINAL CONCLUSION ===")
|
| 463 |
+
print("="*50)
|
| 464 |
+
print(f"""
|
| 465 |
+
This experiment clearly demonstrates the critical role of feature engineering.
|
| 466 |
+
|
| 467 |
+
MODEL 1 (Informed Model):
|
| 468 |
+
- Accuracy: {acc_i:.4f}
|
| 469 |
+
- How it works: We transformed the (x, y, z) coordinates into a new feature
|
| 470 |
+
space: [distance_to_helix_1, distance_to_helix_2]. In this space, the
|
| 471 |
+
problem becomes trivial. A point is Class 0 if its distance to helix 1
|
| 472 |
+
is small, and Class 1 if its distance to helix 2 is small.
|
| 473 |
+
- Result: The model achieved near-perfect accuracy because the data became
|
| 474 |
+
linearly separable. The decision boundary visualizations show a perfect
|
| 475 |
+
circular separator at every Z-level, proving the model generalized perfectly.
|
| 476 |
+
|
| 477 |
+
MODEL 2 (Naive Model):
|
| 478 |
+
- Accuracy: {acc_n:.4f}
|
| 479 |
+
- How it works: This standard MLP was given only the raw (x, y, z) data.
|
| 480 |
+
It tried to find a complex, 3D surface to separate the two twisting helices.
|
| 481 |
+
- Result: The model struggled significantly. While its accuracy is better
|
| 482 |
+
than random guessing, it's far from perfect. The decision boundary plots
|
| 483 |
+
show that it learned strange, contorted shapes that only work for the Z-levels
|
| 484 |
+
it was trained on. It completely failed to learn the underlying rotational
|
| 485 |
+
geometry and did not generalize well.
|
| 486 |
+
|
| 487 |
+
ANSWER TO THE CORE QUESTION:
|
| 488 |
+
High accuracy classification over an arbitrary range of Z is accomplished
|
| 489 |
+
by transforming the input coordinates into a feature space that reflects the
|
| 490 |
+
inherent geometry of the problem, effectively "unrolling" the helices and
|
| 491 |
+
making the classes easily separable.
|
| 492 |
+
""")
|
| 493 |
+
|
| 494 |
+
# ==============================================================================
|
| 495 |
+
# === PART 8: ZIP RESULTS FOR GOOGLE COLAB ===
|
| 496 |
+
# ==============================================================================
|
| 497 |
+
|
| 498 |
+
def zip_results_for_colab(plots_folder, dataset_path):
|
| 499 |
+
"""Zips all generated plot files and the dataset for easy download in Colab."""
|
| 500 |
+
zip_filename = "double_helix_nn_results.zip"
|
| 501 |
+
files_to_zip = []
|
| 502 |
+
|
| 503 |
+
# Add all plots from the plots folder
|
| 504 |
+
for filename in os.listdir(plots_folder):
|
| 505 |
+
if filename.endswith(".png"):
|
| 506 |
+
files_to_zip.append(os.path.join(plots_folder, filename))
|
| 507 |
+
|
| 508 |
+
# Add the dataset file
|
| 509 |
+
if os.path.exists(dataset_path):
|
| 510 |
+
files_to_zip.append(dataset_path)
|
| 511 |
+
|
| 512 |
+
print(f"\nZipping {len(files_to_zip)} result files into '{zip_filename}'...")
|
| 513 |
+
with zipfile.ZipFile(zip_filename, 'w') as zf:
|
| 514 |
+
for file in files_to_zip:
|
| 515 |
+
zf.write(file, os.path.basename(file))
|
| 516 |
+
|
| 517 |
+
print("Zipping complete. Triggering download...")
|
| 518 |
+
from google.colab import files
|
| 519 |
+
files.download(zip_filename)
|
| 520 |
+
|
| 521 |
+
if IN_COLAB:
|
| 522 |
+
zip_results_for_colab(PLOTS_FOLDER, DATASET_PATH)
|