開始使用免費開始

用函式計算 conviction

在她的試辦專案成功後,這家電子書新創的創辦人決定聘請你進行一個更大的專案。她詢問你是否能為 goodreads-10k 資料集中的每一對書籍計算 conviction,好讓她用這些資訊決定哪些書在網站上要擺得更靠近。

你答應接下這份工作,但也發現需要更有效率的方式來計算 conviction,因為你將需要重複計算很多次。你決定撰寫一個計算用的函式。它會接受 pandas DataFrame 的兩個欄位作為輸入,一個作為前件(antecedent),一個作為後件(consequent),並輸出 conviction 指標。注意,pandaspd 可用,numpynp 可用。

本練習屬於課程

Python 的 Market Basket Analysis

檢視課程

練習說明

  • 計算前件的支持度,並指定給 supportA
  • 計算「非後件」的支持度。
  • 計算「前件且非後件」的支持度。

動手互動練習

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

def conviction(antecedent, consequent):
	# Compute support for antecedent AND consequent
	supportAC = np.logical_and(antecedent, consequent).mean()

	# Compute support for antecedent
	supportA = ____.____()

	# Compute support for NOT consequent
	supportnC = 1.0 - ____.____()

	# Compute support for antecedent and NOT consequent
	supportAnC = ____ - supportAC

    # Return conviction
	return supportA * supportnC / supportAnC
編輯並執行程式碼