開始使用免費開始

定義彙總函式

零售商注意到庫存中告示牌類商品的占比偏高,決定對不同類別做進一步彙總,以更完整地探索資料。對你來說這很基本,但該零售商先前甚至連最基本的交易與品項描述性分析都做不到。

他們請你對 candlesbagsboxes 三個類別進行彙總。為了簡化流程,你決定寫一個函式。這個函式會接收一個代表品項類別的字串,然後輸出一個 DataFrame,標示每筆交易是否包含該類別的品項。注意,pandas 已以 pd 匯入,且資料已以 one-hot 編碼格式載入為 onehot

本練習屬於課程

Python 的 Market Basket Analysis

檢視課程

練習說明

  • 完成列表生成式,擷取欄名的子集合。
  • 選取你想要彙總之品項對應的欄位。
  • 使用 aggregate() 函式,分別以字串 bagboxcandle 對 bags、boxes 與 candles 進行彙總。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

def aggregate(item):
	# Select the column headers for sign items in onehot
	item_headers = [i for i in ____.columns if i.lower().find(item)>=0]

	# Select columns of sign items
	item_columns = onehot[____]

	# Return category of aggregated items
	return item_columns.sum(axis = 1) >= 1.0

# Aggregate items for the bags, boxes, and candles categories  
bags = aggregate('bag')
boxes = aggregate('____')
candles = ____
編輯並執行程式碼