<jupyter_start><jupyter_text>--------------------- Project: Marketing Campaign Analysis**Marks:60**--------------------Welcome to the project on Foundations for Data Science. In this project, we aim to analyze marketing data and address some important business problems/questions.--------------------- Context--------------------Marketing Analytics broadly refers to the practice of using analytical methods and techniques to understand the effectiveness of various marketing activities and deploy data-driven decisions to optimize for ROI on conversion rates. It typically involves analyzing various metrics around customer engagement with various marketing activities including but not limited to, ATL (above the line) marketing activities, BTL (below the line) campaigns, targeting personalized offers. Typically the variables of interest are customer profile, campaign conversion rates, and costs associated with various marketing channels. These can generate valuable insights that can help an organization form better marketing strategies, optimize/innovate on delivery, and achieve overall growth. --------------------- Problem Statement--------------------Company 'All You Need' has hired you as a Data Scientist and you've been told by the Chief Marketing Officer that recent marketing campaigns have not been as effective as they were expected to be and the conversion rate is very low.Your task is to analyze the related data, understand the problem, and identify key insights and recommendations for the CMO to potentially implement.  The data set marketing_data.csv consists of 2,240 customers of All You Need company with data on:- Campaign successes/failures- Product preferences- Channel performances- Customer profiles based on the spending habits------------------- Data Dictionary-------------------- ID : Unique ID of each customer- Year_Birth : Age of the customer- Education : Customer's level of education- Marital_Status : Customer's marital status- Kidhome : Number of small children in customer's household- Teenhome : Number of teenagers in customer's household- Income : Customer's yearly household income- Recency : Number of days since the last purchase- MntFishProducts : The amount spent on fish products in the last 2 years- MntMeatProducts : The amount spent on meat products in the last 2 years- MntFruits : The amount spent on fruits products in the last 2 years- MntSweetProducts : Amount spent on sweet products in the last 2 years- MntWines : The amount spent on wine products in the last 2 years- MntGoldProds : The amount spent on gold products in the last 2 years- NumDealsPurchases : Number of purchases made with discount- NumCatalogPurchases : Number of purchases made using catalog (buying goods to be shipped through the mail)- NumStorePurchases : Number of purchases made directly in stores- NumWebPurchases : Number of purchases made through the company's website- NumWebVisitsMonth : Number of visits to company's website in the last month- AcceptedCmp1 : 1 if customer accepted the offer in the first campaign, 0 otherwise - AcceptedCmp2 : 1 if customer accepted the offer in the second campaign, 0 otherwise- AcceptedCmp3 : 1 if customer accepted the offer in the third campaign, 0 otherwise- AcceptedCmp4 : 1 if customer accepted the offer in the fourth campaign, 0 otherwise- AcceptedCmp5 : 1 if customer accepted the offer in the fifth campaign, 0 otherwise- AcceptedCmp6 : 1 if customer accepted the offer in the last campaign, 0 otherwise - Complain : 1 If the customer complained in the last 2 years, 0 otherwise- Country: Country customer belongs to  Importing libraries and overview of the dataset<jupyter_code># Library to supress warnings or deprecation notes 
import warnings
warnings.filterwarnings('ignore')

# Libraries to help with reading and manipulating data
import numpy as np
import pandas as pd

# Libraries to help with data visualization
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
# from google.colab import files
# uploaded = files.upload()<jupyter_output><empty_output><jupyter_text>Load the dataset<jupyter_code># loading the datset

df = pd.read_csv('Marketing data.csv')
df.head()<jupyter_output><empty_output><jupyter_text>Check info of the dataset<jupyter_code>#Checking the info

df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 2240 entries, 0 to 2239
Data columns (total 27 columns):
 #   Column               Non-Null Count  Dtype  
