Topic : Predict if Client will subscribe to direct marketing campaign for a banking institution
Summary : The data is related with direct marketing campaigns of a Portuguese banking institution. The marketing campaigns were based on phone calls. Often, more than one contact to the same client was required, in order to access if the product (bank term deposit) would be ('yes') or not ('no') subscribed.
df_y = df['y'].value_counts()
print 'Percentage of Y=yes:',(df_y[1] / float(df_y[0] + df_y[1])) * 100
sns.countplot(df['y'])
plt.show()
Last Contact month of year (categorical: 'jan', 'feb', 'mar', ..., 'nov', 'dec')
#Distribution of variable month
plt.figure(figsize=(15,5))
plt.subplot(1,2,1)
sns.countplot(df['month'])
plt.title('Bar Graph showing distribution of Calls made across various months')
plt.subplot(1,2,2)
sns.countplot(x="month", hue="y", data=df);
plt.title('Graph showing distribution of Calls made along with Convertion results across various months')
plt.show()
df_months = pd.crosstab(index=df['month'],columns=df['y'])
df_months['percentage(yes)'] = (df_months['yes'] / (df_months['yes'] + df_months['no'])) * 100
df_months.head()
Last contact day of the week (categorical: 'mon','tue','wed','thu','fri')
plt.figure(figsize=(15,5))
plt.subplot(1,2,1)
sns.countplot(df['day_of_week'])
plt.title('Distribution of Calls made across various days of the week')
plt.subplot(1,2,2)
sns.countplot(x="day_of_week", hue="y", data=df);
plt.title('Distribution of Calls made along with Convertion results across various days of the week')
plt.show()
df_days = pd.crosstab(index=df['day_of_week'],columns=df['y'])
df_days['percentage(yes)'] = (df_days['yes'] / (df_days['yes'] + df_days['no'])) * 100
df_days.head()
last contact duration, in seconds (numeric). Important note: this attribute highly affects the output target (e.g., if duration=0 then y='no'). Yet, the duration is not known before a call is performed. Also, after the end of the call y is obviously known. Thus, this input should only be included for benchmark purposes and should be discarded if the intention is to have a realistic predictive model.
#Distribution of variable day of week
plt.figure(figsize=(15,10))
plt.subplot(1,2,1)
plt.hist(df['duration'],bins=[0,100,200,300,400,500,1000,1500,2000])
plt.title('Histogram of Call duration')
plt.xlim(0,2500)
plt.subplot(1,2,2)
sns.boxplot(x='y',y='duration',data=df)
plt.title('Distribution of Call duration(in secs) vs Subscribed')
plt.show()
df[df['duration'] == 0]
NOTE: We only have 4 datapoints with duration = 0 which means these people were contacted first time These 4 data points should be removed before model training, as the duration is not known
new_data = df[df['duration'] != 0]
#Let's see if there is still any trend in coversion based on duration after removing duration = 0, data points
plt.figure(figsize=(10,12))
sns.boxplot(x='y',y='duration',data=new_data)
plt.show()
Number of contacts performed during this campaign and for this client (numeric, includes last contact)
#Distribution of variable day of week
plt.figure(figsize=(15,10))
plt.subplot(1,2,1)
plt.hist(df['campaign'])
plt.title('Distribution of Calls made during a campaign')
camp_less_than_20 = df[df['campaign'] < 20]
plt.subplot(1,2,2)
sns.countplot(x="campaign", hue="y", data=camp_less_than_20);
plt.title('Distribution of Calls made during a campaign vs Subscribed')
plt.show()
df_campaign = pd.crosstab(index=camp_less_than_20['campaign'],columns=camp_less_than_20['y'])
df_campaign['percentage(yes)'] = (df_campaign['yes'] / (df_campaign['yes'] + df_campaign['no'])) * 100
df_campaign.head()
Age of person (numeric)
sns.distplot(df['age'])
plt.show()
Marital status (categorical: 'divorced','married','single','unknown'; note: 'divorced' means divorced or widowed)
plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
sns.countplot(df['marital'])
plt.title('Distribution of Marital status of the population')
plt.subplot(1,2,2)
sns.countplot(x="marital", hue="y", data=df)
plt.title('Distribution of Marital status of the population against the target variable')
plt.show()
df_marital = pd.crosstab(index=df['marital'],columns=df['y'])
df_marital['percentage(yes)'] = (df_marital['yes'] / (df_marital['yes'] + df_marital['no'])) * 100
df_marital
Outcome of the previous marketing campaign (categorical: 'failure','nonexistent','success')
plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
sns.countplot(df['poutcome'])
plt.title('Distribution of outcome of previous marketing campaign')
plt.subplot(1,2,2)
sns.countplot(x="poutcome", hue="y", data=df)
plt.title('Distribution of outcome of previous marketing campaign against the outcome of current campaign')
plt.show()
df_outcome = pd.crosstab(index=df['poutcome'],columns=df['y'])
df_outcome['percentage(yes)'] = (df_outcome['yes'] / (df_outcome['yes'] + df_outcome['no'])) * 100
df_outcome
Contact communication type (categorical: 'cellular','telephone')
df_calls = pd.crosstab(index=df['contact'],columns=df['y'])
df_calls['percentage(yes)'] = (df_calls['yes'] / (df_calls['yes'] + df_calls['no'])) * 100
df_calls
plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
sns.countplot(df['contact'])
plt.title('Distribution of communication type')
plt.subplot(1,2,2)
sns.countplot(x="contact", hue="y", data=df)
plt.title('Distribution of outcome of previous marketing campaign against the outcome of current campaign')
plt.show()
print df['age'].mean()
print df['age'].median()
df.loc[(df['marital'] == 'unknown') & (df['age'] >= 40), 'marital'] = 'married'
df.loc[(df['marital'] == 'unknown') & (df['age'] < 40), 'marital'] = 'single'
df_1 = pd.crosstab(index=df['job'],columns=df['education'])
df_1
df_unskilled = df[(df.job == 'blue-collar') | (df.job == 'housemaid')]
df_service = df[(df.job == 'admin.') | (df.job == 'services') | (df.job == 'technician')]
df_professional = df[(df.job == 'entrepreneur') | (df.job == 'self-employed') | (df.job == 'management')]
df_student = df[(df.job == 'student')]
df_retired = df[(df.job == 'retired')]
df_unemployed = df[(df.job == 'unemployed')]
df.loc[df_unskilled.index, 'job'] = 'Unskilled'
df.loc[df_service.index, 'job'] = 'Service'
df.loc[df_professional.index, 'job'] = 'Professional'
df.loc[df_student.index, 'job'] = 'Student'
df.loc[df_retired.index, 'job'] = 'Retired'
df.loc[df_unemployed.index, 'job'] = 'Unemployed'
df['job'].unique()
df_job2 = pd.crosstab(index=df_service.job,columns=df_service.education)
df_job2.loc['total'] = df_job2.iloc[:,:].sum()
df_job2
df_job1 = pd.crosstab(index=df_unskilled.job,columns=df_unskilled.education)
df_job1.loc['total'] = df_job1.iloc[:,:].sum()
df_job1
df_edu = pd.crosstab(index=df.job,columns=df.education)
df_edu.loc['total'] = df_edu.iloc[:,:].sum()
df_edu
df_basic_ed = df[(df.education == 'basic.4y') | (df.education == 'basic.6y') | (df.education == 'illiterate')]
df_mid_ed = df[(df.education == 'basic.9y') | (df.education == 'high.school')]
df_degree_ed = df[(df.education == 'professional.course') | (df.education == 'university.degree')]
df.loc[df_basic_ed.index, 'education'] = 'Basic'
df.loc[df_mid_ed.index, 'education'] = 'Mid'
df.loc[df_degree_ed.index, 'education'] = 'Degree'
df['education'].value_counts()
pd.crosstab(index=df.job,columns=df.education)
drop_index = df[(df.job == 'unknown') & (df.education == 'unknown')]['y'].index
print len(drop_index)
df_new = df.drop(drop_index,axis=0)
pd.crosstab(index=df_new.job,columns=df_new.education)
df1 = df_new[(df_new.job == 'unknown') & (df_new.education == 'Basic')].index.values
df2 = df_new[(df_new.job == 'unknown') & (df_new.education == 'Degree')].index.values
df3 = df_new[(df_new.job == 'unknown') & (df_new.education == 'Mid')].index.values
df_new.loc[df1, 'job'] = 'Unskilled'
df_new.loc[df2, 'job'] = 'Service'
df_new.loc[df3, 'job'] = 'Service'
df_new[(df_new.job == 'unknown') & (df_new.education == 'Basic')]['job'].index.values
pd.crosstab(index=df_new.job,columns=df_new.education)
df4 = df_new[(df_new.education == 'unknown') & (df_new.job == 'Professional')].index.values
df5 = df_new[(df_new.education == 'unknown') & (df_new.job == 'Retired')].index.values
df6 = df_new[(df_new.education == 'unknown') & (df_new.job == 'Service')].index.values
df7 = df_new[(df_new.education == 'unknown') & (df_new.job == 'Student')].index.values
df8 = df_new[(df_new.education == 'unknown') & (df_new.job == 'Unemployed')].index.values
df9 = df_new[(df_new.education == 'unknown') & (df_new.job == 'Unskilled')].index.values
df_new.loc[df4, 'education'] = 'Degree'
df_new.loc[df5, 'education'] = 'Basic'
df_new.loc[df6, 'education'] = 'Degree'
df_new.loc[df7, 'education'] = 'Mid'
df_new.loc[df8, 'education'] = 'Mid'
df_new.loc[df9, 'education'] = 'Mid'
pd.crosstab(index=df_new.job,columns=df_new.education)
pd.crosstab(index=df_new.loan,columns=df_new.education)
pd.crosstab(index=df_new.loan,columns=df_new.job)
df_new.to_csv('modified.csv')