Loading

CNTMC

[Getting Started Notebook] CNTMC Challenge

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 imporvments.

Starter Code for CNTMC Practice Challange

Note : Create a copy of the notebook and use the copy for submission. Go to File > Save a Copy in Drive to create a new copy

Downloading Dataset

Installing aicrowd-cli

In [1]:
!pip install aicrowd-cli
%load_ext aicrowd.magic
Requirement already satisfied: aicrowd-cli in /home/gauransh/anaconda3/lib/python3.8/site-packages (0.1.10)
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: toml<1,>=0.10.2 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (0.10.2)
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: requests-toolbelt<1,>=0.9.1 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (0.9.1)
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: click<8,>=7.1.2 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (7.1.2)
Requirement already satisfied: tqdm<5,>=4.56.0 in /home/gauransh/anaconda3/lib/python3.8/site-packages (from aicrowd-cli) (4.62.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: 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: 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: 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: 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: 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)
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)
In [2]:
%aicrowd login
Please login here: https://api.aicrowd.com/auth/tK8jq9FaBgEDD9GACzX7OUfbHwY5nggWZgRT9hIvBqI
Opening in existing browser session.
API Key valid
Saved API Key successfully!
In [2]:
!rm -rf data
!mkdir data
%aicrowd ds dl -c cntmc -o data

Importing Libraries

In this baseline, we will be using skleanr library to train the model and generate the predictions

In [3]:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
import os
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import display

Reading the dataset

Here, we will read the train.csv which contains both training samples & labels, and test.csv which contains testing samples.

In [4]:
# Reading the CSV
train_data_df = pd.read_csv("data/train.csv")
test_data_df = pd.read_csv("data/test.csv")

# train_data.shape, test_data.shape
display(train_data_df.head())
display(test_data_df.head())
Age Wife Education Husband Education Numver Of Children Ever Born Wife Religion Wife Working Husband occupation Living Standard Media Exposure Contraceptive Method
0 28 4 4 1 1 1 3 2 0 3
1 47 1 3 9 1 0 3 3 0 3
2 36 2 4 2 1 0 2 4 0 1
3 24 2 2 2 1 1 3 4 0 3
4 29 4 3 4 1 0 3 2 0 2
Age Wife Education Husband Education Numver Of Children Ever Born Wife Religion Wife Working Husband occupation Living Standard Media Exposure
0 39 1 2 2 1 0 2 4 0
1 24 4 4 1 1 0 1 4 0
2 38 4 4 4 1 1 2 4 0
3 43 4 4 3 0 1 2 4 0
4 27 2 2 3 1 1 2 2 1

Data Preprocessing

In [5]:
train_data_df.columns
Out[5]:
Index(['Age', 'Wife Education', 'Husband Education',
       'Numver Of Children Ever Born', 'Wife Religion', 'Wife Working',
       'Husband occupation', 'Living Standard', 'Media Exposure',
       'Contraceptive Method'],
      dtype='object')
In [6]:
# Separating data from the dataframe for final training
X = train_data_df.drop(['Contraceptive Method'], axis=1).to_numpy()
y = train_data_df['Contraceptive Method'].to_numpy()
print(X.shape, y.shape)
(1178, 9) (1178,)
In [7]:
# Visualising the final lable classes for training
sns.countplot(y)
/home/gauransh/anaconda3/lib/python3.8/site-packages/seaborn/_decorators.py:36: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  warnings.warn(
Out[7]:
<AxesSubplot:ylabel='count'>

Splitting the data

In [8]:
# Splitting the training set, and training & validation
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)
print(X_train.shape)
print(y_train.shape)
(942, 9)
(942,)
In [9]:
X_train[0], y_train[0]
Out[9]:
(array([23,  2,  4,  2,  1,  1,  1,  4,  0]), 1)

Training the Model

In [10]:
model = MLPClassifier()
model.fit(X_train, y_train)
/home/gauransh/anaconda3/lib/python3.8/site-packages/sklearn/neural_network/_multilayer_perceptron.py:614: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) reached and the optimization hasn't converged yet.
  warnings.warn(
Out[10]:
MLPClassifier()

Validation

In [11]:
model.score(X_val, y_val)
Out[11]:
0.5932203389830508

So, we are done with the baseline let's test with real testing data and see how we submit it to challange.

Predictions

In [12]:
# Separating data from the dataframe for final testing
X_test = test_data_df.to_numpy()
print(X_test.shape)
(295, 9)
In [13]:
# Predicting the labels
predictions = model.predict(X_test)
predictions.shape
Out[13]:
(295,)
In [15]:
# Converting the predictions array into pandas dataset
submission = pd.DataFrame({"contraceptive":predictions})
submission
Out[15]:
contraceptive
0 1
1 1
2 3
3 1
4 1
... ...
290 3
291 2
292 1
293 3
294 3

295 rows × 1 columns

In [16]:
# Saving the pandas dataframe
!rm -rf assets
!mkdir assets
submission.to_csv(os.path.join("assets", "submission.csv"), index=False)

Submitting our Predictions

Note : Please save the notebook before submitting it (Ctrl + S)

In [17]:
!!aicrowd submission create -c cntmc -f assets/submission.csv
Out[17]:
['submission.csv ━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 2,249/604 bytes • ? • 0:00:00',
 '                                  ╭─────────────────────────╮                                  ',
 '                                  │ Successfully submitted! │                                  ',
 '                                  ╰─────────────────────────╯                                  ',
 '                                        Important links                                        ',
 '┌──────────────────┬──────────────────────────────────────────────────────────────────────────┐',
 '│  This submission │ https://www.aicrowd.com/challenges/cntmc/submissions/171695              │',
 '│                  │                                                                          │',
 '│  All submissions │ https://www.aicrowd.com/challenges/cntmc/submissions?my_submissions=true │',
 '│                  │                                                                          │',
 '│      Leaderboard │ https://www.aicrowd.com/challenges/cntmc/leaderboards                    │',
 '│                  │                                                                          │',
 '│ Discussion forum │ https://discourse.aicrowd.com/c/cntmc                                    │',
 '│                  │                                                                          │',
 '│   Challenge page │ https://www.aicrowd.com/challenges/cntmc                                 │',
 '└──────────────────┴──────────────────────────────────────────────────────────────────────────┘',
 "{'submission_id': 171695, 'created_at': '2022-01-10T21:16:47.072Z'}"]
In [ ]:


Comments

You must login before you can post a comment.

Execute