---  ------               --------------  -----  
 0   ID                   2240 non-null   int64  
 1   Year_Birth           2240 non-null   int64  
 2   Education            2240 non-null   object 
 3   Marital_Status       2240 non-null   object 
 4   Income               2216 non-null   float64
 5   Kidhome              2240 non-null   int64  
 6   Teenhome             2240 non-null   int64  
 7   Recency              2240 non-null   int64  
 8   MntWines             2240 non-null   int64  
 9   MntFruits            2240 non-null   int64  
 10  MntMeatProducts      2240 non-null   int64  
 11  MntFishProducts      2240 non-null   int64  
 12  MntSweetProducts     2240 non-null   int64  
 13  MntGoldProds         2240 non-null   int64  
 14  NumDealsPurchases    2240 non-null   int64  
 15  NumWebPurchases      2240 non-null   i[...]<jupyter_text>**Observations:**- There are a total of 27 columns and 2,240 observations in the dataset- We can see that the Income column has less than 2,240 non-null values i.e. column has missing values. We'll explore this further  Let's check the percentage of missing values for the Income column.<jupyter_code># % Null values in the Income column

(df.isnull().sum()/df.shape[0]*100)['Income']<jupyter_output><empty_output><jupyter_text>**Observations:**- Income has ~1.07% missing values.  Let's create a list for numerical columns in the dataset and check the summary statistics   **Question 1: Find the summary statistics for numerical columns and write your observations. (use describe function).** - 4 Marks<jupyter_code># num_cols contain numerical varibales 
num_cols=['Year_Birth','Income','Recency', 'MntWines', 'MntFruits',
       'MntMeatProducts', 'MntFishProducts', 'MntSweetProducts',
       'MntGoldProds', 'NumDealsPurchases', 'NumWebPurchases',
       'NumCatalogPurchases', 'NumStorePurchases', 'NumWebVisitsMonth','Kidhome',
       'Teenhome']
# printing descriptive statistics of numerical columns

#Uncomment the following code and fill in the blanks
#df[___]._____.T<jupyter_output><empty_output><jupyter_text>**Observations:________**  Let's create a list for categorical columns in the dataset and check the count of each category<jupyter_code>#cat_cols contain categorical variables
cat_cols=['Education', 'Marital_Status', 'AcceptedCmp3', 'AcceptedCmp4', 'AcceptedCmp5', 'AcceptedCmp1',
       'AcceptedCmp2', 'AcceptedCmp6', 'Complain', 'Country']
# Printing the count of each unique value in each column

for column in cat_cols:
    print(df[column].value_counts(normalize=True))
    print("-" * 40)<jupyter_output>Graduation    0.503125
PhD           0.216964
Master        0.165179
2n Cycle      0.090625
Basic         0.024107
Name: Education, dtype: float64
----------------------------------------
Married     0.385714
Together    0.258929
Single      0.214286
Divorced    0.103571
Widow       0.034375
Alone       0.001339
Absurd      0.000893
YOLO        0.000893
Name: Marital_Status, dtype: float64
----------------------------------------
0    0.927232
1    0.072768
Name: AcceptedCmp3, dtype: float64
----------------------------------------
0    0.935714
1    0.064286
Name: AcceptedCmp4, dtype: float64
----------------------------------------
0    0.925446
1    0.074554
Name: AcceptedCmp5, dtype: float64
----------------------------------------
0    0.927232
1    0.072768
Name: AcceptedCmp1, dtype: float64
----------------------------------------
0    0.850893
1    0.149107
Name: AcceptedCmp2, dtype: float64
----------------------------------------
0    0.986607
1    0.013393
Name: AcceptedCmp6[...]<jupyter_text>**Observations:**- In education, 2n cycle and Master means the same thing. We can combine these two categories.- There are many categories in marital status. We can combine the category 'Alone' with 'Single'.- It is not clear from the data that what do the terms 'Absurd', and 'YOLO' actually mean. We can combine these categories to make a new category - 'Others'.- There are only 21 customers who complained in the last two years.- The majority of the customers belong to Spain and least to Mexico.- The most common educational status is Graduation- The most common marital status is Married  Data Preprocessing and Exploratory Data AnalysisIn this section, we will first prepare our dataset for analysis.- Fixing the categories- Creating new columns as the total amount spent, total purchase made, total kids at home, and total accepted campaigns- Dealing with missing values and outliers- Extract key insights from the data  Replacing the "2n Cycle" category with "Master" in Education and "YOLO", "Alone", and "Absurd" categories with "Single" in Marital_Status<jupyter_code># Replacing 2n Cycle with Master

