Book Image

Data Science for Marketing Analytics

By : Tommy Blanchard, Debasish Behera, Pranshu Bhatnagar
Book Image

Data Science for Marketing Analytics

By: Tommy Blanchard, Debasish Behera, Pranshu Bhatnagar

Overview of this book

Data Science for Marketing Analytics covers every stage of data analytics, from working with a raw dataset to segmenting a population and modeling different parts of the population based on the segments. The book starts by teaching you how to use Python libraries, such as pandas and Matplotlib, to read data from Python, manipulate it, and create plots, using both categorical and continuous variables. Then, you'll learn how to segment a population into groups and use different clustering techniques to evaluate customer segmentation. As you make your way through the chapters, you'll explore ways to evaluate and select the best segmentation approach, and go on to create a linear regression model on customer value data to predict lifetime value. In the concluding chapters, you'll gain an understanding of regression techniques and tools for evaluating regression models, and explore ways to predict customer choice using classification algorithms. Finally, you'll apply these techniques to create a churn model for modeling customer product choices. By the end of this book, you will be able to build your own marketing reporting and interactive dashboard solutions.
Table of Contents (12 chapters)
Data Science for Marketing Analytics
Preface

Chapter 5: Predicting Customer Revenue Using Linear Regression


Activity 8: Examining Relationships between Storefront Locations and Features about their Area

  1. Load the data from location_rev.csv and then take a look at it:

    import pandas as pd
    
    df = pd.read_csv('location_rev.csv')
    df.head()
  2. Use seaborn's pairplot function to visualize the data and its relationships:

    import seaborn as sns
    %matplotlib inline
    
    sns.pairplot(df)
  3. Use correlations to investigate the relationship between the different variables and the location revenue:

    df.corr()

The resulting correlations should make sense. The more competitors in the area, the lower the revenue of a location, while the median income, loyalty members, and population density are all positively related. A location's age is also positively correlated with revenue, indicating that the longer a location is open, the better known it is and the more customers it attracts (or perhaps, only locations that do well last a long time).

Activity 9: Building a Regression Model to Predict Storefront Location Revenue

  1. Import the data from location_rev.csv and view the first few rows:

    import pandas as pd
    df = pd.read_csv('location_rev.csv')
    df.head()
  2. Create a variable, X, with the predictors in it, and store the outcome (revenue) in a separate variable, y:

    X = df[['num_competitors',
           'median_income',
           'num_loyalty_members',
           'population_density',
           'location_age'
           ]]
    y = df['revenue']
  3. Split the data into training and test sets. Use random_state = 100:

    from sklearn.model_selection import train_test_split
    
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 100)
  4. Create a linear regression model and fit it on the training data:

    from sklearn.linear_model import LinearRegression
    
    model = LinearRegression()
    model.fit(X_train,y_train)
  5. Print out the model coefficients:

    model.coef_
  6. Print out the model intercept:

    model.intercept_
  7. Produce a prediction for a location that has 3 competitors; a median income of 30,000; 1,200 loyalty members; a population density of 2,000; and a location age of 10:

    single_location = pd.DataFrame({
        'num_competitors': [3],
        'median_income': [30000],
        'num_loyalty_members': [1200],
        'population_density': [2000],
        'location_age': [10]
    })
    
    model.predict(single_location)
  8. Plot the model predictions versus the true values on the test data:

    import matplotlib.pyplot as plt
    %matplotlib inline
    
    plt.scatter(model.predict(X_test),y_test)
    plt.xlabel('Model Predictions')
    plt.ylabel('True Value')
    plt.plot([0, 100000], [0, 100000], 'k-', color = 'r')
    plt.show()
  9. Calculate the correlation between the model predictions and the true values of the test data:

    from scipy.stats.stats import pearsonr
    
    pearsonr(model.predict(X_test),y_test)

The first number shows an extremely high correlation value (just over 0.9, where 1.0 would be a perfect correlation). The second number shows an extremely small p-value, indicating that it's very unlikely that this correlation is due to chance. Taken together, this indicates that our model is working very well on the test data.