Market Segmentation

Overview

When doing analytics with market segments, it is often necessary to take a dataset and to group the dataset into the various segments, and then to do various calculations on the results.

In this example, we utilize the collections library to group the data.

Sample Data

The market segmentation script includes a script for generating sample data whcih is used in the analysis.

import sample as py customers, purchases = py.sample()

The following is a json dump of the first 5 items in the generated purchases data:

[{"customer_id": "416afa4d", "item": {"name": "item1", "price": 100}, "number": 2}, {"customer_id": "416afa4d", "item": {"name": "item5", "price": 76}, "number": 1}, {"customer_id": "416afa4d", "item": {"name": "item3", "price": 20}, "number": 4}, {"customer_id": "416afa4d", "item": {"name": "item1", "price": 100}, "number": 3}, {"customer_id": "416afa4d", "item": {"name": "item4", "price": 500}, "number": 4},

Each record has a customer id, indicating the customer who purchased the item, an item representing a product purchased by the customer, and a number of items purchased.

Grouping the Data

For this example, we use the collections library to group all the records and place the results in a simple dictionary.

from lib.collections import group purchase_group = group(purchases, lambda x:x['item']['name'])

Calculating Sum

The group function in the collections library can take a function that can aggregate the results for each group. Here, we provide a function that can sum the results.

def sum(agg, item): if agg == None: agg = 0 agg = agg + item['number']*item['item']['price'] return agg purchase_group = group(purchases, lambda x:x['item']['name'], sum)

Sample Script

import lib.sample as py from lib.collections import group customers, purchases = py.sample() def sum(agg, item): if agg == None: agg = 0 agg = agg + item['number']*item['item']['price'] return agg purchase_group = group(purchases, lambda x:x['item']['name'], sum) def average(agg, item): if agg == None: agg = {'sum':0.0, 'count':0.0} agg['sum'] += item['number']*item['item']['price'] agg['count'] += 1.0 agg['average'] = agg['sum']/agg['count'] return agg purchase_group2 = group(purchases, lambda x:x['item']['name'], average) records = group.to_list(['name'], purchase_group2) print(records)

Try It!