ADDI Alzheimers Detection Challenge
Grid search + voting classifier
perform a GS over a voting classifier made of RF and BG
this is just a starter notebook for sklearn. sampling and parameters must be tuned for gaining better score.
What is the notebook about?¶
The challenge is to use the features extracted from the Clock Drawing Test to build an automated and algorithm to predict whether each participant is one of three phases:
1) Pre-Alzheimer’s (Early Warning) 2) Post-Alzheimer’s (Detection) 3) Normal (Not an Alzheimer’s patient)
In machine learning terms: this is a 3-class classification task.
How to use this notebook? 📝¶
- Update the config parameters. You can define the common variables here
Variable | Description |
---|---|
AICROWD_DATASET_PATH |
Path to the file containing test data (The data will be available at /ds_shared_drive/ on aridhia workspace). This should be an absolute path. |
AICROWD_PREDICTIONS_PATH |
Path to write the output to. |
AICROWD_ASSETS_DIR |
In case your notebook needs additional files (like model weights, etc.,), you can add them to a directory and specify the path to the directory here (please specify relative path). The contents of this directory will be sent to AIcrowd for evaluation. |
AICROWD_API_KEY |
In order to submit your code to AIcrowd, you need to provide your account's API key. This key is available at https://www.aicrowd.com/participants/me |
- Installing packages. Please use the Install packages 🗃 section to install the packages
- Training your models. All the code within the Training phase ⚙️ section will be skipped during evaluation. Please make sure to save your model weights in the assets directory and load them in the predictions phase section
Setup AIcrowd Utilities 🛠¶
We use this to bundle the files for submission and create a submission on AIcrowd. Do not edit this block.
!pip install -q -U aicrowd-cli
%load_ext aicrowd.magic
AIcrowd Runtime Configuration 🧷¶
Define configuration parameters. Please include any files needed for the notebook to run under ASSETS_DIR
. We will copy the contents of this directory to your final submission file 🙂
The dataset is available under /ds_shared_drive
on the workspace.
import os
# Please use the absolute for the location of the dataset.
# Or you can use relative path with `os.getcwd() + "test_data/validation.csv"`
AICROWD_DATASET_PATH = os.getenv("DATASET_PATH", "/ds_shared_drive/validation.csv")
AICROWD_PREDICTIONS_PATH = os.getenv("PREDICTIONS_PATH", "predictions.csv")
AICROWD_ASSETS_DIR = "assets"
pip install -U scikit-learn# Install packages 🗃
Please add all pacakage installations in this section
pip install numpy pandas scikit-learn
Define preprocessing code 💻¶
The code that is common between the training and the prediction sections should be defined here. During evaluation, we completely skip the training section. Please make sure to add any common logic between the training and prediction sections here.
Import common packages¶
Please import packages that are common for training and prediction phases here.
import numpy as np
import pandas as pd
import pickle
from sklearn import preprocessing
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, VotingClassifier
from joblib import parallel_backend
# some precdef read(uri):
def read(uri):
x_df = pd.read_csv(uri)
x_df.drop('row_id', axis=1, inplace=True)
cmap = {'normal': 0, 'post_alzheimer': 1, 'pre_alzheimer': 2, 'TL': 1, 'BL': 2, 'TR': 3, 'BR': 4}
x_df = x_df.applymap(lambda s: cmap.get(s) if s in cmap else s)
x_df = x_df.fillna(0)
return x_df
Training phase ⚙️¶
You can define your training code here. This sections will be skipped during evaluation.
clf0 = GradientBoostingClassifier(n_estimators=100, learning_rate=0.2, max_depth=4, random_state=0)
clf1 = RandomForestClassifier(n_estimators=100)
clf2 = RandomForestClassifier(n_estimators=100)
params = {'c0__learning_rate': [0.1, 0.2, 0.3],
'c0__n_estimators': [50, 100, 150, 200],
'c0__max_depth': [3, 4, 5],
'c0__subsample': [1.0, 0.9, 0.7],
'c1__n_estimators': [50, 100, 150, 200],
'c1__min_samples_split': [2, 3]}
eclf = VotingClassifier(estimators=[('c0', clf0), ('c1', clf1), ('c2', clf2)], voting='soft', weights=[1, 2, 1])
clf = GridSearchCV(estimator=eclf, param_grid=params, cv=5, verbose=1)
Load training data¶
x_df = read('/ds_shared_drive/train.csv')
s_low = x_df[x_df['diagnosis'].isin([1, 2])]
s_high = x_df[x_df['diagnosis'] == 0].sample(n=s_low.shape[0], random_state=2048)
x_df = pd.concat([s_low, s_high]).sample(frac=1).reset_index(drop=True)
y_df = x_df['diagnosis']
x_df.drop('diagnosis', axis=1, inplace=True)
# scaler = preprocessing.MinMaxScaler()
# x_df = scaler.fit_transform(x_df)
# print(list(train_df.columns))
# train_df.describe()
# x_train, x_test, y_train, y_test = train_test_split(x_df, y_df ,test_size = 0.2, shuffle=True)
x_train = x_df
y_train = y_df
Train your model¶
with parallel_backend('threading', n_jobs=4):
clf.fit(x_train, y_train)
# some custom code block
Save your trained model¶
with open('assets/model.pkl', 'wb') as file:
pickle.dump(clf, file)
Prediction phase 🔎¶
Please make sure to save the weights from the training section in your assets directory and load them in this section
with open('assets/model.pkl', 'rb') as file:
clf = pickle.load(file)
Load test data¶
test_data = pd.read_csv(AICROWD_DATASET_PATH)
test_data.head()
x_df = read(AICROWD_DATASET_PATH)
# x_df = scaler.transform(x_df)
Generate predictions¶
predictions = {
"row_id": test_data["row_id"].values,
"normal_diagnosis_probability": np.zeros(len(test_data["row_id"].values)),
"post_alzheimer_diagnosis_probability": np.zeros(len(test_data["row_id"].values)),
"pre_alzheimer_diagnosis_probability": np.zeros(len(test_data["row_id"].values)),
}
prd = clf.predict_proba(x_df)
r_map = {0: 'normal_diagnosis_probability', 1: 'post_alzheimer_diagnosis_probability', 2: 'pre_alzheimer_diagnosis_probability'}
for i in range(prd.shape[0]):
for j in range(prd.shape[1]):
predictions[r_map[j]][i] = prd[i][j]
predictions_df = pd.DataFrame.from_dict(predictions)
Save predictions 📨¶
predictions_df.to_csv(AICROWD_PREDICTIONS_PATH, index=False)
Submit to AIcrowd 🚀¶
NOTE: PLEASE SAVE THE NOTEBOOK BEFORE SUBMITTING IT (Ctrl + S)
!DATASET_PATH=$AICROWD_DATASET_PATH \
aicrowd notebook submit \
--assets-dir $AICROWD_ASSETS_DIR \
--challenge addi-alzheimers-detection-challenge
Content
Comments
You must login before you can post a comment.