df["Education"].replace("2n Cycle", "Master", inplace=True)
# Replacing YOLO, Alone, Absurd with Single

df["Marital_Status"].replace(["Alone",], "Single", inplace=True)
df['Marital_Status'].replace(["Absurd", "YOLO"], "Others", inplace=True)<jupyter_output><empty_output><jupyter_text>We have fixed the categories in the Marital_Status. Now, let's see the distribution count in different categories for marital status.<jupyter_code>df.Marital_Status.value_counts()<jupyter_output><empty_output><jupyter_text>**Observation**:- The majority of customer belong to married category and the other category have only 4 observations.  Creating new features from the existing features<jupyter_code># creating new features to get overll picture of a customer, how much he/she has spend, how many children he/she has, total campaigns accepted etc.


# total spending by a customer
spending_col = [col for col in df.columns if 'Mnt' in col]
df['Total_Spending'] = df[spending_col].sum(axis = 1) 

#total purchases made by a customer
platform_col = [col for col in df.columns if 'Purchases' in col]
df['Total_Purchase'] = df[platform_col].sum(axis = 1) 

#total no. of childern
df['NumberofChildren'] = df['Kidhome'] + df['Teenhome']  

# Total no. of campaign accepted by a customer
campaigns_cols = [col for col in df.columns if 'Cmp' in col]
df['TotalCampaignsAcc'] = df[campaigns_cols].sum(axis=1)<jupyter_output><empty_output><jupyter_text>Let's check outliers for new variables - Total_Spending, Total_Purchase. Also, let's analyze the Year_Birth column as we observed above that it had a minimum value of 1893.<jupyter_code># Plotting boxplot for Year_Birth, Total_Spending, Total_Purchase

cols=['Year_Birth','Total_Spending','Total_Purchase']
for i in cols:
    sns.boxplot(x=df[i])
    plt.show()<jupyter_output><empty_output><jupyter_text>**Observations:**- The birth year is reported as <=1900 for some users, while the current year is 2021. it's very unlikely that the person is alive. it may be a reporting error.- There are some outliers in total spending and total purchase. - The observations marked as outliers are very closed to the upper whisker and some extreme points can be expected for variables like total spending. We can leave these outliers untreated.  Let's check the number of observations for which year birth is less than 1900.<jupyter_code>df[df['Year_Birth'] < 1900]<jupyter_output><empty_output><jupyter_text>**Observation**:- There are only 2 observations for which birth year is less than 1900. We can drop these observations.<jupyter_code>#keeping data for customers having birth year >1900

df = df[df['Year_Birth'] > 1900]<jupyter_output><empty_output><jupyter_text>Check the outliers and impute the missing values for the Income variable<jupyter_code>#plotting Boxplot for income

plt.figure(figsize=(10,4))
sns.boxplot(df['Income'])
plt.title('Income boxplot', size=16)
plt.show()<jupyter_output><empty_output><jupyter_text>**Observations:**- We can see from the boxplot that there are some outliers in the income variable.- Let's find the value at upper whisker to check how many observations are marked as outliers.<jupyter_code>#Calculating the upper whisker for the Income variable

Q1 = df.quantile(q=0.25) #First quartile
Q3 = df.quantile(q=0.75) #Third quartile
IQR = Q3 - Q1            #Inter Quartile Range

upper_whisker = (Q3 + 1.5*IQR)['Income']   #Upper Whisker
print(upper_whisker)
#Checking the observations marked as outliers
df[df.Income>upper_whisker]<jupyter_output><empty_output><jupyter_text>**Observations**:- We have only 8 observations with an income greater than the upper whisker. - Only 3 observations (ID- 4931, 1501, 8475) out of 8 outliers have purchased more than 11 times in the last 2 years.- Other 5 observations have very less amount of total spending. **Let's compare the summary statistics for these observations with observations on the other side of the upper whisker.**<jupyter_code>#Checking the summary statistics for observations marked as outliers
df[df.Income>upper_whisker].describe().T
#Checking the summary statistics for observations not marked as outliers
df[df.Income<upper_whisker].describe().T<jupyter_output><empty_output><jupyter_text>**Observations**:- None of the outliers have accepted any of the campaigns or have submitted any complaints in the last 2 years.- We can see that customers who are outliers have lower mean expenditure per customer for all the products except meat products.- The outliers have a higher number of catalog purchases on average and very low number of web purchases.- We can drop the 5 observations at indices [527, 731, 853, 1826, 1925] as they would not add value to our analysis.<jupyter_code>#Dropping 5 observations at indexes 527, 731, 853, 1826, 1925
df.drop(index=[527, 731, 853, 1826, 1925], inplace=True)<jupyter_output><empty_output><jupyter_text>Check the distribution for Income<jupyter_code>#plotting displot for income

