Notes
Notes - notes.io |
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, confusion_matrix
# Assume your dataset is loaded in a DataFrame 'df'
# Let's further assume your dataset has an additional column 'population'
# which represents the population of each region.
#### ----- Step 1: Feature Engineering ----- ####
## Proportionality Features ---------------------------------
# Example transformations assuming you're provided with 'population' for normalization
df['day_confirmed_per_capita'] = df['Day_confirmed'] / df['population']
df['cumulative_cases_per_capita'] = df['Confirmed'] / df['population']
df['day_deaths_per_capita'] = df['Day_deaths'] / df['population']
## Add Growth Trend Features -----------------------------
# 7-day rolling average of daily confirmed cases (smoothing noisy data)
df['roll_avg_new_cases_7d'] = df.groupby('Region')['Day_confirmed'].transform(lambda x: x.rolling(window=7).mean())
# 14-day rolling average of daily deaths to capture death trends (slower lag response)
df['roll_avg_new_deaths_14d'] = df.groupby('Region')['Day_deaths'].transform(lambda x: x.rolling(window=14).mean())
## Cumulative Mortality Rate ------------------------------
df['mortality_rate'] = (df['Deaths'] / df['Confirmed']) * 100
## Optional Handling: Log Transformations on skewed metrics --------------
# Avoid negative infinity errors by using np.log1p which safely captures log(0) -> log(1)
df['log_day_confirmed_per_capita'] = np.log1p(df['day_confirmed_per_capita'])
df['log_cumulative_cases_per_capita'] = np.log1p(df['cumulative_cases_per_capita'])
df['log_day_deaths_per_capita'] = np.log1p(df['day_deaths_per_capita'])
#### ----- Step 2: Preprocessing and Cleaning ----- ####
# Fill missing values (if any) with zeroes for basic placeholders of unreported data
df.fillna(0, inplace=True)
## List of features we plan to use (select relevant feature columns)
features_to_use = [
'log_day_confirmed_per_capita',
'log_cumulative_cases_per_capita',
'roll_avg_new_cases_7d',
'roll_avg_new_deaths_14d',
'mortality_rate'
]
X = df[features_to_use] # Input Features for training
y = df["target"] # Target variable ('High Demand' or 'Low Demand')
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
#### ----- Step 3: Feature Scaling ----- ####
scaler = StandardScaler() # Scaling method chosen - zero mean/unit variance
X_train_scaled = scaler.fit_transform(X_train) # Fit on training set and transform it Continuing with the code, we'll now focus on scaling your test set and training the model. Afterward, we will evaluate its performance.
# Scale the test set as well based on the same scaler fitted on training data
X_test_scaled = scaler.transform(X_test)
#### ----- Step 4: Model Training ----- ####
# Use RandomForestClassifier as proposed earlier
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
# Train (fit) the Random Forest model on the scaled training data
rf_model.fit(X_train_scaled, y_train)
#### ----- Step 5: Model Evaluation ----- ####
# Use the trained model to make predictions on both training and test sets
train_predictions = rf_model.predict(X_train_scaled)
test_predictions = rf_model.predict(X_test_scaled)
# Print out a confusion matrix for evaluation (on test set)
print("Confusion Matrix (Test Datatset):")
print(confusion_matrix(y_test, test_predictions))
# Print a more detailed classification report (precision, recall, f1-score)
print("nClassification Report:")
print(classification_report(y_test, test_predictions))
#### OPTIONAL: Investigate Feature Importance -----
import matplotlib.pyplot as plt
# Extract feature importance from the Random Forest model
importances = rf_model.feature_importances_
feature_names = features_to_use
# Sort features by importance score in descending order
sorted_idx = np.argsort(importances)[::-1]
plt.figure(figsize=(10,6))
plt.title("Feature Importances - Random Forest")
plt.bar(range(len(importances)), importances[sorted_idx], align='center')
plt.xticks(range(len(importances)), [feature_names[i] for i in sorted_idx], rotation=90)
plt.tight_layout()
plt.show()
-------
# Step to find thresholds for 'High' demand based on 'demanding' metrics
upper_percentile_for_cases = df['log_day_confirmed_per_capita'].quantile(0.75)
upper_percentile_for_mortality = df['mortality_rate'].quantile(0.75)
# Function to set target variable based on defined criteria
def define_target(row):
if row['log_day_confirmed_per_capita'] >= upper_percentile_for_cases or
row['mortality_rate'] >= upper_percentile_for_mortality:
return 'High Demand'
else:
return 'Low Demand'
# Applying function to determine `target` for each entry in our DataFrame
df['target'] = df.apply(define_target, axis=1)
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
