Loading

CRDIO

[Getting Started Notebook] CRDIO Challange

This is a Baseline Code to get you started with the challenge.

gauransh_k

You can use this code to start understanding the data and create a baseline model for further improvements.

AIcrowd-Logo

Baseline for CRDIO Challenge on AIcrowd

Author : Gauransh Kumar, Shubham Sharma

Download Necessary Packages 📚

In [1]:
import sys
!{sys.executable} -m pip install numpy
!{sys.executable} -m pip install pandas
!{sys.executable} -m pip install scikit-learn
!{sys.executable} -m pip install aicrowd-cli
%load_ext aicrowd.magic
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
Requirement already satisfied: numpy in /home/gauransh/anaconda3/lib/python3.8/site-packages (1.22.2)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
Requirement already satisfied: pandas in /home/gauransh/anaconda3/lib/python3.8/site-packages (1.3.2)
Requirement already satisfied: numpy>=1.17.3 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from pandas) (1.22.2)
Requirement already satisfied: python-dateutil>=2.7.3 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from pandas) (2.8.2)
Requirement already satisfied: pytz>=2017.3 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from pandas) (2021.1)
Requirement already satisfied: six>=1.5 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from python-dateutil>=2.7.3->pandas) (1.15.0)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
Requirement already satisfied: scikit-learn in /home/gauransh/anaconda3/lib/python3.8/site-packages (0.24.2)
Requirement already satisfied: joblib>=0.11 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from scikit-learn) (1.0.1)
Requirement already satisfied: numpy>=1.13.3 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from scikit-learn) (1.22.2)
Requirement already satisfied: scipy>=0.19.1 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from scikit-learn) (1.6.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from scikit-learn) (2.2.0)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
Requirement already satisfied: aicrowd-cli in /home/gauransh/anaconda3/lib/python3.8/site-packages (0.1.10)
Requirement already satisfied: tqdm<5,>=4.56.0 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (4.60.0)
Requirement already satisfied: rich<11,>=10.0.0 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (10.15.2)
Requirement already satisfied: GitPython==3.1.18 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (3.1.18)
Requirement already satisfied: requests<3,>=2.25.1 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (2.26.0)
Requirement already satisfied: click<8,>=7.1.2 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (7.1.2)
Requirement already satisfied: pyzmq==22.1.0 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (22.1.0)
Requirement already satisfied: toml<1,>=0.10.2 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (0.10.2)
Requirement already satisfied: requests-toolbelt<1,>=0.9.1 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (0.9.1)
Requirement already satisfied: gitdb<5,>=4.0.1 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from GitPython==3.1.18->aicrowd-cli) (4.0.9)
Requirement already satisfied: smmap<6,>=3.0.1 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from gitdb<5,>=4.0.1->GitPython==3.1.18->aicrowd-cli) (5.0.0)
Requirement already satisfied: certifi>=2017.4.17 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from requests<3,>=2.25.1->aicrowd-cli) (2021.10.8)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from requests<3,>=2.25.1->aicrowd-cli) (1.26.6)
Requirement already satisfied: charset-normalizer~=2.0.0 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from requests<3,>=2.25.1->aicrowd-cli) (2.0.0)
Requirement already satisfied: idna<4,>=2.5 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from requests<3,>=2.25.1->aicrowd-cli) (3.1)
Requirement already satisfied: commonmark<0.10.0,>=0.9.0 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from rich<11,>=10.0.0->aicrowd-cli) (0.9.1)
Requirement already satisfied: pygments<3.0.0,>=2.6.0 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from rich<11,>=10.0.0->aicrowd-cli) (2.10.0)
Requirement already satisfied: colorama<0.5.0,>=0.4.0 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from rich<11,>=10.0.0->aicrowd-cli) (0.4.4)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)
WARNING: Ignoring invalid distribution -umpy (/home/gauransh/anaconda3/lib/python3.8/site-packages)

Download Data

The first step is to download out train test data. We will be training a model on the train data and make predictions on test data. We submit our predictions

In [2]:
!rm -rf data
!mkdir data
%aicrowd ds dl -c crdio -o data

Import packages

In [3]:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC
from sklearn.metrics import f1_score,precision_score,recall_score,accuracy_score