#uncomment the lines of code given below
sns.displot(df['Income'], kde=True, height=5, aspect=2)
plt.title('Income distribution', size=16, )
plt.ylabel('count');<jupyter_output><empty_output><jupyter_text>**Observations:**- After treating outliers, the distribution for the income variable is close to normal distribution with very few extreme observations to the right.- We will replace the missing values for the income variable with the median, and not mean, as the variable is slightly skewed to the right<jupyter_code>#filling null values with median

df['Income'].fillna(df.Income.median(), inplace=True)<jupyter_output><empty_output><jupyter_text>Analyzing all the campaigns  **Question 2: Write your observations on acceptance rate for each campaign given in the below plot.** - 4 Marks  Let's find out what is the acceptance rate for each campaign?<jupyter_code># PLotting the % acceptance for every campaign

Camp_cols=['AcceptedCmp1', 'AcceptedCmp2', 'AcceptedCmp3', 'AcceptedCmp4', 'AcceptedCmp5', 'AcceptedCmp6']

success_campaign=(df[Camp_cols].sum()/df.shape[0])*100

# plot
success_campaign.plot(kind='bar', figsize=(6,6))
plt.ylabel("Perentage")
plt.show()<jupyter_output><empty_output><jupyter_text>**Observations:_______**  Let's analyze what kind of customer are accepting campaigns?<jupyter_code>#uncomment the lines of code given below
plt.figure(figsize=(8,8))
sns.swarmplot(x='TotalCampaignsAcc', y='Income', data=df)
plt.show()<jupyter_output><empty_output><jupyter_text>**Observations:**- Higher the income higher the number of campaigns accepted.<jupyter_code># Let's see the mean income of customers
df.Income.mean()<jupyter_output><empty_output><jupyter_text>**Question 3: Write your observations on acceptance rate for each campaign according to the income level.** - 7 Marks The mean income of customers is close to 52K. Let's divide the income into 2 segments of income>52k and income<52k and see the acceptance rate in each segment.<jupyter_code># making dataframes of customers having income <52k and >52K
df1=df[df.Income<52000]
df2=df[df.Income>52000]

Camp_cols=['AcceptedCmp1', 'AcceptedCmp2', 'AcceptedCmp3', 'AcceptedCmp4', 'AcceptedCmp5', 'AcceptedCmp6']

#Calculating success rate of each campaing for both segments 
success_campaign1=pd.DataFrame((df1[Camp_cols].sum()/df1.shape[0])*100, columns=['Income <52K'])

success_campaign2=pd.DataFrame((df2[Camp_cols].sum()/df2.shape[0])*100, columns=['Income >52K'])

new_df=pd.concat([success_campaign1, success_campaign2], axis=1)

# plot
plt.figure(figsize=(8,8))
sns.lineplot(data=new_df)
plt.title("Percentage Acceptance of each campaign")
plt.ylabel("Percentage Acceptance of a campaign")
plt.show()<jupyter_output><empty_output><jupyter_text>**Observations:__________** Let's find out who has accepted the last campaign and what could be the reason?<jupyter_code>#uncomment the lines of code given below.

