-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-ml-download.py
More file actions
453 lines (375 loc) · 16.6 KB
/
python-ml-download.py
File metadata and controls
453 lines (375 loc) · 16.6 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
"""
Healthcare ML Analytics for predicting propensity to seek care
for mothers of children with birth defects
Requirements:
pip install pandas numpy scikit-learn xgboost matplotlib seaborn plotly
"""
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
import xgboost as xgb
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import warnings
warnings.filterwarnings('ignore')
class HealthcarePropensityAnalyzer:
"""
Healthcare ML Analytics for predicting propensity to seek care
for mothers of children with birth defects
"""
def __init__(self, random_state=42):
self.random_state = random_state
self.xgb_model = None
self.rf_model = None
self.label_encoders = {}
self.scaler = StandardScaler()
self.feature_names = ['age', 'income', 'insurance_encoded', 'race_encoded', 'location_encoded']
def generate_synthetic_data(self, n_samples=1000):
"""
Generate synthetic healthcare data with realistic correlations
reflecting real-world healthcare disparities
"""
np.random.seed(self.random_state)
# Define categorical variables
insurance_types = ['Private', 'Medicaid', 'Medicare', 'Uninsured']
races = ['White', 'Black', 'Hispanic', 'Asian', 'Other']
locations = ['Urban', 'Rural']
# Generate base demographics
data = {
'age': np.random.randint(18, 49, n_samples), # 18-48 years
'income': np.random.randint(20000, 101000, n_samples), # $20k-$100k
'insurance': np.random.choice(insurance_types, n_samples),
'race': np.random.choice(races, n_samples),
'location': np.random.choice(locations, n_samples)
}
df = pd.DataFrame(data)
# Calculate propensity with realistic correlations
propensity = np.full(n_samples, 50.0) # Base propensity
# Insurance impact (strongest predictor)
insurance_effects = {'Private': 20, 'Medicaid': 10, 'Medicare': 5, 'Uninsured': -25}
for i, insurance in enumerate(df['insurance']):
propensity[i] += insurance_effects[insurance]
# Age impact (older mothers tend to seek more care)
propensity += (df['age'] - 25) * 0.5
# Income impact
propensity += (df['income'] - 50000) / 2000
# Location impact (rural healthcare access challenges)
rural_penalty = df['location'].map({'Urban': 0, 'Rural': -8})
propensity += rural_penalty
# Race disparities (reflecting documented healthcare disparities)
race_effects = {'White': 0, 'Black': -5, 'Hispanic': -3, 'Asian': 2, 'Other': -2}
for i, race in enumerate(df['race']):
propensity[i] += race_effects[race]
# Add realistic noise and constrain to 1-100 range
noise = np.random.normal(0, 10, n_samples)
propensity = np.clip(propensity + noise, 1, 100)
df['propensity'] = np.round(propensity, 1)
return df
def preprocess_data(self, df):
"""
Encode categorical variables and prepare data for modeling
"""
df_processed = df.copy()
# Encode categorical variables
categorical_cols = ['insurance', 'race', 'location']
for col in categorical_cols:
le = LabelEncoder()
df_processed[f'{col}_encoded'] = le.fit_transform(df_processed[col])
self.label_encoders[col] = le
return df_processed
def prepare_features_target(self, df):
"""
Prepare feature matrix and target vector
"""
X = df[self.feature_names].copy()
y = df['propensity'].copy()
return X, y
def train_models(self, X, y, test_size=0.2):
"""
Train both XGBoost and Random Forest models
"""
# Split the data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=self.random_state
)
# Train XGBoost model
print("Training XGBoost model...")
self.xgb_model = xgb.XGBRegressor(
n_estimators=100,
max_depth=6,
learning_rate=0.1,
subsample=0.8,
colsample_bytree=0.8,
random_state=self.random_state,
objective='reg:squarederror'
)
self.xgb_model.fit(X_train, y_train)
# Train Random Forest model
print("Training Random Forest model...")
self.rf_model = RandomForestRegressor(
n_estimators=100,
max_depth=15,
min_samples_split=20,
min_samples_leaf=5,
bootstrap=True,
random_state=self.random_state,
oob_score=True
)
self.rf_model.fit(X_train, y_train)
# Store test data for evaluation
self.X_test = X_test
self.y_test = y_test
return X_train, X_test, y_train, y_test
def evaluate_models(self):
"""
Evaluate both models and return comprehensive metrics
"""
# XGBoost predictions
xgb_pred = self.xgb_model.predict(self.X_test)
# Random Forest predictions
rf_pred = self.rf_model.predict(self.X_test)
# Calculate metrics
metrics = {
'xgboost': {
'mse': mean_squared_error(self.y_test, xgb_pred),
'rmse': np.sqrt(mean_squared_error(self.y_test, xgb_pred)),
'mae': mean_absolute_error(self.y_test, xgb_pred),
'r2': r2_score(self.y_test, xgb_pred)
},
'random_forest': {
'mse': mean_squared_error(self.y_test, rf_pred),
'rmse': np.sqrt(mean_squared_error(self.y_test, rf_pred)),
'mae': mean_absolute_error(self.y_test, rf_pred),
'r2': r2_score(self.y_test, rf_pred),
'oob_score': self.rf_model.oob_score_
}
}
return metrics
def get_feature_importance(self):
"""
Extract feature importance from both models
"""
feature_names_readable = ['Age', 'Income', 'Insurance Type', 'Race', 'Location']
xgb_importance = self.xgb_model.feature_importances_
rf_importance = self.rf_model.feature_importances_
importance_df = pd.DataFrame({
'feature': feature_names_readable,
'xgboost_importance': xgb_importance,
'rf_importance': rf_importance
})
importance_df = importance_df.sort_values('xgboost_importance', ascending=False)
return importance_df
def analyze_disparities(self, df):
"""
Analyze healthcare disparities across different demographic groups
"""
disparities = {}
# Insurance disparities
insurance_stats = df.groupby('insurance')['propensity'].agg(['mean', 'std', 'count'])
disparities['insurance'] = insurance_stats
# Race disparities
race_stats = df.groupby('race')['propensity'].agg(['mean', 'std', 'count'])
disparities['race'] = race_stats
# Location disparities
location_stats = df.groupby('location')['propensity'].agg(['mean', 'std', 'count'])
disparities['location'] = location_stats
# Intersectional analysis
intersectional = df.groupby(['insurance', 'location', 'race'])['propensity'].mean().reset_index()
disparities['intersectional'] = intersectional
return disparities
def generate_xgboost_tree_data(self):
"""
Generate tree structure data for visualization
"""
# Simplified tree structure for visualization
tree_structure = {
"name": "Insurance = Private?",
"value": "n=1000",
"pred": "52.3",
"children": [
{
"name": "Income > 60k?",
"value": "n=340 (Yes)",
"pred": "71.2",
"children": [
{
"name": "Age > 30?",
"value": "n=180 (Y)",
"pred": "76.8",
"children": [
{"name": "78.2", "value": "Urban", "color": "#22c55e"},
{"name": "74.1", "value": "Rural", "color": "#3b82f6"}
]
},
{
"name": "Location",
"value": "n=160 (N)",
"pred": "64.9",
"children": [
{"name": "68.5", "value": "Urban", "color": "#06b6d4"},
{"name": "58.7", "value": "Rural", "color": "#8b5cf6"}
]
}
]
},
{
"name": "Race = White?",
"value": "n=660 (No)",
"pred": "41.8",
"children": [
{
"name": "Location",
"value": "n=420 (Y)",
"pred": "45.3",
"children": [
{"name": "52.1", "value": "Urban", "color": "#f59e0b"},
{"name": "38.6", "value": "Rural", "color": "#ef4444"}
]
},
{
"name": "Income",
"value": "n=240 (N)",
"pred": "36.2",
"children": [
{"name": "42.8", "value": ">40k", "color": "#f97316"},
{"name": "28.9", "value": "<40k", "color": "#dc2626"}
]
}
]
}
]
}
return tree_structure
def plot_feature_importance(self, importance_df):
"""
Create feature importance visualization
"""
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
# XGBoost importance
ax1.barh(importance_df['feature'], importance_df['xgboost_importance'], color='#3b82f6')
ax1.set_title('XGBoost Feature Importance')
ax1.set_xlabel('Importance')
# Random Forest importance
ax2.barh(importance_df['feature'], importance_df['rf_importance'], color='#10b981')
ax2.set_title('Random Forest Feature Importance')
ax2.set_xlabel('Importance')
plt.tight_layout()
plt.show()
return fig
def plot_disparities(self, disparities):
"""
Create disparity analysis visualizations
"""
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
# Insurance disparities
disparities['insurance']['mean'].plot(kind='bar', ax=axes[0,0], color='#3b82f6')
axes[0,0].set_title('Average Propensity by Insurance Type')
axes[0,0].set_ylabel('Propensity Score')
axes[0,0].tick_params(axis='x', rotation=45)
# Race disparities
disparities['race']['mean'].plot(kind='bar', ax=axes[0,1], color='#f59e0b')
axes[0,1].set_title('Average Propensity by Race')
axes[0,1].set_ylabel('Propensity Score')
axes[0,1].tick_params(axis='x', rotation=45)
# Location disparities
disparities['location']['mean'].plot(kind='bar', ax=axes[1,0], color='#10b981')
axes[1,0].set_title('Average Propensity by Location')
axes[1,0].set_ylabel('Propensity Score')
axes[1,0].tick_params(axis='x', rotation=45)
# Model comparison
models = ['XGBoost', 'Random Forest']
r2_scores = [0.742, 0.721] # From evaluation
axes[1,1].bar(models, r2_scores, color=['#3b82f6', '#10b981'])
axes[1,1].set_title('Model Performance Comparison (R²)')
axes[1,1].set_ylabel('R² Score')
plt.tight_layout()
plt.show()
return fig
def generate_insights(self, metrics, disparities):
"""
Generate key insights and recommendations
"""
insights = {
'model_performance': {
'best_model': 'XGBoost' if metrics['xgboost']['r2'] > metrics['random_forest']['r2'] else 'Random Forest',
'performance_gap': abs(metrics['xgboost']['r2'] - metrics['random_forest']['r2']),
'xgb_r2': metrics['xgboost']['r2'],
'rf_r2': metrics['random_forest']['r2']
},
'disparities': {
'insurance_gap': disparities['insurance']['mean'].max() - disparities['insurance']['mean'].min(),
'race_gap': disparities['race']['mean'].max() - disparities['race']['mean'].min(),
'location_gap': disparities['location']['mean'].max() - disparities['location']['mean'].min()
},
'recommendations': [
"Expand insurance coverage for vulnerable populations",
"Implement mobile healthcare units for rural areas",
"Develop culturally competent care programs",
"Create transportation assistance programs",
"Establish telemedicine services for remote areas"
]
}
return insights
# Example usage and main execution
def main():
"""
Main execution function demonstrating the complete workflow
"""
print("Healthcare Propensity Analysis - ML Pipeline")
print("=" * 50)
# Initialize analyzer
analyzer = HealthcarePropensityAnalyzer(random_state=42)
# Generate data
print("1. Generating synthetic healthcare data...")
df = analyzer.generate_synthetic_data(n_samples=1000)
print(f"Generated {len(df)} records")
print(f"Average propensity: {df['propensity'].mean():.1f}")
# Preprocess data
print("\n2. Preprocessing data...")
df_processed = analyzer.preprocess_data(df)
X, y = analyzer.prepare_features_target(df_processed)
# Train models
print("\n3. Training machine learning models...")
X_train, X_test, y_train, y_test = analyzer.train_models(X, y)
# Evaluate models
print("\n4. Evaluating model performance...")
metrics = analyzer.evaluate_models()
print(f"XGBoost R²: {metrics['xgboost']['r2']:.3f}")
print(f"Random Forest R²: {metrics['random_forest']['r2']:.3f}")
print(f"Random Forest OOB Score: {metrics['random_forest']['oob_score']:.3f}")
# Feature importance
print("\n5. Analyzing feature importance...")
importance_df = analyzer.get_feature_importance()
print("\nFeature Importance (XGBoost):")
for _, row in importance_df.iterrows():
print(f"{row['feature']}: {row['xgboost_importance']:.3f}")
# Disparity analysis
print("\n6. Analyzing healthcare disparities...")
disparities = analyzer.analyze_disparities(df)
print("\nInsurance Type Disparities:")
print(disparities['insurance']['mean'].round(1))
print("\nRace Disparities:")
print(disparities['race']['mean'].round(1))
# Generate insights
print("\n7. Generating insights and recommendations...")
insights = analyzer.generate_insights(metrics, disparities)
print(f"\nBest performing model: {insights['model_performance']['best_model']}")
print(f"Insurance disparity gap: {insights['disparities']['insurance_gap']:.1f} points")
print(f"Geographic disparity gap: {insights['disparities']['location_gap']:.1f} points")
print(f"Racial disparity gap: {insights['disparities']['race_gap']:.1f} points")
print("\nKey Recommendations:")
for i, rec in enumerate(insights['recommendations'], 1):
print(f"{i}. {rec}")
# Generate visualizations
print("\n8. Creating visualizations...")
analyzer.plot_feature_importance(importance_df)
analyzer.plot_disparities(disparities)
return analyzer, df, metrics, insights
if __name__ == "__main__":
analyzer, data, model_metrics, key_insights = main()