<jupyter_start><jupyter_text>Imports<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Getting our Datap1-customers.xlsx - This dataset includes the following information on about 2,300 customers.p1-mailinglist.xlsx - This dataset is the 250 customers that you need to predict sales. This is the list of customers that the company would send a catalog to.<jupyter_code>customers = pd.read_excel('p1-customers.xlsx')
mail_list = pd.read_excel("p1-mailinglist.xlsx")<jupyter_output><empty_output><jupyter_text>Checking the head of our Data, its info() and describe() methods to better understand it.<jupyter_code>customers.head()
mail_list.head()
customers.info()
mail_list.info()
customers.describe()
mail_list.describe()<jupyter_output><empty_output><jupyter_text>EDA: Exploring Variables that can properly predict Avg_Sale_Amount in Customer data using sns.pairplot function, we will plot pairwise relationship between every numeric columns in the Customer Data<jupyter_code>sns.pairplot(customers)<jupyter_output><empty_output><jupyter_text>reviewing the above plots, we can see visually that Avg_Num_Producst_Purchased highly correlates with Avg_Sale_Amount<jupyter_code>sns.jointplot(data=customers, x='Avg_Num_Products_Purchased', y='Avg_Sale_Amount')<jupyter_output><empty_output><jupyter_text>There's a theory that Customer Segment correlates with Avg_Sale_Amount, but we can't assess the relationship between both variables cause  Customer Segment is a categorical Variable. So we are going to convert it into dummy variable  There are four Unique values in the Customer_Segment Column<jupyter_code>customers['Customer_Segment'].nunique()<jupyter_output><empty_output><jupyter_text>Converting the Customer Segment column into a dummy<jupyter_code>customers = pd.get_dummies(customers, columns=['Customer_Segment'])<jupyter_output><empty_output><jupyter_text>Prepping the Customer Data for Regression<jupyter_code>#Target Variable

Y = customers.Avg_Sale_Amount
#Predictor Variables

X = customers.drop(["Avg_Sale_Amount","Name","Customer_ID","Address","City","State","ZIP","Store_Number","Responded_to_Last_Catalog","Years_as_Customer"], axis=1)
X.head()<jupyter_output><empty_output><jupyter_text>Training the ModelNow its time to train our model on our training data!** Import LinearRegression from sklearn.linear_model **<jupyter_code>from sklearn import linear_model<jupyter_output><empty_output><jupyter_text>**Create an instance of a LinearRegression() model named Model.**<jupyter_code>model = linear_model.LinearRegression()<jupyter_output><empty_output><jupyter_text>** Train/fit model on the training data.**<jupyter_code>model.fit(X,Y)<jupyter_output><empty_output><jupyter_text>**Printing out the coefficients of the model**The coefficient value signifies how much the mean of the dependent variable changes given a one-unit shift in the independent variable while holding other variables in the model constant.A positive coefficient indicates that as the value of the independent variable increases, the mean of the dependent variable also tends to increase. A negative coefficient suggests that as the independent variable increases, the dependent variable tends to decrease.<jupyter_code>coeffecients = pd.DataFrame(model.coef_, X.columns, columns=['Coeffecient'])
coeffecients<jupyter_output><empty_output><jupyter_text>Printing out the Intercept<jupyter_code>model.intercept_<jupyter_output><empty_output><jupyter_text>Prepping our Test_Data (Mail_List)<jupyter_code>mail_list.head()<jupyter_output><empty_output><jupyter_text>Converting the Customer Segment column into a dummy<jupyter_code>mail_list = pd.get_dummies(mail_list, columns=['Customer_Segment'])
mail_list.head()
mail_test = mail_list.drop(["Name","Customer_ID","Address","City","State","ZIP","Store_Number","Score_No","Score_Yes","Years_as_Customer"], axis=1)
mail_test.head()<jupyter_output><empty_output><jupyter_text>Predicting Test DataNow that we have fit our model, let's evaluate its performance by predicting off the test values!** Using model.predict() to predict off the mail_test set of the data.**<jupyter_code>Predicted_Profit = model.predict(mail_test)<jupyter_output><empty_output><jupyter_text>Adding the prediction to the original mailing list Data<jupyter_code>mail_list['predicted_profit'] = Predicted_Profit

mail_list.head()<jupyter_output><empty_output><jupyter_text>Applying the business rule of multiplying the predicted profit by score_yes i.e. the probability that customer will respond to catalogScore_Yes: The probability that the customer WILL respond to the catalog and make a purchase.Hint: We want to calculate the expected revenue from these 250 people in order to get expected profit. This means we need to multiply the probability that a person will buy our catalog as well. For example, if a customer were to buy from us, we predict this customer will buy 450 dollars worth of products. At a 30 percent chance that this person will actually buy from us, we can expect revenue to be 450 x 30% = $135."<jupyter_code>mail_list['expected_revenue'] = mail_list['predicted_profit'] * mail_list['Score_Yes']
mail_list.head()<jupyter_output><empty_output><jupyter_text>Taking the sum of all expected_Revenue for all the customers in the mailing list<jupyter_code>Sum_Expected_Revenue = mail_list['expected_revenue'].sum() 
Sum_Expected_Revenue<jupyter_output><empty_output><jupyter_text>Calculating FInal Profit for all catalog for every customer in the mailing listcost for printing all the catalog is 6.5 * 250 The average gross margin (price - cost) on all products sold through the catalog is 50%.<jupyter_code>Profit = (Sum_Expected_Revenue * 0.5) - (6.5 * 250)<jupyter_output><empty_output><jupyter_text>Profit Predicted (22,047) is the money the company can expect to earn from sending out a catalog to new customers and it exceeds the 10,000$ limit set by the business, with this information we can recommend Management to proceed to sending out the catalog to the new 250 customers.<jupyter_code>Profit<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08143111542820139, 'fraction_numerical': 0.00906588959041606, 'mean_word_length': 8.193452380952381, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '252543', 'license': 'MIT', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1834, 'n_words': 572, 'path': 'P2 - Predicting Catalog Demand/Solution using Python/Predicting Catalog Demand.ipynb', 'repo_name': 'JoshuaOkafor/Udacity-Predictive-Analytics-for-Business-Nanodegree', 'size': '6177'}