Introduction to Predictive Analytics in Python
Nele Verbiest, Ph.D
Data Scientist @PythonPredictions
disc_mean_gift | Incidence | Size |
---|---|---|
[2, 78] | 0.013042 | 20013 |
(78, 87] | 0.029554 | 19997 |
(87, 94] | 0.040831 | 20034 |
(94, 103] | 0.063563 | 20405 |
(103, 197] | 0.103524 | 19551 |
# Load the numpy module import numpy as np # Function that calculates the predictor insight graph table def create_pig_table(df, target, variable):
# Group by the variable you want to plot groups = df[[target,variable]].groupby(variable)
# Calculate the size and incidence of each group pig_table = groups[target].agg(Incidence = 'mean').reset_index() return pig_table
print(create_pig_table(basetable,"target","country")
country Incidence Size
India 0.050934 49849
UK 0.050512 10057
USA 0.048486 40094
variables = ["country", "gender", "disc_mean_gift", "age"]
# Empty dictionary. pig_tables = {}
# Loop over all variables for variable in variables:
# Create the predictor insight graph table pig_table = create_pig_table(basetable, "target", variable)
# Store the table in the dictionary pig_tables[variable] = pig_table
print(create_pig_table(basetable,"target","country")
country Incidence Size
India 0.050934 49849
UK 0.050512 10057
USA 0.048486 40094
Introduction to Predictive Analytics in Python