医学数据分析实训 项目三 关联规则分析预备项目---购物车分析

文章目录

1 预备项目

关联规则分析实践---------购物车分析

python 复制代码
import warnings
import numpy as np
import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
warnings.filterwarnings("ignore", category=DeprecationWarning)
# 读入数据
df_Retails = pd.read_excel('input/Online Retail.xlsx')
df_Retails.head()
python 复制代码
# 数据理解
print(df_Retails.shape)

df_Retails.columns

df_Retails.describe()


python 复制代码
#查看国家一列的取值
df_Retails.Country.unique()
#各国家的购物数量
df_Retails["Country"].value_counts()
#查看InvoiceNo一列中是否有重复的值
df_Retails.duplicated(subset=["InvoiceNo"]).any()
#是否有缺失值
df_Retails.isna().sum()
df_Retails['InvoiceNo'].isnull().sum(axis = 0)
python 复制代码
python 复制代码
#空格处理
df_Retails['Description'] = df_Retails['Description'].str.strip()
# Description: Product (item) name. Nominal.
#查看是否有缺失值
df_Retails['Description'].isna().sum()
#缺失值处理
df_Retails.dropna(axis=0
                  , subset=['Description']
                  , inplace=True)
print(df_Retails.shape)
#查看是否有缺失值
print(df_Retails['Description'].isna().sum())
#删除含有C字母的已取消订单
df_Retails['InvoiceNo'] = df_Retails['InvoiceNo'].astype('str')

df_Retails = df_Retails[~df_Retails['InvoiceNo'].str.contains('C')]
df_Retails.shape
python 复制代码
#将数据改为每一行一条购物记录
#考虑到内存限制只计算Germany,全部计算则计算量太大
df_ShoppingCarts = (df_Retails[df_Retails['Country'] =="Germany"]
                    .groupby(['InvoiceNo', 'Description'])['Quantity']
                    .sum()
                    .unstack()
                    .reset_index()
                    .fillna(0)
                    .set_index('InvoiceNo'))

print(df_ShoppingCarts.shape)

df_ShoppingCarts.head()
python 复制代码
#查看InvoiceNo一列中是否有重复的值
df_Retails.duplicated(subset=["InvoiceNo"]).any()

def encode_units(x):
    if x <= 0:
        return 0
    if x >= 1:
        return 1

df_ShoppingCarts_sets = df_ShoppingCarts.map(encode_units)

df_ShoppingCarts_sets.head()

1 产生频繁集

python 复制代码
# 产生频繁集 最小支持度为0.07, 在输出中使用原始列名
df_Frequent_Itemsets = apriori(df_ShoppingCarts_sets
                               , min_support=0.07
                               , use_colnames=True)
df_Frequent_Itemsets

2 产生关联规则

python 复制代码
# 生成关联规则,使用提升度(lift)作为度量 置提升度的最小阈值为 1,表示无正相关关系的规则也会被计算
df_AssociationRules = association_rules(df_Frequent_Itemsets
                                        , metric="lift"
                                        , min_threshold=1)
#输出结果的解读:https://rasbt.github.io/mlxtend/user_guide/frequent_patterns/association_rules/
df_AssociationRules.head()
python 复制代码
# 筛选关联规则 筛选提升度不小于 2 置信度不小于 0.8 的关联规则
df_A= df_AssociationRules[(df_AssociationRules['lift'] >= 2) &
                          (df_AssociationRules['confidence'] >= 0.8) ]
df_A
python 复制代码
# 可视化结果
import seaborn as sns
import matplotlib.pyplot as plt

sns.scatterplot(x = "support"
                , y = "confidence"
                , size = "lift"
                , data = df_AssociationRules)
plt.show()
相关推荐
databook17 小时前
数据分析师的“水晶球”:时间序列分析
python·数据挖掘·数据分析
玄同7651 天前
Python 流程控制:LLM 批量推理与 API 限流处理
服务器·人工智能·python·深度学习·自然语言处理·数据挖掘·知识图谱
计算机程序设计小李同学1 天前
基于贝叶斯分类算法的垃圾邮件筛选器开发
人工智能·分类·数据挖掘
ClkLog-开源埋点用户分析2 天前
【埋点分析系统】初次选型的实用指南(附开源解决方案)
数据分析·开源·开源软件·用户画像·埋点分析
天呐草莓2 天前
集成学习 (ensemble learning)
人工智能·python·深度学习·算法·机器学习·数据挖掘·集成学习
电商API_180079052472 天前
淘宝商品视频提取API全解析:从授权到落地实战
爬虫·python·信息可视化·数据分析·音视频
没有梦想的咸鱼185-1037-16632 天前
面向自然科学的人工智能建模方法【涵盖机器学习与深度学习的核心方法(如随机森林、XGBoost、CNN、LSTM、Transformer等)】
人工智能·深度学习·随机森林·机器学习·数据分析·卷积神经网络·transformer
十三画者2 天前
【文献分享】PepQueryMHC:基于免疫肽组学数据实现肿瘤抗原的快速全面筛选
数据挖掘·数据分析
DX_水位流量监测2 天前
地埋式积水监测仪:城市防涝的智能感知核心
大数据·网络·人工智能·数据分析·自动化
TM1Club2 天前
Zoey的TM1聊天室|#3 合并报表提速:业财一体如何实现关联方对账自动化
大数据·开发语言·人工智能·经验分享·数据分析·自动化·数据库系统