Я пытался провести анализ рыночной корзины, чтобы определить, какие виды услуг можно объединить.
Набор данных состоит из 142155 наблюдений и содержит следующие столбцы:
-ID транзакции
-Geohash_user: замаскированный идентификатор пользователя.
-Работа создана в: Когда клиент отправил запрос на работу (Отметка времени).
- Задание запрошено в: Когда клиент нуждается в услуге (Дата).
-Ид типа услуги
-Service Type
-Keywords: с каждым типом сервиса связаны уникальные ключевые слова.
-Category: Категория сервиса.
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
import mlxtend as ml
# import dataset
df = pd.read_csv('Transaction.csv')
df.head()
# clean data
df.dropna(axis=0, subset=['transaction_id'], inplace=True)
# consolidate service types into one transaction per row with service type 1 hot encoded.
new_df = df.groupby(['transaction_id', 'service_type']).size().reset_index(name='count')
basket = (new_df.groupby(['transaction_id', 'service_type'])['count'].sum().unstack().reset_index().fillna(0).set_index('transaction_id'))
basket.head()
# The basket data is filled with zeros, but we need to ensure that any positive
# values are converted to a 1, and anything less than or equal to 0 is set to 0.
# This step completes the one hot encoding of the data.
# the encoding function
def encode_units(x):
if x <= 0:
return 0
if x >= 1:
return 1
basket_sets = basket.applymap(encode_units)
frequent_servicesets = apriori(basket_sets, min_support=0.01, use_colnames=True)
rules = association_rules(frequent_servicesets, metric='lift')
rules.sort_values('confidence', ascending=False, inplace=True)
rules.head(10)
'''
Upon running the program, I end up with an empty table as follows:
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/pfXK1.png
May I know why is it so, and how can I solve it?
Thank you.
Best regards,
Hui San