<jupyter_start><jupyter_text>Portfolio Exercise Starbucks  Background InformationThe dataset you will be provided in this portfolio exercise was originally used as a take-home assignment provided by Starbucks for their candidates.   The data for this exercise consists of about 120,000 data points split in 2:1 ratio among training and test files. Each data point includes one column indicating whether or not an individual was sent a promotion for a specific product, and one column indicating whether or not that individual eventually purchased that product. Each individual also had seven additional features associated with them. Optimization StrategyYour task is to use the training data to understand what patterns in V1-V7 to indicate that a promotion should be provided to a user.  Specifically, your goal is to maximize the following metrics:* **Incremental Response Rate (IRR)** Ratio of the number of purchasers in the promotion group to the total number of customers in the purchasers group minus the ratio of the number of purchasers in the non-promotional group to the total number of customers in the non-promotional group.* **Net Incremental Revenue (NIR)**The total number of purchasers that received the promotion times 10 minus the number of promotions given times 0.15 minus the number of purchasers who were not given the promotion times 10.For a full description of what starbucks provides to candidates see the [instructions available here](https://drive.google.com/open?id=18klca9Sef1Rs6q8DW4l7o349r8B70qXM).Below you can find the training data provided.  Explore the data and different optimization strategies. How To Test Your Strategy?When you feel like you have an optimization strategy, complete the **promotion_strategy** function to pass to the **test_results** function.  From past data, we know there are four possible outomes:Table of actual promotion vs. predicted promotion customers.  |   | Actual      |    |    | |---|-------------|----|----|| **Predicted**   | Yes| No |  | Yes             | **I**   | **II**|  | No              | **III** | **IV**|  The metrics are only being compared for the individual's we predict should obtain the promotion - that is quadrants I and II here.  Since the first set of individuals that receive the promotion (in the training set) receive it randomly, we can expect that quadrants I and II will have approximately equal participants.  Comparing quadrant I to II then gives an idea of how well your promotion strategy will work in the future. Get started by reading in the data below.  See how each variable or combination of variables along with a promotion influences the chance of purchasing.  When you feel like you have a strategy for who should receive a promotion, test your strategy against the test dataset used in the final test_results function.<jupyter_code># load in packages
from itertools import combinations

from test_results import test_results, score
import numpy as np
import pandas as pd
import scipy as sp
import sklearn as sk

import matplotlib.pyplot as plt
import seaborn as sb
%matplotlib inline

# load in the data
train_data = pd.read_csv('data/training.csv')
train_data.head()<jupyter_output><empty_output><jupyter_text>* **Incremental Response Rate (IRR)** Ratio of the number of purchasers in the promotion group to the total number of customers in the purchasers group minus the ratio of the number of purchasers in the non-promotional group to the total number of customers in the non-promotional group.<jupyter_code>yes_df, no_df = train_data[train_data.Promotion.str.contains('Yes')], train_data[train_data.Promotion.str.contains('No')]  
y_promo, y_no = yes_df.purchase.sum(), no_df.purchase.sum()

m_promo, m_no = len(yes_df), len(no_df)

iir = y_promo / m_promo - y_no / m_no
print(f'IIR: {iir:.5f}')<jupyter_output>IIR: 0.00945<jupyter_text>* **Net Incremental Revenue (NIR)**The total number of purchasers that received the promotion times 10 minus the number of promotions given times 0.15 minus the number of purchasers who were not given the promotion times 10.<jupyter_code>nir = y_promo * 10 - m_promo * .15 - y_no * 10
print(f'NIR: {nir:.2f}')
def nir(y_promo, m_promo, y_no):
    return y_promo * 10 - m_promo * .15 - y_no * 10
train_data.corr()
    
yes_df.corr()
no_df.corr()
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix
train_data.Promotion = train_data.Promotion.apply(lambda x: 1 if x == 'Yes' else 0)
train_data

x_train, y = train_data.drop(columns=['ID', 'purchase']), train_data['purchase']
rfc = RandomForestClassifier()
rfc.fit(x_train, y)
for c, f in zip(x_train.columns, rfc.feature_importances_):
    print(f'{c}: {f}')
test_data = pd.read_csv('data/Test.csv')
test_data.Promotion = test_data.Promotion.apply(lambda x: 1 if x == 'Yes' else 0)
x_test, y_test = test_data.drop(columns=['ID', 'purchase']), test_data['purchase']
pred = rfc.predict(x_test)
print(classification_report(y_test, pred))
print(confusion_matrix(y_test, pred))
def promotion_strategy(df):
    '''
    INPUT 
    df - a dataframe with *only* the columns V1 - V7 (same as train_data)

    OUTPUT
    promotion_df - np.array with the values
                   'Yes' or 'No' related to whether or not an 
                   individual should recieve a promotion 
                   should be the length of df.shape[0]
                
    Ex:
    INPUT: df
    
    V1	V2	  V3	V4	V5	V6	V7
    2	30	-1.1	1	1	3	2
    3	32	-0.6	2	3	2	2
    2	30	0.13	1	1	4	2
    
    OUTPUT: promotion
    
    array(['Yes', 'Yes', 'No'])
    indicating the first two users would recieve the promotion and 
    the last should not.
    '''
    
    
    
    
    return promotion
# This will test your results, and provide you back some information 
# on your how well your promotion_strategy will work in practice

test_results(promotion_strategy)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07477265072414954, 'fraction_numerical': 0.016840687100033683, 'mean_word_length': 4.037319762510602, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 1, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '397768', 'license': 'MIT', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1683, 'n_words': 748, 'path': 'portfolio_exercises/Experiments/Starbucks.ipynb', 'repo_name': 'thomasdunlap/DSND_Term2', 'size': '5938'}