text_html_output_2.png text_html_output_1.png
from datetime import timedelta

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_cs

import seaborn as sns

TARGET = 'Is_CANCELLED'
NA_CATEGORY = 'NA_'

train = pd.read_csv('/kaggle/input/techcom-ai-competition/train.csv')
test = pd.read_csv('/kaggle/input/techcom-ai-competition/test.csv')
sample_sub = pd.read_csv('/kaggle/input/techcom-ai-competition/sample_submission.csv')
train.rename(columns={'MEMBERSHIP_STATUS': 'Is_CANCELLED'}, inplace=True)
train[TARGET] = train[TARGET].map({'INFORCE': 0, 'CANCELLED': 1})
test['END_DATE'] = np.nan

def add_date_features(df: pd.DataFrame):
    df['START_DATE'] = pd.to_datetime(df['START_DATE'], format='%Y%m%d')
    df['START_YEAR'] = df['START_DATE'].dt.year
    df['START_MONTH'] = df['START_DATE'].dt.month
    df['START_YM'] = df['START_YEAR'] * 100 + df['START_MONTH']
    df['END_DATE'] = pd.to_datetime(df['END_DATE'], format='%Y%m%d')
    df['END_YEAR'] = df['END_DATE'].dt.year
    df['END_MONTH'] = df['END_DATE'].dt.month
    df['END_YM'] = df['END_YEAR'] * 100 + df['END_MONTH']
    df['N_DAYS'] = (df['END_DATE'] - df['START_DATE']) / timedelta(days=1)
    return df
train = add_date_features(train)
test = add_date_features(test)

years = list(range(2006, 2014))
plt.figure(figsize=(5, 4.5))
ax1 = sns.lineplot(x='START_YEAR', y='MEMBERSHIP_NUMBER', data=train.groupby('START_YEAR')['MEMBERSHIP_NUMBER'].count().reset_index(), label='START_YEAR')
plt.ylabel('Join')
ax2 = ax1.twinx()
ax2 = sns.lineplot(x='END_YEAR', y='MEMBERSHIP_NUMBER', data=train.groupby('END_YEAR')['MEMBERSHIP_NUMBER'].count().reset_index(), ax=ax2, label='END_YEAR', color='orange', legend=False)
plt.ylabel('Churn')
plt.xlabel('START/END_YEAR')
handler1, label1 = ax1.get_legend_handles_labels()
handler2, label2 = ax2.get_legend_handles_labels()
ax1.legend(handler1 + handler2, label1 + label2, loc=2, borderaxespad=0.0, bbox_to_anchor=(1.0, 1.05), frameon=False)
plt.suptitle('Number of rows by year fields')
for year_field in ('START_YEAR', 'END_YEAR'):
    display(train.groupby(year_field)['MEMBERSHIP_NUMBER'].count().sort_index().reset_index().rename(columns={'MEMBERSHIP_NUMBER': 'N_ROWS'}))