Analisis Market Basket dengan Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School


| TID | Transaksi |
|---|---|
| 1 | biografi, sejarah |
| 2 | fiksi |
| 3 | biografi, puisi |
| 4 | fiksi, sejarah |
| 5 | biografi |
| ... | ... |
| 75000 | fiksi, puisi |
Identifikasi produk yang sering dibeli bersama.
Buat rekomendasi berdasarkan temuan ini.
| TID | Transaksi |
|---|---|
| 11 | fiksi, biografi |
| 12 | fiksi, biografi |
| 13 | sejarah, biografi |
| ... | ... |
| 19 | fiksi, biografi |
| 20 | fiksi, biografi |
| ... | ... |
import pandas as pd
# Load transactions from pandas.
books = pd.read_csv("datasets/bookstore.csv")
# Print the header
print(books.head(2))
TID Transaction
0 biography, history
1 fiction
Sebagai pengingat, lihat Pandas Cheat Sheet.
# Split transaction strings into lists.
transactions = books['Transaction'].apply(lambda t: t.split(','))
# Convert DataFrame into list of strings.
transactions = list(transactions)
# Print the first transaction.
print(transactions[0])
['biography', 'history']
# Count the number of transactions that contain biography and fiction.
transactions.count(['biography', 'fiction'])
218
# Count the number of transactions that contain fiction and poetry.
transactions.count(['fiction', 'poetry'])
5357

Analisis Market Basket dengan Python