df[df['AcceptedCmp6']==1].shape<jupyter_output><empty_output><jupyter_text>- There are only 30 customers who have accepted the last campaign.- Let's check if these customers are new or they have accepted previous campaigns as well.<jupyter_code>grouped2=df.groupby('AcceptedCmp6').mean()['TotalCampaignsAcc']
grouped2<jupyter_output><empty_output><jupyter_text>**Observations:**- We know that the maximum number of campaigns any customer has accepted is 5.- We can observe that the value for TotalCampaignsAcc is ~3.6 for customers who have accepted the last campaign.- This implies that these 30 customers are those loyal customers who have been accepting most of the campaigns.  It could be that different campaigns are focussed on different set of products. Let's check if the product preference for those who accepted the campaigns is different from those who didn't - using amount spent and number of purchases Let's define a function which will take the column name for the product as input and will generate the barplot for every campaign and average amount spent on a product<jupyter_code>def amount_per_campaign(columns_name):
    p1=pd.DataFrame(df.groupby(['AcceptedCmp1']).mean()[columns_name]).T
    p2=pd.DataFrame(df.groupby(['AcceptedCmp2']).mean()[columns_name]).T
    p3=pd.DataFrame(df.groupby(['AcceptedCmp3']).mean()[columns_name]).T
    p4=pd.DataFrame(df.groupby(['AcceptedCmp4']).mean()[columns_name]).T
    p5=pd.DataFrame(df.groupby(['AcceptedCmp5']).mean()[columns_name]).T
    p6=pd.DataFrame(df.groupby(['AcceptedCmp6']).mean()[columns_name]).T
    pd.concat([p1,p2,p3,p4,p5,p6],axis=0).set_index([Camp_cols]).plot(kind='line', figsize=(8,8))
    plt.ylabel('Average amount spend on' + ' ' + columns_name)
    plt.show()<jupyter_output><empty_output><jupyter_text>Use the function defined above to generate barplots for different purchasing Products<jupyter_code>#here is an example showing how to use this fuction on the column MntWines
amount_per_campaign('MntWines')<jupyter_output><empty_output><jupyter_text>**Observations:**- For the customers accepting campaign 3, 4, 5, and 6 the average amount spent on wine is quite high.  **Question 4: Write the code and your observations on average amount spent on different products across all campaigns.** - 7 Marks<jupyter_code>#meat products

#call the function amount_per_campaign for MntMeatProducts

# Fruit products

#call the function amount_per_campaign for MntFruits
# gold products

#call the function amount_per_campaign for MntGoldProds
#sweet products

#call the function amount_per_campaign for MntSweetProducts<jupyter_output><empty_output><jupyter_text>**Observations:__________**  We have analyzed the relationship between campaigns and different products. Now, let's see the relationship of campaigns with different purchasing channels. We have a defined a function which will take the column name of the channel name as input and will generate the barplot for every campaign and average purchase made through that channel if the campaign is accepted<jupyter_code>def Purchases_per_campaign(columns_name):
    dp1=pd.DataFrame(df.groupby(['AcceptedCmp1']).mean()[columns_name]).T
    dp2=pd.DataFrame(df.groupby(['AcceptedCmp2']).mean()[columns_name]).T
    dp3=pd.DataFrame(df.groupby(['AcceptedCmp3']).mean()[columns_name]).T
    dp4=pd.DataFrame(df.groupby(['AcceptedCmp4']).mean()[columns_name]).T
    dp5=pd.DataFrame(df.groupby(['AcceptedCmp5']).mean()[columns_name]).T
    dp6=pd.DataFrame(df.groupby(['AcceptedCmp6']).mean()[columns_name]).T
    pd.concat([dp1,dp2,dp3,dp4,dp5,dp6],axis=0).set_index([Camp_cols]).plot(kind='line', figsize=(8,8))
    plt.ylabel('Average' + ' ' + columns_name)
    plt.show()
#here is an example showing how to use this fuction on the column NumDealsPurchases
Purchases_per_campaign('NumDealsPurchases')<jupyter_output><empty_output><jupyter_text>**Observations:**- For the customers accepting campaign 3, 4, and 6 the average deals purchase is quite low.  **Question 5: Write the code and your observations on average number of purchases from different channels across all campaigns.** - 7 Marks<jupyter_code># store purchase

#call the function Purchases_per_campaign for NumStorePurchases
#Catalog purchase

#call the function Purchases_per_campaign for NumCatalogPurchases
#Web purchases

