Validate myReduce Function¶
Let us validate myReduce
Function.
- Compute order revenue for a given order id using order_items.
- We will use
myFilter
to filter for the order items for the given order id. - Use
myMap
to extractorder_item_subtotal
. We will also convert data type oforder_item_subtotal
tofloat
. - We can now compute
order_revenue
usingmyReduce
Function.
In [1]:
%run 02_develop_myFilter_function.ipynb
In [2]:
%run 04_develop_myMap_function.ipynb
In [3]:
%run 06_develop_myReduce_function.ipynb
In [4]:
myFilter
Out[4]:
<function __main__.myFilter(c, f)>
In [5]:
myMap
Out[5]:
<function __main__.myMap(c, f)>
In [6]:
myReduce
Out[6]:
<function __main__.myReduce(c, f)>
In [7]:
order_items_path = "/data/retail_db/order_items/part-00000"
order_items = open(order_items_path). \
read(). \
splitlines()
In [8]:
order_items[:10]
Out[8]:
['1,1,957,1,299.98,299.98', '2,2,1073,1,199.99,199.99', '3,2,502,5,250.0,50.0', '4,2,403,1,129.99,129.99', '5,4,897,2,49.98,24.99', '6,4,365,5,299.95,59.99', '7,4,502,3,150.0,50.0', '8,4,1014,4,199.92,49.98', '9,5,957,1,299.98,299.98', '10,5,365,5,299.95,59.99']
In [9]:
order_item = '2,2,1073,1,199.99,199.99'
int(order_item.split(',')[1]) == 2
Out[9]:
True
In [10]:
order_items_filtered = myFilter(
order_items,
lambda order_item: int(order_item.split(',')[1]) == 2
)
In [11]:
order_items_filtered
Out[11]:
['2,2,1073,1,199.99,199.99', '3,2,502,5,250.0,50.0', '4,2,403,1,129.99,129.99']
In [12]:
order_item = '2,2,1073,1,199.99,199.99'
float(order_item.split(',')[4])
Out[12]:
199.99
In [13]:
order_item_subtotals = myMap(
order_items_filtered,
lambda order_item: float(order_item.split(',')[4])
)
In [14]:
order_item_subtotals
Out[14]:
[199.99, 250.0, 129.99]
In [15]:
sum(order_item_subtotals)
Out[15]:
579.98
In [16]:
myReduce(order_item_subtotals, lambda t, e: t + e)
Out[16]:
579.98
In [17]:
myReduce(order_item_subtotals, lambda t, e: min(t, e))
Out[17]:
129.99