Load Data

  • We use pandas ๐Ÿผ library to load our data.
  • Pandas loads the data into dataframes and facilitates us to analyse the data.
  • Learn more about it here ๐Ÿค“
In [4]:
all_data_path = "data/train.csv" #path where data is stored
In [5]:
all_data = pd.read_csv(all_data_path) #load data in dataframe using pandas

Visualize the data 👀

In [6]:
all_data.head()
Out[6]:
LBE LB AC FM UC ASTV MSTV ALTV MLTV DL ... Min Max Nmax Nzeros Mode Mean Median Variance Tendency NSP
0 135.0 135.0 8.0 2.0 7.0 60.0 1.0 0.0 5.2 0.0 ... 123.0 163.0 0.0 0.0 148.0 142.0 145.0 7.0 0.0 1
1 133.0 133.0 0.0 4.0 6.0 60.0 2.5 0.0 0.0 4.0 ... 83.0 148.0 2.0 0.0 131.0 122.0 130.0 14.0 1.0 1
2 135.0 135.0 10.0 2.0 4.0 60.0 0.9 0.0 0.5 0.0 ... 124.0 163.0 1.0 0.0 148.0 143.0 146.0 6.0 0.0 1
3 120.0 120.0 3.0 1.0 2.0 56.0 0.5 0.0 8.8 0.0 ... 106.0 141.0 1.0 0.0 125.0 125.0 126.0 3.0 0.0 1
4 132.0 132.0 6.0 0.0 6.0 27.0 1.7 0.0 11.3 0.0 ... 54.0 176.0 8.0 0.0 150.0 146.0 149.0 18.0 1.0 1

5 rows ร— 24 columns

The dataset consists of 24 attributes out of which first 23 attributes describes details of CTGs features and last attribute called NSP is used to classify these CTGs on the basis of fetal state.

Split Data into Train and Validation 🔪

  • The next step is to think of a way to test how well our model is performing. we cannot use the test data given as it does not contain the data labels for us to verify.
  • The workaround this is to split the given training data into training and validation. Typically validation sets give us an idea of how our model will perform on unforeseen data. it is like holding back a chunk of data while training our model and then using it to for the purpose of testing. it is a standard way to fine-tune hyperparameters in a model.
  • There are multiple ways to split a dataset into validation and training sets. following are two popular ways to go about it, k-fold, leave one out. ๐Ÿง
  • Validation sets are also used to avoid your model from overfitting on the train dataset.
In [7]:
X_train, X_val= train_test_split(all_data, test_size=0.2, random_state=42)
  • We have decided to split the data with 20 % as validation and 80 % as training.
  • To learn more about the train_test_split function click here. ๐Ÿง
  • This is of course the simplest way to validate your model by simply taking a random chunk of the train set and setting it aside solely for the purpose of testing our train model on unseen data. as mentioned in the previous block, you can experiment ๐Ÿ”ฌ with and choose more sophisticated techniques and make your model better.
  • Now, since we have our data splitted into train and validation sets, we need to get the corresponding labels separated from the data.
  • with this step we are all set move to the next step with a prepared dataset.
In [8]:
X_train,y_train = X_train.iloc[:,:-1],X_train.iloc[:,-1]
X_val,y_val = X_val.iloc[:,:-1],X_val.iloc[:,-1]

TRAINING PHASE 🏋️

Define the Model

  • We have fixed our data and now we are ready to train our model.

  • There are a ton of classifiers to choose from some being Logistic Regression, SVM, Random Forests, Decision Trees, etc.๐Ÿง

  • Remember that there are no hard-laid rules here. you can mix and match classifiers, it is advisable to read up on the numerous techniques and choose the best fit for your solution , experimentation is the key.

  • A good model does not depend solely on the classifier but also on the features you choose. So make sure to analyse and understand your data well and move forward with a clear view of the problem at hand. you can gain important insight from here.๐Ÿง

In [9]:
classifier = SVC(gamma='auto')

# from sklearn.linear_model import LogisticRegression
# classifier = LogisticRegression()
  • To start you off, We have used a basic Support Vector Machines classifier here.
  • But you can tune parameters and increase the performance. To see the list of parameters visit here.
  • Do keep in mind there exist sophisticated techniques for everything, the key as quoted earlier is to search them and experiment to fit your implementation.