#call the function Purchases_per_campaign for NumWebPurchases<jupyter_output><empty_output><jupyter_text>**Observations:_______**<jupyter_code>#Recency

#uncomment the line of code given below.
Purchases_per_campaign('Recency')<jupyter_output><empty_output><jupyter_text>**Observations:**- Average recency of the customers who accepted campaign 2 is quite low which implies that campaign 2 was accepted by the customers who recently purchased an item.   We have analyzed the relationship between campaigns and numerical variables. Let's see the relationship of campaigns with different categorical variables We will check the percentage acceptance of each campaign with respect to each category in the categorical variable. The percentage acceptance is calculated as number of customers who have accepted the campaign to the total number of customers.<jupyter_code>def Cat_Campaign_Relation(df, column_name):
    e1=(df.groupby([column_name]).sum()['AcceptedCmp1']/df.groupby([column_name]).count()['AcceptedCmp1'])
    e2=(df.groupby([column_name]).sum()['AcceptedCmp2']/df.groupby([column_name]).count()['AcceptedCmp2'])
    e3=(df.groupby([column_name]).sum()['AcceptedCmp3']/df.groupby([column_name]).count()['AcceptedCmp3'])
    e4=(df.groupby([column_name]).sum()['AcceptedCmp4']/df.groupby([column_name]).count()['AcceptedCmp4'])
    e5=(df.groupby([column_name]).sum()['AcceptedCmp5']/df.groupby([column_name]).count()['AcceptedCmp5'])
    e6=(df.groupby([column_name]).sum()['AcceptedCmp6']/df.groupby([column_name]).count()['AcceptedCmp6'])
    df_new=pd.concat([e1,e2,e3,e4,e5,e6],axis=1).T
    plt.figure(figsize=(8,8))
    sns.lineplot(data=df_new, markers=True, linewidth=2)
    plt.ylabel('Percentage Acceptance')
    plt.show()
#here is an example showing how to use this fuction on the column Education
Cat_Campaign_Relation(df, 'Education')<jupyter_output><empty_output><jupyter_text>**Observations:**- More than 20% of the customers with Ph.D have accepted campaign 2.- Customers with basic education have only accepted campaign 1 and 2.- Except customers with basic education level, all education levels follow the same trend.  **Question 6: Write the code and your observations on percentage acceptance for different categorical variables across all campaigns.** - 7 Marks<jupyter_code>#NumberofChildren

#call the function Cat_Campaign_Relation for NumberofChildren
#Let's filter the observations with 'Others' category as they are only 4 such observations
df_rest=df[df.Marital_Status!='Others']

#call the function Cat_Campaign_Relation for NumberofChildren with dataframe df_rest
#Let's filter the observations for 'ME' country as they are only 3 such observations
df_not_mexico=df[df.Country!='ME']

#Plot
plt.figure(figsize=(8,8))
sns.heatmap((df_not_mexico.groupby('Country').sum()[Camp_cols]/df_not_mexico.groupby('Country').count()[Camp_cols])*100, annot=True, fmt='0.2f', cmap="YlGnBu")<jupyter_output><empty_output><jupyter_text>**Observation:_______**  Check the product preferences by customers<jupyter_code>#creating a list which contain all the columns name of products

mnt_cols = [col for col in df.columns if 'Mnt' in col]

spending=df[mnt_cols].mean(axis=0)
spending.plot(kind='bar', figsize=(10,5))
plt.ylabel("Average spend by customers")
plt.show()<jupyter_output><empty_output><jupyter_text>**Observations**:- The mean amount spent by customers in the last 2 years is highest for wines followed by meat products. Let's check if the product preferences are similar for different types of customers. We will calculate the percentage amount spent by customers on a product for each category with respect to the total spending by customers belonging to that category.<jupyter_code>def amount_per_category(df, column_name):
    df_new1=((df.groupby([column_name]).sum()[mnt_cols].T)/df.groupby([column_name]).sum()['Total_Spending'])
    plt.figure(figsize=(10,8))
    sns.heatmap(df_new1.T, annot=True, cmap="YlGnBu")
    plt.show()
# plot showing the percentage of total spending of different products by a group of customers having same education level

