v0 trial
Browse files- NdR_disease.py +242 -0
- NdR_female_superheros.py +196 -0
- NdR_male_superheros.py +196 -0
- app.py +3 -107
NdR_disease.py
ADDED
@@ -0,0 +1,242 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
import torch.nn as nn
|
5 |
+
import torch.optim as optim
|
6 |
+
from sklearn.utils import shuffle
|
7 |
+
from sklearn.preprocessing import StandardScaler
|
8 |
+
from sklearn.model_selection import train_test_split
|
9 |
+
|
10 |
+
# Set random seed for reproducibility
|
11 |
+
np.random.seed(42)
|
12 |
+
torch.manual_seed(42)
|
13 |
+
|
14 |
+
def run_disease_train()
|
15 |
+
# Number of samples per condition
|
16 |
+
N_per_class = 500
|
17 |
+
|
18 |
+
# List of conditions (classes)
|
19 |
+
conditions = ['Common Cold', 'Seasonal Allergies', 'Migraine', 'Gastroenteritis', 'Tension Headache']
|
20 |
+
|
21 |
+
# Total number of classes
|
22 |
+
num_classes = len(conditions)
|
23 |
+
|
24 |
+
# Total number of samples
|
25 |
+
N = N_per_class * num_classes
|
26 |
+
|
27 |
+
# Number of original features
|
28 |
+
D = 10 # Number of symptoms/features
|
29 |
+
|
30 |
+
# Update the total number of features after adding interaction terms
|
31 |
+
total_features = D + 2 # Original features plus two interaction terms
|
32 |
+
|
33 |
+
# Initialize feature matrix X and label vector y
|
34 |
+
X = np.zeros((N, total_features))
|
35 |
+
y = np.zeros(N, dtype=int)
|
36 |
+
|
37 |
+
# Define the mean and standard deviation for each feature per condition
|
38 |
+
# Features: [Fever, Cough, Sneezing, Runny Nose, Nausea, Vomiting, Diarrhea, Headache, Fatigue, Stress Level]
|
39 |
+
condition_stats = {
|
40 |
+
'Common Cold': {
|
41 |
+
'mean': [1, 6, 7, 8, 1, 1, 1, 5, 5, 5],
|
42 |
+
'std': [1.5, 2, 2, 2, 1.5, 1.5, 1.5, 2, 2, 2]
|
43 |
+
},
|
44 |
+
'Seasonal Allergies': {
|
45 |
+
'mean': [0, 3, 8, 9, 1, 1, 1, 4, 4, 6],
|
46 |
+
'std': [1.5, 2, 2, 2, 1.5, 1.5, 1.5, 2, 2, 2]
|
47 |
+
},
|
48 |
+
'Migraine': {
|
49 |
+
'mean': [0, 1, 1, 1, 2, 2, 2, 8, 7, 8],
|
50 |
+
'std': [1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 2, 2, 2]
|
51 |
+
},
|
52 |
+
'Gastroenteritis': {
|
53 |
+
'mean': [2, 2, 1, 1, 7, 6, 8, 5, 6, 5],
|
54 |
+
'std': [1.5, 2, 1.5, 1.5, 2, 2, 2, 2, 2, 2]
|
55 |
+
},
|
56 |
+
'Tension Headache': {
|
57 |
+
'mean': [0, 1, 1, 1, 1, 1, 1, 6, 5, 8],
|
58 |
+
'std': [1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 2, 2, 2]
|
59 |
+
},
|
60 |
+
}
|
61 |
+
|
62 |
+
# Generate synthetic data for each condition
|
63 |
+
for idx, condition in enumerate(conditions):
|
64 |
+
start = idx * N_per_class
|
65 |
+
end = (idx + 1) * N_per_class
|
66 |
+
means = condition_stats[condition]['mean']
|
67 |
+
stds = condition_stats[condition]['std']
|
68 |
+
X_condition = np.random.normal(means, stds, (N_per_class, D))
|
69 |
+
# Ensure feature values are within reasonable ranges
|
70 |
+
X_condition = np.clip(X_condition, 0, 10)
|
71 |
+
# Introduce non-linear feature interactions
|
72 |
+
interaction_term = np.sin(X_condition[:, 7]) * np.log1p(X_condition[:, 9]) # Headache and Stress Level
|
73 |
+
interaction_term2 = X_condition[:, 0] * X_condition[:, 4] # Fever * Nausea
|
74 |
+
X_condition = np.hstack((X_condition, interaction_term.reshape(-1, 1), interaction_term2.reshape(-1, 1)))
|
75 |
+
X[start:end] = X_condition
|
76 |
+
y[start:end] = idx
|
77 |
+
|
78 |
+
# Shuffle the dataset
|
79 |
+
X, y = shuffle(X, y, random_state=42)
|
80 |
+
|
81 |
+
# Normalize the features
|
82 |
+
scaler = StandardScaler()
|
83 |
+
X_scaled = scaler.fit_transform(X)
|
84 |
+
|
85 |
+
# Split data into training and test sets
|
86 |
+
|
87 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
88 |
+
X_scaled, y, test_size=0.2, random_state=42
|
89 |
+
)
|
90 |
+
|
91 |
+
# Convert data to torch tensors
|
92 |
+
X_train_tensor = torch.from_numpy(X_train).float()
|
93 |
+
y_train_tensor = torch.from_numpy(y_train).long()
|
94 |
+
X_test_tensor = torch.from_numpy(X_test).float()
|
95 |
+
y_test_tensor = torch.from_numpy(y_test).long()
|
96 |
+
|
97 |
+
# Random prediction function
|
98 |
+
def random_prediction(num_samples):
|
99 |
+
random_preds = np.random.randint(num_classes, size=num_samples)
|
100 |
+
return random_preds
|
101 |
+
|
102 |
+
# Random prediction and evaluation
|
103 |
+
random_preds = random_prediction(len(y_test))
|
104 |
+
random_accuracy = (random_preds == y_test).sum() / y_test.size
|
105 |
+
|
106 |
+
# Define Linear Model
|
107 |
+
class LinearModel(nn.Module):
|
108 |
+
def __init__(self, input_dim, output_dim):
|
109 |
+
super(LinearModel, self).__init__()
|
110 |
+
self.linear = nn.Linear(input_dim, output_dim)
|
111 |
+
|
112 |
+
def forward(self, x):
|
113 |
+
return self.linear(x)
|
114 |
+
|
115 |
+
# Initialize Linear Model
|
116 |
+
input_dim = total_features
|
117 |
+
output_dim = num_classes
|
118 |
+
linear_model = LinearModel(input_dim, output_dim)
|
119 |
+
|
120 |
+
# Loss and optimizer for Linear Model
|
121 |
+
criterion = nn.CrossEntropyLoss()
|
122 |
+
optimizer = optim.SGD(linear_model.parameters(), lr=0.01, weight_decay=1e-4)
|
123 |
+
|
124 |
+
# Training the Linear Model
|
125 |
+
num_epochs = 50
|
126 |
+
for epoch in range(num_epochs):
|
127 |
+
linear_model.train()
|
128 |
+
outputs = linear_model(X_train_tensor)
|
129 |
+
loss = criterion(outputs, y_train_tensor)
|
130 |
+
optimizer.zero_grad()
|
131 |
+
loss.backward()
|
132 |
+
optimizer.step()
|
133 |
+
if (epoch + 1) % 10 == 0:
|
134 |
+
st.write('Linear Model - Epoch [{}/{}], Loss: {:.4f}'.format(
|
135 |
+
epoch + 1, num_epochs, loss.item()))
|
136 |
+
|
137 |
+
# Evaluate Linear Model
|
138 |
+
linear_model.eval()
|
139 |
+
with torch.no_grad():
|
140 |
+
outputs = linear_model(X_test_tensor)
|
141 |
+
_, predicted = torch.max(outputs.data, 1)
|
142 |
+
linear_accuracy = (predicted == y_test_tensor).sum().item() / y_test_tensor.size(0)
|
143 |
+
|
144 |
+
# Define Neural Network Model with regularization
|
145 |
+
class NeuralNet(nn.Module):
|
146 |
+
def __init__(self, input_dim, hidden_dims, output_dim):
|
147 |
+
super(NeuralNet, self).__init__()
|
148 |
+
layers = []
|
149 |
+
in_dim = input_dim
|
150 |
+
for h_dim in hidden_dims:
|
151 |
+
layers.append(nn.Linear(in_dim, h_dim))
|
152 |
+
layers.append(nn.ReLU())
|
153 |
+
layers.append(nn.BatchNorm1d(h_dim))
|
154 |
+
layers.append(nn.Dropout(0.5))
|
155 |
+
in_dim = h_dim
|
156 |
+
layers.append(nn.Linear(in_dim, output_dim))
|
157 |
+
self.model = nn.Sequential(*layers)
|
158 |
+
|
159 |
+
def forward(self, x):
|
160 |
+
return self.model(x)
|
161 |
+
|
162 |
+
# Initialize Neural Network Model
|
163 |
+
hidden_dims = [256, 128, 64]
|
164 |
+
neural_model = NeuralNet(input_dim, hidden_dims, output_dim)
|
165 |
+
|
166 |
+
# Loss and optimizer for Neural Network Model
|
167 |
+
criterion = nn.CrossEntropyLoss()
|
168 |
+
optimizer = optim.Adam(neural_model.parameters(), lr=0.001, weight_decay=1e-4)
|
169 |
+
|
170 |
+
# Training the Neural Network Model
|
171 |
+
num_epochs = 300
|
172 |
+
for epoch in range(num_epochs):
|
173 |
+
neural_model.train()
|
174 |
+
outputs = neural_model(X_train_tensor)
|
175 |
+
loss = criterion(outputs, y_train_tensor)
|
176 |
+
optimizer.zero_grad()
|
177 |
+
loss.backward()
|
178 |
+
optimizer.step()
|
179 |
+
if (epoch + 1) % 30 == 0:
|
180 |
+
st.write('Neural Network - Epoch [{}/{}], Loss: {:.4f}'.format(
|
181 |
+
epoch + 1, num_epochs, loss.item()))
|
182 |
+
|
183 |
+
# Evaluate Neural Network Model
|
184 |
+
neural_model.eval()
|
185 |
+
with torch.no_grad():
|
186 |
+
outputs = neural_model(X_test_tensor)
|
187 |
+
_, predicted = torch.max(outputs.data, 1)
|
188 |
+
neural_accuracy = (predicted == y_test_tensor).sum().item() / y_test_tensor.size(0)
|
189 |
+
|
190 |
+
# Summary of Accuracies
|
191 |
+
st.write("\nSummary of Accuracies:....")
|
192 |
+
st.write(f'Random Prediction Accuracy: {random_accuracy * 100:.2f}%')
|
193 |
+
st.write(f'Linear Model Accuracy: {linear_accuracy * 100:.2f}%')
|
194 |
+
st.write(f'Neural Network Model Accuracy: {neural_accuracy * 100:.2f}%')
|
195 |
+
|
196 |
+
return linear_model, neural_model, scaler, conditions, num_classes
|
197 |
+
|
198 |
+
# Function to get user input and make predictions
|
199 |
+
def get_user_input_and_predict(linear_model, neural_model, scaler, conditions, num_classes):
|
200 |
+
st.write("\nAdjust the sliders for the following symptoms on a scale from 0 (none) to 10 (severe):")
|
201 |
+
|
202 |
+
# Feature names
|
203 |
+
feature_names = ['Fever', 'Cough', 'Sneezing', 'Runny Nose', 'Nausea', 'Vomiting',
|
204 |
+
'Diarrhea', 'Headache', 'Fatigue', 'Stress Level']
|
205 |
+
|
206 |
+
# Create sliders for user input
|
207 |
+
user_features = []
|
208 |
+
for feature in feature_names:
|
209 |
+
value = st.slider(feature, 0, 10, 5) # Default value set to 5
|
210 |
+
user_features.append(value)
|
211 |
+
|
212 |
+
# Calculate interaction terms
|
213 |
+
interaction_term = np.sin(user_features[7]) * np.log1p(user_features[9]) # Headache and Stress Level
|
214 |
+
interaction_term2 = user_features[0] * user_features[4] # Fever * Nausea
|
215 |
+
user_features.extend([interaction_term, interaction_term2])
|
216 |
+
|
217 |
+
# Normalize features
|
218 |
+
user_features = scaler.transform([user_features])
|
219 |
+
user_tensor = torch.from_numpy(user_features).float()
|
220 |
+
|
221 |
+
# Random prediction
|
222 |
+
random_pred = np.random.randint(num_classes)
|
223 |
+
st.write(f"\nRandom Prediction: {conditions[random_pred]}")
|
224 |
+
|
225 |
+
# Linear Model Prediction
|
226 |
+
linear_model.eval()
|
227 |
+
with torch.no_grad():
|
228 |
+
outputs = linear_model(user_tensor)
|
229 |
+
_, predicted = torch.max(outputs.data, 1)
|
230 |
+
linear_pred = predicted.item()
|
231 |
+
st.write(f"Linear Model Prediction: {conditions[linear_pred]}")
|
232 |
+
|
233 |
+
# Neural Network Prediction
|
234 |
+
neural_model.eval()
|
235 |
+
with torch.no_grad():
|
236 |
+
outputs = neural_model(user_tensor)
|
237 |
+
_, predicted = torch.max(outputs.data, 1)
|
238 |
+
neural_pred = predicted.item()
|
239 |
+
st.write(f"Neural Network Prediction: {conditions[neural_pred]}")
|
240 |
+
|
241 |
+
linear_model, neural_model, scaler, conditions, num_classes = run_disease_train()
|
242 |
+
get_user_input_and_predict(linear_model, neural_model, scaler, conditions, num_classes)
|
NdR_female_superheros.py
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.optim as optim
|
5 |
+
from sklearn.utils import shuffle
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
from sklearn.preprocessing import StandardScaler
|
8 |
+
|
9 |
+
# Set random seed for reproducibility
|
10 |
+
np.random.seed(42)
|
11 |
+
torch.manual_seed(42)
|
12 |
+
|
13 |
+
# Number of samples per superhero
|
14 |
+
N_per_class = 200
|
15 |
+
|
16 |
+
# List of female superheroes
|
17 |
+
superheroes = ['Wonder Woman', 'Captain Marvel', 'Black Widow', 'Storm', 'Supergirl']
|
18 |
+
|
19 |
+
# Total number of classes
|
20 |
+
num_classes = len(superheroes)
|
21 |
+
|
22 |
+
# Total number of samples
|
23 |
+
N = N_per_class * num_classes
|
24 |
+
|
25 |
+
# Number of original features
|
26 |
+
D = 5 # Strength, Speed, Intelligence, Durability, Energy Projection
|
27 |
+
|
28 |
+
# Update the total number of features after adding the interaction term
|
29 |
+
total_features = D + 1 # Original features plus the interaction term
|
30 |
+
|
31 |
+
# Initialize feature matrix X and label vector y
|
32 |
+
X = np.zeros((N, total_features))
|
33 |
+
y = np.zeros(N, dtype=int)
|
34 |
+
|
35 |
+
# Define the mean and standard deviation for each feature per superhero
|
36 |
+
# Features: [Strength, Speed, Intelligence, Durability, Energy Projection]
|
37 |
+
superhero_stats = {
|
38 |
+
'Wonder Woman': {
|
39 |
+
'mean': [9, 9, 8, 9, 8],
|
40 |
+
'std': [0.5, 0.5, 0.5, 0.5, 0.5]
|
41 |
+
},
|
42 |
+
'Captain Marvel': {
|
43 |
+
'mean': [10, 9, 7, 10, 10],
|
44 |
+
'std': [0.5, 0.5, 0.5, 0.5, 0.5]
|
45 |
+
},
|
46 |
+
'Black Widow': {
|
47 |
+
'mean': [5, 7, 8, 6, 2],
|
48 |
+
'std': [0.5, 0.5, 0.5, 0.5, 0.5]
|
49 |
+
},
|
50 |
+
'Storm': {
|
51 |
+
'mean': [6, 7, 8, 6, 9],
|
52 |
+
'std': [0.5, 0.5, 0.5, 0.5, 0.5]
|
53 |
+
},
|
54 |
+
'Supergirl': {
|
55 |
+
'mean': [10, 10, 8, 10, 9],
|
56 |
+
'std': [0.5, 0.5, 0.5, 0.5, 0.5]
|
57 |
+
},
|
58 |
+
}
|
59 |
+
|
60 |
+
# Generate synthetic data for each superhero with non-linear relationships
|
61 |
+
for idx, hero in enumerate(superheroes):
|
62 |
+
start = idx * N_per_class
|
63 |
+
end = (idx + 1) * N_per_class
|
64 |
+
means = superhero_stats[hero]['mean']
|
65 |
+
stds = superhero_stats[hero]['std']
|
66 |
+
X_hero = np.random.normal(means, stds, (N_per_class, D))
|
67 |
+
# Ensure feature values are within reasonable ranges before computing interaction
|
68 |
+
X_hero = np.clip(X_hero, 1, 10)
|
69 |
+
# Introduce non-linear feature interactions
|
70 |
+
interaction_term = np.sin(X_hero[:, 1]) * np.log(X_hero[:, 4]) # Interaction between Speed and Energy Projection
|
71 |
+
X_hero = np.hstack((X_hero, interaction_term.reshape(-1, 1)))
|
72 |
+
X[start:end] = X_hero
|
73 |
+
y[start:end] = idx
|
74 |
+
|
75 |
+
# Ensure all feature values are within reasonable ranges
|
76 |
+
X[:, :D] = np.clip(X[:, :D], 1, 10)
|
77 |
+
|
78 |
+
# Shuffle the dataset
|
79 |
+
X, y = shuffle(X, y, random_state=42)
|
80 |
+
|
81 |
+
# Normalize the features
|
82 |
+
scaler = StandardScaler()
|
83 |
+
X = scaler.fit_transform(X)
|
84 |
+
|
85 |
+
# Split data into training and test sets
|
86 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
87 |
+
X, y, test_size=0.2, random_state=42)
|
88 |
+
|
89 |
+
# Convert data to torch tensors
|
90 |
+
X_train_tensor = torch.from_numpy(X_train).float()
|
91 |
+
y_train_tensor = torch.from_numpy(y_train).long()
|
92 |
+
X_test_tensor = torch.from_numpy(X_test).float()
|
93 |
+
y_test_tensor = torch.from_numpy(y_test).long()
|
94 |
+
|
95 |
+
# Random prediction function
|
96 |
+
def random_prediction(X):
|
97 |
+
num_samples = X.shape[0]
|
98 |
+
random_preds = np.random.randint(num_classes, size=num_samples)
|
99 |
+
return random_preds
|
100 |
+
|
101 |
+
# Random prediction and evaluation
|
102 |
+
random_preds = random_prediction(X_test)
|
103 |
+
random_accuracy = (random_preds == y_test).sum() / y_test.size
|
104 |
+
print('Random Prediction Accuracy: {:.2f}%'.format(100 * random_accuracy))
|
105 |
+
|
106 |
+
# Define Linear Model
|
107 |
+
class LinearModel(nn.Module):
|
108 |
+
def __init__(self, input_dim, output_dim):
|
109 |
+
super(LinearModel, self).__init__()
|
110 |
+
self.linear = nn.Linear(input_dim, output_dim)
|
111 |
+
|
112 |
+
def forward(self, x):
|
113 |
+
return self.linear(x)
|
114 |
+
|
115 |
+
# Initialize Linear Model
|
116 |
+
input_dim = total_features
|
117 |
+
output_dim = num_classes
|
118 |
+
linear_model = LinearModel(input_dim, output_dim)
|
119 |
+
|
120 |
+
# Loss and optimizer for Linear Model
|
121 |
+
criterion = nn.CrossEntropyLoss()
|
122 |
+
optimizer = optim.SGD(linear_model.parameters(), lr=0.01, weight_decay=1e-4)
|
123 |
+
|
124 |
+
# Training the Linear Model
|
125 |
+
num_epochs = 100
|
126 |
+
for epoch in range(num_epochs):
|
127 |
+
linear_model.train()
|
128 |
+
outputs = linear_model(X_train_tensor)
|
129 |
+
loss = criterion(outputs, y_train_tensor)
|
130 |
+
optimizer.zero_grad()
|
131 |
+
loss.backward()
|
132 |
+
optimizer.step()
|
133 |
+
if (epoch + 1) % 20 == 0:
|
134 |
+
print('Linear Model - Epoch [{}/{}], Loss: {:.4f}'.format(
|
135 |
+
epoch + 1, num_epochs, loss.item()))
|
136 |
+
|
137 |
+
# Evaluate Linear Model
|
138 |
+
linear_model.eval()
|
139 |
+
with torch.no_grad():
|
140 |
+
outputs = linear_model(X_test_tensor)
|
141 |
+
_, predicted = torch.max(outputs.data, 1)
|
142 |
+
linear_accuracy = (predicted == y_test_tensor).sum().item() / y_test_tensor.size(0)
|
143 |
+
print('Linear Model Accuracy: {:.2f}%'.format(100 * linear_accuracy))
|
144 |
+
|
145 |
+
# Define Neural Network Model with regularization
|
146 |
+
class NeuralNet(nn.Module):
|
147 |
+
def __init__(self, input_dim, hidden_dims, output_dim):
|
148 |
+
super(NeuralNet, self).__init__()
|
149 |
+
layers = []
|
150 |
+
in_dim = input_dim
|
151 |
+
for h_dim in hidden_dims:
|
152 |
+
layers.append(nn.Linear(in_dim, h_dim))
|
153 |
+
layers.append(nn.ReLU())
|
154 |
+
layers.append(nn.BatchNorm1d(h_dim))
|
155 |
+
layers.append(nn.Dropout(0.3))
|
156 |
+
in_dim = h_dim
|
157 |
+
layers.append(nn.Linear(in_dim, output_dim))
|
158 |
+
self.model = nn.Sequential(*layers)
|
159 |
+
|
160 |
+
def forward(self, x):
|
161 |
+
return self.model(x)
|
162 |
+
|
163 |
+
# Initialize Neural Network Model
|
164 |
+
hidden_dims = [128, 64, 32]
|
165 |
+
neural_model = NeuralNet(input_dim, hidden_dims, output_dim)
|
166 |
+
|
167 |
+
# Loss and optimizer for Neural Network Model
|
168 |
+
criterion = nn.CrossEntropyLoss()
|
169 |
+
optimizer = optim.Adam(neural_model.parameters(), lr=0.001, weight_decay=1e-4)
|
170 |
+
|
171 |
+
# Training the Neural Network Model
|
172 |
+
num_epochs = 200
|
173 |
+
for epoch in range(num_epochs):
|
174 |
+
neural_model.train()
|
175 |
+
outputs = neural_model(X_train_tensor)
|
176 |
+
loss = criterion(outputs, y_train_tensor)
|
177 |
+
optimizer.zero_grad()
|
178 |
+
loss.backward()
|
179 |
+
optimizer.step()
|
180 |
+
if (epoch + 1) % 20 == 0:
|
181 |
+
print('Neural Network - Epoch [{}/{}], Loss: {:.4f}'.format(
|
182 |
+
epoch + 1, num_epochs, loss.item()))
|
183 |
+
|
184 |
+
# Evaluate Neural Network Model
|
185 |
+
neural_model.eval()
|
186 |
+
with torch.no_grad():
|
187 |
+
outputs = neural_model(X_test_tensor)
|
188 |
+
_, predicted = torch.max(outputs.data, 1)
|
189 |
+
neural_accuracy = (predicted == y_test_tensor).sum().item() / y_test_tensor.size(0)
|
190 |
+
print('Neural Network Model Accuracy: {:.2f}%'.format(100 * neural_accuracy))
|
191 |
+
|
192 |
+
# Summary of Accuracies
|
193 |
+
print("\nSummary of Accuracies:")
|
194 |
+
print('Random Prediction Accuracy: {:.2f}%'.format(100 * random_accuracy))
|
195 |
+
print('Linear Model Accuracy: {:.2f}%'.format(100 * linear_accuracy))
|
196 |
+
print('Neural Network Model Accuracy: {:.2f}%'.format(100 * neural_accuracy))
|
NdR_male_superheros.py
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
import torch.optim as optim
|
5 |
+
from sklearn.utils import shuffle
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
from sklearn.preprocessing import StandardScaler
|
8 |
+
|
9 |
+
# Set random seed for reproducibility
|
10 |
+
np.random.seed(42)
|
11 |
+
torch.manual_seed(42)
|
12 |
+
|
13 |
+
# Number of samples per superhero
|
14 |
+
N_per_class = 200
|
15 |
+
|
16 |
+
# List of superheroes
|
17 |
+
superheroes = ['Iron Man', 'Hulk', 'Flash', 'Batman', 'Thor']
|
18 |
+
|
19 |
+
# Total number of classes
|
20 |
+
num_classes = len(superheroes)
|
21 |
+
|
22 |
+
# Total number of samples
|
23 |
+
N = N_per_class * num_classes
|
24 |
+
|
25 |
+
# Number of original features
|
26 |
+
D = 5 # Strength, Speed, Intelligence, Durability, Energy Projection
|
27 |
+
|
28 |
+
# Update the total number of features after adding the interaction term
|
29 |
+
total_features = D + 1 # Original features plus the interaction term
|
30 |
+
|
31 |
+
# Initialize feature matrix X and label vector y
|
32 |
+
X = np.zeros((N, total_features))
|
33 |
+
y = np.zeros(N, dtype=int)
|
34 |
+
|
35 |
+
# Define the mean and standard deviation for each feature per superhero
|
36 |
+
# Features: [Strength, Speed, Intelligence, Durability, Energy Projection]
|
37 |
+
superhero_stats = {
|
38 |
+
'Iron Man': {
|
39 |
+
'mean': [7, 7, 9, 8, 8],
|
40 |
+
'std': [0.5, 0.5, 0.2, 0.5, 0.5]
|
41 |
+
},
|
42 |
+
'Hulk': {
|
43 |
+
'mean': [10, 5, 3, 10, 2],
|
44 |
+
'std': [0.5, 0.5, 0.2, 0.5, 0.5]
|
45 |
+
},
|
46 |
+
'Flash': {
|
47 |
+
'mean': [4, 10, 6, 5, 3],
|
48 |
+
'std': [0.5, 0.5, 0.2, 0.5, 0.5]
|
49 |
+
},
|
50 |
+
'Batman': {
|
51 |
+
'mean': [5, 6, 9, 6, 2],
|
52 |
+
'std': [0.5, 0.5, 0.2, 0.5, 0.5]
|
53 |
+
},
|
54 |
+
'Thor': {
|
55 |
+
'mean': [10, 8, 7, 10, 9],
|
56 |
+
'std': [0.5, 0.5, 0.2, 0.5, 0.5]
|
57 |
+
},
|
58 |
+
}
|
59 |
+
|
60 |
+
# Generate synthetic data for each superhero with non-linear relationships
|
61 |
+
for idx, hero in enumerate(superheroes):
|
62 |
+
start = idx * N_per_class
|
63 |
+
end = (idx + 1) * N_per_class
|
64 |
+
means = superhero_stats[hero]['mean']
|
65 |
+
stds = superhero_stats[hero]['std']
|
66 |
+
X_hero = np.random.normal(means, stds, (N_per_class, D))
|
67 |
+
# Ensure feature values are within reasonable ranges before computing interaction
|
68 |
+
X_hero = np.clip(X_hero, 1, 10)
|
69 |
+
# Introduce non-linear feature interactions
|
70 |
+
interaction_term = np.sin(X_hero[:, 0]) * np.log(X_hero[:, 2])
|
71 |
+
X_hero = np.hstack((X_hero, interaction_term.reshape(-1, 1)))
|
72 |
+
X[start:end] = X_hero
|
73 |
+
y[start:end] = idx
|
74 |
+
|
75 |
+
# Ensure all feature values are within reasonable ranges
|
76 |
+
X[:, :D] = np.clip(X[:, :D], 1, 10)
|
77 |
+
|
78 |
+
# Shuffle the dataset
|
79 |
+
X, y = shuffle(X, y, random_state=42)
|
80 |
+
|
81 |
+
# Normalize the features
|
82 |
+
scaler = StandardScaler()
|
83 |
+
X = scaler.fit_transform(X)
|
84 |
+
|
85 |
+
# Split data into training and test sets
|
86 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
87 |
+
X, y, test_size=0.2, random_state=42)
|
88 |
+
|
89 |
+
# Convert data to torch tensors
|
90 |
+
X_train_tensor = torch.from_numpy(X_train).float()
|
91 |
+
y_train_tensor = torch.from_numpy(y_train).long()
|
92 |
+
X_test_tensor = torch.from_numpy(X_test).float()
|
93 |
+
y_test_tensor = torch.from_numpy(y_test).long()
|
94 |
+
|
95 |
+
# Random prediction function
|
96 |
+
def random_prediction(X):
|
97 |
+
num_samples = X.shape[0]
|
98 |
+
random_preds = np.random.randint(num_classes, size=num_samples)
|
99 |
+
return random_preds
|
100 |
+
|
101 |
+
# Random prediction and evaluation
|
102 |
+
random_preds = random_prediction(X_test)
|
103 |
+
random_accuracy = (random_preds == y_test).sum() / y_test.size
|
104 |
+
print('Random Prediction Accuracy: {:.2f}%'.format(100 * random_accuracy))
|
105 |
+
|
106 |
+
# Define Linear Model
|
107 |
+
class LinearModel(nn.Module):
|
108 |
+
def __init__(self, input_dim, output_dim):
|
109 |
+
super(LinearModel, self).__init__()
|
110 |
+
self.linear = nn.Linear(input_dim, output_dim)
|
111 |
+
|
112 |
+
def forward(self, x):
|
113 |
+
return self.linear(x)
|
114 |
+
|
115 |
+
# Initialize Linear Model
|
116 |
+
input_dim = total_features
|
117 |
+
output_dim = num_classes
|
118 |
+
linear_model = LinearModel(input_dim, output_dim)
|
119 |
+
|
120 |
+
# Loss and optimizer for Linear Model
|
121 |
+
criterion = nn.CrossEntropyLoss()
|
122 |
+
optimizer = optim.SGD(linear_model.parameters(), lr=0.01, weight_decay=1e-4)
|
123 |
+
|
124 |
+
# Training the Linear Model
|
125 |
+
num_epochs = 100
|
126 |
+
for epoch in range(num_epochs):
|
127 |
+
linear_model.train()
|
128 |
+
outputs = linear_model(X_train_tensor)
|
129 |
+
loss = criterion(outputs, y_train_tensor)
|
130 |
+
optimizer.zero_grad()
|
131 |
+
loss.backward()
|
132 |
+
optimizer.step()
|
133 |
+
if (epoch + 1) % 20 == 0:
|
134 |
+
print('Linear Model - Epoch [{}/{}], Loss: {:.4f}'.format(
|
135 |
+
epoch + 1, num_epochs, loss.item()))
|
136 |
+
|
137 |
+
# Evaluate Linear Model
|
138 |
+
linear_model.eval()
|
139 |
+
with torch.no_grad():
|
140 |
+
outputs = linear_model(X_test_tensor)
|
141 |
+
_, predicted = torch.max(outputs.data, 1)
|
142 |
+
linear_accuracy = (predicted == y_test_tensor).sum().item() / y_test_tensor.size(0)
|
143 |
+
print('Linear Model Accuracy: {:.2f}%'.format(100 * linear_accuracy))
|
144 |
+
|
145 |
+
# Define Neural Network Model with regularization
|
146 |
+
class NeuralNet(nn.Module):
|
147 |
+
def __init__(self, input_dim, hidden_dims, output_dim):
|
148 |
+
super(NeuralNet, self).__init__()
|
149 |
+
layers = []
|
150 |
+
in_dim = input_dim
|
151 |
+
for h_dim in hidden_dims:
|
152 |
+
layers.append(nn.Linear(in_dim, h_dim))
|
153 |
+
layers.append(nn.ReLU())
|
154 |
+
layers.append(nn.BatchNorm1d(h_dim))
|
155 |
+
layers.append(nn.Dropout(0.3))
|
156 |
+
in_dim = h_dim
|
157 |
+
layers.append(nn.Linear(in_dim, output_dim))
|
158 |
+
self.model = nn.Sequential(*layers)
|
159 |
+
|
160 |
+
def forward(self, x):
|
161 |
+
return self.model(x)
|
162 |
+
|
163 |
+
# Initialize Neural Network Model
|
164 |
+
hidden_dims = [128, 64, 32]
|
165 |
+
neural_model = NeuralNet(input_dim, hidden_dims, output_dim)
|
166 |
+
|
167 |
+
# Loss and optimizer for Neural Network Model
|
168 |
+
criterion = nn.CrossEntropyLoss()
|
169 |
+
optimizer = optim.Adam(neural_model.parameters(), lr=0.001, weight_decay=1e-4)
|
170 |
+
|
171 |
+
# Training the Neural Network Model
|
172 |
+
num_epochs = 200
|
173 |
+
for epoch in range(num_epochs):
|
174 |
+
neural_model.train()
|
175 |
+
outputs = neural_model(X_train_tensor)
|
176 |
+
loss = criterion(outputs, y_train_tensor)
|
177 |
+
optimizer.zero_grad()
|
178 |
+
loss.backward()
|
179 |
+
optimizer.step()
|
180 |
+
if (epoch + 1) % 20 == 0:
|
181 |
+
print('Neural Network - Epoch [{}/{}], Loss: {:.4f}'.format(
|
182 |
+
epoch + 1, num_epochs, loss.item()))
|
183 |
+
|
184 |
+
# Evaluate Neural Network Model
|
185 |
+
neural_model.eval()
|
186 |
+
with torch.no_grad():
|
187 |
+
outputs = neural_model(X_test_tensor)
|
188 |
+
_, predicted = torch.max(outputs.data, 1)
|
189 |
+
neural_accuracy = (predicted == y_test_tensor).sum().item() / y_test_tensor.size(0)
|
190 |
+
print('Neural Network Model Accuracy: {:.2f}%'.format(100 * neural_accuracy))
|
191 |
+
|
192 |
+
# Summary of Accuracies
|
193 |
+
print("\nSummary of Accuracies:")
|
194 |
+
print('Random Prediction Accuracy: {:.2f}%'.format(100 * random_accuracy))
|
195 |
+
print('Linear Model Accuracy: {:.2f}%'.format(100 * linear_accuracy))
|
196 |
+
print('Neural Network Model Accuracy: {:.2f}%'.format(100 * neural_accuracy))
|
app.py
CHANGED
@@ -8,112 +8,7 @@ from sklearn.preprocessing import StandardScaler
|
|
8 |
from sklearn.linear_model import LogisticRegression
|
9 |
from sklearn.model_selection import train_test_split
|
10 |
|
11 |
-
|
12 |
-
# Function for disease task
|
13 |
-
def run_disease_task():
|
14 |
-
# Number of samples per class
|
15 |
-
N_per_class = 500
|
16 |
-
|
17 |
-
# Number of classes
|
18 |
-
num_classes = 5
|
19 |
-
|
20 |
-
# Total number of samples
|
21 |
-
N = N_per_class * num_classes
|
22 |
-
|
23 |
-
# Number of features
|
24 |
-
D = 2 # For visualization purposes
|
25 |
-
|
26 |
-
# Initialize feature matrix X and label vector y
|
27 |
-
X = np.zeros((N, D))
|
28 |
-
y = np.zeros(N, dtype=int)
|
29 |
-
|
30 |
-
# Generate a multi-class spiral dataset
|
31 |
-
def generate_multi_class_spiral(points, classes):
|
32 |
-
X = np.zeros((points * classes, 2))
|
33 |
-
y = np.zeros(points * classes, dtype=int)
|
34 |
-
for class_number in range(classes):
|
35 |
-
ix = range(points * class_number, points * (class_number + 1))
|
36 |
-
r = np.linspace(0.0, 1, points)
|
37 |
-
t = np.linspace(class_number * 4, (class_number + 1) * 4, points) + np.random.randn(points) * 0.2
|
38 |
-
X[ix] = np.c_[r * np.sin(t), r * np.cos(t)]
|
39 |
-
y[ix] = class_number
|
40 |
-
return X, y
|
41 |
-
|
42 |
-
X, y = generate_multi_class_spiral(N_per_class, num_classes)
|
43 |
-
|
44 |
-
# Shuffle the dataset
|
45 |
-
X, y = shuffle(X, y, random_state=42)
|
46 |
-
|
47 |
-
# Normalize the features
|
48 |
-
scaler = StandardScaler()
|
49 |
-
X_scaled = scaler.fit_transform(X)
|
50 |
-
|
51 |
-
# Convert data to torch tensors
|
52 |
-
X_train_tensor = torch.from_numpy(X_scaled).float()
|
53 |
-
y_train_tensor = torch.from_numpy(y).long()
|
54 |
-
|
55 |
-
# Split data into training and test sets
|
56 |
-
X_train_tensor, X_test_tensor, y_train_tensor, y_test_tensor = train_test_split(
|
57 |
-
X_train_tensor, y_train_tensor, test_size=0.2, random_state=42
|
58 |
-
)
|
59 |
-
|
60 |
-
# Logistic Regression Model
|
61 |
-
linear_model = LogisticRegression(max_iter=200)
|
62 |
-
linear_model.fit(X_scaled[: int(0.8 * N)], y[: int(0.8 * N)])
|
63 |
-
|
64 |
-
# Linear model accuracy
|
65 |
-
linear_accuracy = linear_model.score(X_scaled[int(0.8 * N) :], y[int(0.8 * N) :])
|
66 |
-
|
67 |
-
# Neural Network Model
|
68 |
-
class NeuralNet(nn.Module):
|
69 |
-
def __init__(self, input_dim, hidden_dims, output_dim):
|
70 |
-
super(NeuralNet, self).__init__()
|
71 |
-
layers = []
|
72 |
-
in_dim = input_dim
|
73 |
-
for h_dim in hidden_dims:
|
74 |
-
layers.append(nn.Linear(in_dim, h_dim))
|
75 |
-
layers.append(nn.ReLU())
|
76 |
-
layers.append(nn.BatchNorm1d(h_dim))
|
77 |
-
layers.append(nn.Dropout(0.3))
|
78 |
-
in_dim = h_dim
|
79 |
-
layers.append(nn.Linear(in_dim, output_dim))
|
80 |
-
self.model = nn.Sequential(*layers)
|
81 |
-
|
82 |
-
def forward(self, x):
|
83 |
-
return self.model(x)
|
84 |
-
|
85 |
-
# Initialize Neural Network Model
|
86 |
-
hidden_dims = [128, 64, 32]
|
87 |
-
neural_model = NeuralNet(D, hidden_dims, num_classes)
|
88 |
-
|
89 |
-
# Loss and optimizer for Neural Network Model
|
90 |
-
criterion = nn.CrossEntropyLoss()
|
91 |
-
optimizer = optim.Adam(neural_model.parameters(), lr=0.001, weight_decay=1e-4)
|
92 |
-
|
93 |
-
# Training the Neural Network Model
|
94 |
-
num_epochs = 200
|
95 |
-
for epoch in range(num_epochs):
|
96 |
-
neural_model.train()
|
97 |
-
outputs = neural_model(X_train_tensor)
|
98 |
-
loss = criterion(outputs, y_train_tensor)
|
99 |
-
optimizer.zero_grad()
|
100 |
-
loss.backward()
|
101 |
-
optimizer.step()
|
102 |
-
if (epoch + 1) % 20 == 0:
|
103 |
-
st.write(f'Neural Network - Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item():.4f}')
|
104 |
-
|
105 |
-
# Evaluate Neural Network Model
|
106 |
-
neural_model.eval()
|
107 |
-
with torch.no_grad():
|
108 |
-
outputs = neural_model(X_test_tensor)
|
109 |
-
_, predicted = torch.max(outputs.data, 1)
|
110 |
-
neural_accuracy = (predicted == y_test_tensor).sum().item() / y_test_tensor.size(0)
|
111 |
-
st.write(f'Neural Network Model Accuracy: {neural_accuracy * 100:.2f}%')
|
112 |
-
|
113 |
-
# Summary of Accuracies
|
114 |
-
st.write("\nSummary of Accuracies:")
|
115 |
-
st.write(f'Linear Model Accuracy: {linear_accuracy * 100:.2f}%')
|
116 |
-
st.write(f'Neural Network Model Accuracy: {neural_accuracy * 100:.2f}%')
|
117 |
|
118 |
|
119 |
# Function for male superhero task
|
@@ -156,4 +51,5 @@ if task == "Superhero":
|
|
156 |
|
157 |
elif task == "Disease":
|
158 |
if st.button("Run Disease Task"):
|
159 |
-
|
|
|
|
8 |
from sklearn.linear_model import LogisticRegression
|
9 |
from sklearn.model_selection import train_test_split
|
10 |
|
11 |
+
from NdR_disease import run_disease_train, get_user_input_and_predict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
|
14 |
# Function for male superhero task
|
|
|
51 |
|
52 |
elif task == "Disease":
|
53 |
if st.button("Run Disease Task"):
|
54 |
+
linear_model, neural_model, scaler, conditions, num_classes = run_disease_train()
|
55 |
+
get_user_input_and_predict(linear_model, neural_model, scaler, conditions, num_classes)
|