"""
# Introduction
"""
"""
Facebook is an American online social media and social networking service owned by Meta Platforms. Founded in 2004 by Mark Zuckerberg with fellow Harvard College students and roommates Eduardo Saverin, Andrew McCollum, Dustin Moskovitz, and Chris Hughes, its name comes from the face book directories often given to American university students. Membership was initially limited to Harvard students, gradually expanding to other North American universities and, since 2006, anyone over 13 years old. As of 2020, Facebook claimed 2.8 billion monthly active users, and ranked seventh in global internet usage.


"""
"""
# Importing Packages for Analysis
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
"""
# Import of the data file in CSV format
"""
df = pd.read_csv('../input/facebook-ad-campaign/data.csv')
"""
# Exploratory Data Analysis (EDA)
"""
"""
**Top Five values in the Datset**
"""
df.head()
"""
**Finding out the number of null values in each column**
"""
df.isnull().sum()
"""
**Removing all empty rows**
"""
df.dropna(inplace=True)
df.info()
"""
**Descriptive Statistics of the Dataset**
"""
df.describe()
"""
**Setting Index of the dataset**
"""
Index=np.arange(1,762)
df.set_index(Index,inplace=True)
"""
**Pairplot of the Dataset**
"""
sb.pairplot(df,hue='gender')
"""
**The targetted Age in the Ad Campaign**
"""
df.age.value_counts().plot(kind='bar')
plt.xticks(rotation=45)
plt.xlabel('Age')
plt.title('Targetted Age in the Ad Campaign')
"""
* The Ad Campaigns are mostly made for the age of 30 to 34 
* The campaign is least targetted for the middle aged people between 45 to 49
"""
"""
**Correlation throught Heatmap**
"""
ax=plt.figure(figsize=(10,6))
corr=df.corr()
sb.heatmap(corr,linewidths=1,linecolor='black',annot=True)
plt.xticks(rotation=45)
"""
The correlated valued lies between 0.7 and 1 
"""
"""
**Genders targetted for the Ad Campaign**
"""
df.gender.value_counts()
"""
* The Ads are targetted mainly for the Men as it is around 2x that of Female
"""
"""
**Finding out who spent the maximum on Ad Campaign**
"""
df[df['spent'] ==df['spent'].max()]
"""
* The maximum amount spend on Ads is around 640 

"""
"""
**Maximum number of clicks received by an ID**
"""
df[df['clicks'] ==df['clicks'].max()]
"""
Id 1178 has received the maximum number of clicks of 340 by spending around 640 
"""
"""
**Total conversions by each Id**
"""
tc=(df.groupby(['campaign_id'])).total_conversion.sum()
tc
"""
Id 1178 has received the maximum number of conversions of 1050
"""
"""
**Total Approved conversions by each Id**
"""
ac=(df.groupby(['campaign_id'])).approved_conversion.sum()
ac
"""
Id 1178 has received the maximum number of approved conversions of 378
"""
"""
**Percentage of Ads approved**
"""
percent_approved = (ac/tc)*100
percent_approved
"""
Id 916 has received the highest percentage number of approved conversions of 41.38%
"""
"""
**Finding out total amount spent of Ads**
"""
(df.groupby(['campaign_id'])).spent.sum()
"""
Id 936 has spent the highest with 296936.80
"""
"""
**Total Number of impressions received**
"""
(df.groupby(['campaign_id'])).impressions.sum()
"""
Id 1178 has received the highest number of impressions of 69902476
"""
"""
**Total Number of clicks received**
"""
(df.groupby(['campaign_id'])).clicks.sum()
"""
Id 1178 has received the highest number of impressions of 9577
"""
"""
# Regression Analysis
"""
"""
**Assigning independant variables(X) and dependant variable(y)**
"""
X=df[['total_conversion','approved_conversion']]
y=df[['spent']]
"""
**Importing train test split**
"""
from sklearn.model_selection import train_test_split
"""
**Setting test size to 20%**
"""
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
"""
**Importing linear regression**
"""
from sklearn.linear_model import LinearRegression
lr= LinearRegression()
"""
**Fitting of the data and predicting the values**
"""
lr.fit(X_train, y_train)
pred = lr.predict(X_test)
"""
**Finding out the R-squared value**
"""
from sklearn.metrics import r2_score
r2=r2_score(y_test,pred)
r2
"""
The R-squared value is 0.87
"""
"""
**Conclusion :** The amount spent is related to the conversions, the more you spent, the more you get reach
"""
"""
# Thank You
"""