Market Basket Analysis v Pythonu
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School


| TID | Transakce |
|---|---|
| 1 | biografie, historie |
| 2 | beletrie |
| 3 | biografie, poezie |
| 4 | beletrie, historie |
| 5 | biografie |
| ... | ... |
| 75000 | beletrie, poezie |
Identifikujte produkty kupované společně.
Sestavte doporučení na základě těchto zjištění.
| TID | Transakce |
|---|---|
| 11 | beletrie, biografie |
| 12 | beletrie, biografie |
| 13 | historie, biografie |
| ... | ... |
| 19 | beletrie, biografie |
| 20 | beletrie, biografie |
| ... | ... |
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
Pro zopakování viz 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

Market Basket Analysis v Pythonu