Upload 1077_252_49.py
Browse files- 1077_252_49.py +128 -0
1077_252_49.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""1077_252_49
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1Oc-ciFRATiivfVFWe8Dd7m5LvNeQIQZT
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
# Commented out IPython magic to ensure Python compatibility.
|
| 11 |
+
import warnings
|
| 12 |
+
warnings.filterwarnings('ignore')
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
import pandas as pd
|
| 16 |
+
import matplotlib
|
| 17 |
+
matplotlib.use('Agg')
|
| 18 |
+
import matplotlib.pyplot as plt
|
| 19 |
+
# %matplotlib inline
|
| 20 |
+
|
| 21 |
+
import seaborn as sns
|
| 22 |
+
|
| 23 |
+
from sklearn.model_selection import train_test_split
|
| 24 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 25 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
| 26 |
+
from sklearn.inspection import permutation_importance
|
| 27 |
+
|
| 28 |
+
sns.set(style='whitegrid')
|
| 29 |
+
|
| 30 |
+
plt.switch_backend('Agg')
|
| 31 |
+
|
| 32 |
+
file_path = '/content/air_quality_health_dataset.csv'
|
| 33 |
+
try:
|
| 34 |
+
df = pd.read_csv(file_path, encoding='ISO-8859-1', delimiter=',')
|
| 35 |
+
print('Data loaded successfully.')
|
| 36 |
+
except Expection as e:
|
| 37 |
+
print('Error loading data:', e)
|
| 38 |
+
|
| 39 |
+
df.head()
|
| 40 |
+
|
| 41 |
+
missing_values = df.isnull().sum()
|
| 42 |
+
print('Missing values in each column:')
|
| 43 |
+
print(missing_values)
|
| 44 |
+
|
| 45 |
+
# Handling missing values (basic strategy): drop rows with critical missing data
|
| 46 |
+
df.dropna(subset=['date', 'aqi', 'pm2_5', 'hospital_admissions'], inplace=True)
|
| 47 |
+
|
| 48 |
+
# Convert population_density to a numeric value if possible; if not possible, leave it as is
|
| 49 |
+
try:
|
| 50 |
+
df['population_density_numeric'] = pd.to_numeric(df['population_density'], errors='coerce')
|
| 51 |
+
print('Converted population_density to numeric where possible.')
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print('Error converting population_density:', e)
|
| 54 |
+
|
| 55 |
+
# For any remaining non-numeric entries in population_density_numeric, we can fill them with the median
|
| 56 |
+
if 'population_density_numeric' in df.columns:
|
| 57 |
+
median_val = df['population_density_numeric'].median()
|
| 58 |
+
df['population_density_numeric'].fillna(median_val, inplace=True)
|
| 59 |
+
|
| 60 |
+
# Final sanity check
|
| 61 |
+
print('Data types after processing:')
|
| 62 |
+
print(df.dtypes)
|
| 63 |
+
|
| 64 |
+
numeric_df = df.select_dtypes(include=[np.number])
|
| 65 |
+
|
| 66 |
+
if numeric_df.shape[1] >= 4:
|
| 67 |
+
plt.figure(figsize=(10, 8))
|
| 68 |
+
corr = numeric_df.corr()
|
| 69 |
+
sns.heatmap(corr, annot=True, cmap='coolwarm', fmt='.2f')
|
| 70 |
+
plt.title('Correlation Heatmap')
|
| 71 |
+
plt.tight_layout()
|
| 72 |
+
plt.savefig('correlation_heatmap.png')
|
| 73 |
+
plt.show()
|
| 74 |
+
|
| 75 |
+
else:
|
| 76 |
+
print('Not enough numeric columns for a correlation heatmap.')
|
| 77 |
+
|
| 78 |
+
sns.pairplot(df[['aqi', 'pm2_5', 'pm10', 'hospital_admissions']])
|
| 79 |
+
plt.suptitle('Pair Plot of Selected Variables', y=1.02)
|
| 80 |
+
plt.savefig('pairplot.png')
|
| 81 |
+
plt.show()
|
| 82 |
+
|
| 83 |
+
numeric_cols = numeric_df.columns
|
| 84 |
+
for col in numeric_cols:
|
| 85 |
+
plt.figure(figsize=(6, 4))
|
| 86 |
+
sns.histplot(df[col].dropna(), kde=True, bins=30)
|
| 87 |
+
plt.title(f'Histogram of {col}')
|
| 88 |
+
plt.savefig(f'histogram_{col}.png')
|
| 89 |
+
plt.show()
|
| 90 |
+
|
| 91 |
+
plt.figure(figsize=(10, 6))
|
| 92 |
+
sns.countplot(data=df, x='city', order=df['city'].value_counts().index)
|
| 93 |
+
plt.title('Count Plot of Cities')
|
| 94 |
+
plt.xticks(rotation=45, ha='right')
|
| 95 |
+
plt.savefig('countplot_cities.png')
|
| 96 |
+
plt.show()
|
| 97 |
+
|
| 98 |
+
target = 'hospital_admissions'
|
| 99 |
+
features = ['aqi', 'pm2_5', 'pm10', 'no2', 'o3', 'temperature', 'humidity', 'hospital_capacity']
|
| 100 |
+
|
| 101 |
+
model_df = df.dropna(subset=features + [target]).copy()
|
| 102 |
+
|
| 103 |
+
X = model_df[features]
|
| 104 |
+
y = model_df[target]
|
| 105 |
+
|
| 106 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 107 |
+
|
| 108 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
| 109 |
+
model.fit(X_train, y_train)
|
| 110 |
+
|
| 111 |
+
y_pred = model.predict(X_test)
|
| 112 |
+
|
| 113 |
+
mse = mean_squared_error(y_test, y_pred)
|
| 114 |
+
r2 = r2_score(y_test, y_pred)
|
| 115 |
+
print(f'Mean Squared Error: {mse:2f}')
|
| 116 |
+
print(f'R*2 Score: {r2:.2F}')
|
| 117 |
+
|
| 118 |
+
perm_importance = permutation_importance(model, X_test, y_test, n_repeats=10, random_state=42)
|
| 119 |
+
|
| 120 |
+
feature_importance = pd.Series(perm_importance.importances_mean, index=features)
|
| 121 |
+
feature_importance = feature_importance.sort_values()
|
| 122 |
+
|
| 123 |
+
plt.figure(figsize=(8,6))
|
| 124 |
+
plt.barh(feature_importance.index, feature_importance.values, color='skyblue')
|
| 125 |
+
plt.xlabel('Permutation Importance')
|
| 126 |
+
plt.title('Feature Importance')
|
| 127 |
+
plt.savefig('feature_importance.png')
|
| 128 |
+
plt.show()
|