To read more about other sklearn classifiers visit here ๐Ÿง. Try and use other classifiers to see how the performance of your model changes. Try using Logistic Regression or MLP and compare how the performance changes.

Train the classifier

In [10]:
classifier.fit(X_train, y_train)
Out[10]:
SVC(gamma='auto')

Got a warning! Dont worry, its just beacuse the number of iteration is very less(defined in the classifier in the above cell).Increase the number of iterations and see if the warning vanishes and also see how the performance changes.Do remember increasing iterations also increases the running time.( Hint: max_iter=500)

Validation Phase 🤔

Wonder how well your model learned! Lets check it.

Predict on Validation

Now we predict using our trained model on the validation set we created and evaluate our model on unforeseen data.

In [11]:
y_pred = classifier.predict(X_val)

Evaluate the Performance

  • We have used basic metrics to quantify the performance of our model.
  • This is a crucial step, you should reason out the metrics and take hints to improve aspects of your model.
  • Do read up on the meaning and use of different metrics. there exist more metrics and measures, you should learn to use them correctly with respect to the solution,dataset and other factors.
  • F1 score are the metrics for this challenge
In [12]:
precision = precision_score(y_val,y_pred,average='micro')
recall = recall_score(y_val,y_pred,average='micro')
accuracy = accuracy_score(y_val,y_pred)
f1 = f1_score(y_val,y_pred,average='macro')
In [13]:
print("Accuracy of the model is :" ,accuracy)
print("Recall of the model is :" ,recall)
print("Precision of the model is :" ,precision)
print("F1 score of the model is :" ,f1)
Accuracy of the model is : 0.8205882352941176
Recall of the model is : 0.8205882352941176
Precision of the model is : 0.8205882352941176
F1 score of the model is : 0.4130135499278444

Testing Phase 😅

We are almost done. We trained and validated on the training data. Now its the time to predict on test set and make a submission.

Load Test Set

Load the test data on which final submission is to be made.

In [14]:
final_test_path = "data/test.csv"
final_test = pd.read_csv(final_test_path)

Predict Test Set

Time for the moment of truth! Predict on test set and time to make the submission.

In [15]:
submission = classifier.predict(final_test)

Save the prediction to csv

In [18]:
# Saving the pandas dataframe
!rm -rf assets
!mkdir assets
submission = pd.DataFrame(submission)
submission.to_csv('assets/submission.csv',header=['NSP'],index=False)

๐Ÿšง Note :

  • Do take a look at the submission format.
  • The submission file should contain a header.
  • Follow all submission guidelines strictly to avoid inconvenience.

Make a submission using the aicrwd-cli

In [19]:
!aicrowd submission create -c crdio -f assets/submission.csv
submission.csv โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ” 100.0% โ€ข 2,501/856 bytes โ€ข ? โ€ข 0:00:00
                                            โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ                                            
                                            โ”‚ Successfully submitted! โ”‚                                            
                                            โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ                                            
                                                  Important links                                                  
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  This submission โ”‚ https://www.aicrowd.com/challenges/ai-blitz-4/problems/crdio/submissions/176357              โ”‚
โ”‚                  โ”‚                                                                                              โ”‚
โ”‚  All submissions โ”‚ https://www.aicrowd.com/challenges/ai-blitz-4/problems/crdio/submissions?my_submissions=true โ”‚
โ”‚                  โ”‚                                                                                              โ”‚
โ”‚      Leaderboard โ”‚ https://www.aicrowd.com/challenges/ai-blitz-4/problems/crdio/leaderboards                    โ”‚
โ”‚                  โ”‚                                                                                              โ”‚
โ”‚ Discussion forum โ”‚ https://discourse.aicrowd.com/c/ai-blitz-4                                                   โ”‚
โ”‚                  โ”‚                                                                                              โ”‚
โ”‚   Challenge page โ”‚ https://www.aicrowd.com/challenges/ai-blitz-4/problems/crdio                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
{'submission_id': 176357, 'created_at': '2022-03-05T13:30:50.446Z'}

Comments

You must login before you can post a comment.

Execute