amount_per_category(df, 'Education')<jupyter_output><empty_output><jupyter_text>**Observations:**- Customers with PhD spend ~60% of their total spending on wines.- Customers with Graduation and Master's spend ~45-50% of their total spending on wines.- Customers with Graduation and Master's spend ~27-29% of their total spending on meat.- Customers with PhD spend ~25% of their total spending on meat.- Customers having education level Master or PhD spend ~80% on meat and wines.- Customers with basic education spend more on Fruits, Fish, Sweet, and Gold products.  **Question 7: Write the code and your observations on percentage amount spent on different products for each category of the mentioned categorical variables.** - 7 Marks<jupyter_code>#call the function amount_per_category for Marital_Status with dataframe df_rest
#call the function amount_per_category for Country with dataframe df_not_mexico<jupyter_output><empty_output><jupyter_text>**Observations:_______**  Check different channel performances  Let's calculate the percentage of purchases for all the channels.<jupyter_code># list of cols for channels

channel_cols = [col for col in df.columns if 'Purchases' in col] 

#making dataframe of columns having purchase and taking sum of them.
channels = pd.DataFrame(df[channel_cols].sum()/df.Total_Purchase.sum(), columns=['NumberofPurchases'])

# plot
channels.plot(kind='bar', figsize=(6,6))
plt.ylabel("Percentage Purchases")
plt.show()<jupyter_output><empty_output><jupyter_text>**Observations**:- We can see that the most purchases are from the stores followed by web purchases.- Number of deal purchases and catalog purchases are low.  **Question 8: Write your observations on percentage purchases from different channels for different categories of the income_cat column.** - 4 Marks Let's check how number of purchases via different channels varies for different income bins.<jupyter_code>#Binning the income column
df['income_cat']=pd.qcut(df.Income, q=[0, 0.25, 0.50, 0.75, 1], labels=['low', 'medium', 'high', 'very_high'])
group=df.groupby('income_cat').sum()[channel_cols]
(group/group.sum()).plot(kind='bar', figsize=(8,8))
plt.ylabel("Percentage Purchases")
plt.show()<jupyter_output><empty_output><jupyter_text>**Observations:__________**  We can also visualize the correlation by purchases from different channels and income of the customer.  **Question 9: Find the correlation matrix for the columns mentioned below and visualize the same using heatmap.** - 3 Marks<jupyter_code>corr=df[['Income', 'NumDealsPurchases', 'NumWebPurchases', 'NumCatalogPurchases', 'NumStorePurchases' ]].corr()

#Write your code here<jupyter_output><empty_output><jupyter_text>**Observations:____** As we know from our analysis we have done so far that customers with income, number of children, and amount spending on wines are the important factors.  Let's try to come up with new customer profile on the basis of these 3 attributes and check what would be the acceptance rate for that customer profile.<jupyter_code>df3=df[df.Income>52000]
df4=df3[df3.MntWines>df3.MntWines.mean()]
new_profile=df4[df4.NumberofChildren==0]
#Calculating success rate of each campaing for both segments 
success_campaign3=pd.DataFrame(success_campaign, columns=['Overall Acceptance'])

success_campaign4=pd.DataFrame((new_profile[Camp_cols].sum()/new_profile.shape[0])*100, columns=['New Customer Profile Acceptance '])

# plot
pd.concat([success_campaign3, success_campaign4], axis=1).plot(kind='bar', figsize=(8,6))
plt.title("")
plt.ylabel("")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10533920462364112, 'fraction_numerical': 0.026833631484794274, 'mean_word_length': 5.056041666666666, 'pattern_counts': {'":': 0, '<': 163, '<?xml version=': 0, '>': 166, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '39', 'dir': 'jupyter-structured-clean-dedup', 'id': '432691', 'license': 'MIT', 'n_tokens_mistral': 9082, 'n_tokens_neox': 8408, 'n_words': 3045, 'path': 'MIT ADSP/Foundations for Data Science/Learner_Notebook_Project_Marketing_Campaign_Analysis.ipynb', 'repo_name': 'Victor-Dimitrov/ML-projects', 'size': '29068'}