-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunsupervised.py
More file actions
117 lines (94 loc) · 4.53 KB
/
Copy pathunsupervised.py
File metadata and controls
117 lines (94 loc) · 4.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
import seaborn as sns
# Set the style using seaborn
sns.set_style("whitegrid") # or "darkgrid", "white", "dark", "ticks"
def analyze_healthcare_patterns():
"""Analyze healthcare patterns to support SDG 3 goals."""
try:
# Load and preprocess data with ethical considerations
df = pd.read_csv('archive/healthcare_dataset.csv')
# Reduce dataset size by sampling
df = df.sample(n=min(500, len(df)), random_state=42)
print("SDG 3 Healthcare Analysis Initiative")
print("===================================")
print(f"Analyzing {len(df)} patient records for healthcare patterns\n")
# Optimize memory usage by selecting only necessary columns
df = df[['Name', 'Age', 'Billing Amount', 'Medical Condition',
'Admission Type']].copy()
# Convert data types to reduce memory
df['Age'] = df['Age'].astype('int32')
df['Billing Amount'] = df['Billing Amount'].astype('float32')
# Ethical consideration: Anonymize patient data
df['Name'] = 'ANONYMIZED'
# Select features relevant to healthcare access analysis
features = ['Age', 'Billing Amount', 'Medical Condition', 'Admission Type']
X = df[features].copy()
# Preprocess categorical variables
le = LabelEncoder()
X['Medical Condition'] = le.fit_transform(X['Medical Condition'])
X['Admission Type'] = le.fit_transform(X['Admission Type'])
# Scale numerical features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Find optimal clusters using silhouette analysis
optimal_clusters = find_optimal_clusters(X_scaled)
# Apply clustering
kmeans = KMeans(n_clusters=optimal_clusters, random_state=42)
df['Cluster'] = kmeans.fit_predict(X_scaled)
# Generate visualizations
create_healthcare_visualizations(df)
# Generate insights
generate_healthcare_insights(df)
except Exception as e:
print(f"Analysis Error: {str(e)}")
finally:
plt.close('all')
def find_optimal_clusters(X_scaled):
"""Determine optimal number of patient clusters."""
silhouette_scores = []
for k in range(2, 7):
kmeans = KMeans(n_clusters=k, random_state=42)
labels = kmeans.fit_predict(X_scaled)
score = silhouette_score(X_scaled, labels)
silhouette_scores.append(score)
return range(2, 7)[np.argmax(silhouette_scores)]
def create_healthcare_visualizations(df):
"""Create visualizations for healthcare analysis."""
# Use a more efficient backend
plt.switch_backend('Agg')
# Reduce figure quality for faster rendering
plt.style.use('default') # More efficient than seaborn style
fig, axes = plt.subplots(2, 1, figsize=(10, 8), dpi=80) # Reduced size and DPI
# Healthcare Access Pattern Visualization
scatter = axes[0].scatter(df['Age'], df['Billing Amount'],
c=df['Cluster'], cmap='viridis',
alpha=0.6, s=50)
axes[0].set_xlabel('Patient Age')
axes[0].set_ylabel('Healthcare Cost ($)')
axes[0].set_title('Healthcare Access and Cost Patterns')
plt.colorbar(scatter, ax=axes[0], label='Patient Group')
# Medical Condition Distribution
condition_dist = pd.crosstab(df['Cluster'], df['Medical Condition'])
condition_dist.plot(kind='bar', stacked=True, ax=axes[1])
axes[1].set_title('Healthcare Needs by Patient Group')
axes[1].set_xlabel('Patient Group')
axes[1].set_ylabel('Number of Patients')
plt.tight_layout()
plt.show()
def generate_healthcare_insights(df):
"""Generate insights for healthcare policy recommendations."""
print("\nHealthcare Access Insights:")
print("==========================")
for cluster in df['Cluster'].unique():
cluster_data = df[df['Cluster'] == cluster]
print(f"\nPatient Group {cluster + 1}:")
print(f"- Average Age: {cluster_data['Age'].mean():.1f} years")
print(f"- Average Healthcare Cost: ${cluster_data['Billing Amount'].mean():.2f}")
print(f"- Most Common Condition: {cluster_data['Medical Condition'].mode()[0]}")
if __name__ == "__main__":
analyze_healthcare_patterns()