6.2 关联规则可视化

## 加载包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy as sp
## 图像在jupyter notebook中显示
%matplotlib inline
## 显示的图片格式(mac中的高清格式),还可以设置为"bmp"等格式
%config InlineBackend.figure_format = "retina"
## 输出图显示中文
from matplotlib.font_manager import FontProperties
fonts = FontProperties(fname = "D:\Desktop\python在机器学习中的应用\方正粗黑宋简体.ttf",size=14)
## 引入3D坐标系
from mpl_toolkits.mplot3d import Axes3D
## cm模块提供大量的colormap函数
from matplotlib import cm
import matplotlib as mpl
## 挖掘频繁项集和关联规则
from mlxtend.frequent_patterns import apriori,association_rules  
from mlxtend.preprocessing import TransactionEncoder
## 读取数据
datadf = pd.read_excel("D:\Desktop\python在机器学习中的应用\调查问卷2.xls")
## 对数据集进行编码
datanew = np.array(datadf.iloc[:,1::])
oht = TransactionEncoder() # 相应类别若含有实例则为true,否则为false
oht_ary = oht.fit(datanew).transform(datanew)
## 将编码后的数据集做成数据表,每列为各个选项
df = pd.DataFrame(oht_ary, columns=oht.columns_)
## 发现频繁项集,最小支持度为0.3
df_fre = apriori(df, min_support=0.3,use_colnames=True)
## 找到关联规则,通过置信度阈值发现规则
rule2 = association_rules(df_fre, metric="confidence", min_threshold=0.7)
rule2["antelen"] = rule2.antecedents.apply(lambda x:len(x))
rule2 = rule2[(rule2.antelen == 1) & (rule2.lift > 1)]
rule2.head()

在这里插入图片描述
*rule2 = association_rules(df_fre, metric=“confidence”, min_threshold=0.7)*寻找置信度大于0.7的规则,
rule2 = rule2[(rule2.antelen == 1) & (rule2.lift > 1)]
只提取前项的事件数量大于1的规则得到新的规则 rule2

## 绘制支持度和置信度的散点图
rule2.plot(kind="scatter",x = "support",c = "r",
           y = "confidence",s = 30,figsize=(8,5))
plt.grid("on")
plt.xlabel("支持度",FontProperties = fonts,size = 12)
plt.ylabel("置信度",FontProperties = fonts,size = 12)
plt.title("38个规则散点图",FontProperties = fonts)
plt.show()

在这里插入图片描述

## 将部分关联规则使用关系网络图进行可视化
import networkx as nx
plt.figure(figsize=(12,12))
## 生成社交网络图
G=nx.DiGraph()

## 为图像添加边
for ii in rule2.index:
    G.add_edge(rule2.antecedents[ii],rule2.consequents[ii],weight = rule2.support[ii])
    
## 定义2种边
elarge=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] >0.6]
emidle=[(u,v) for (u,v,d) in G.edges(data=True) if (d['weight'] <= 0.6)&(d['weight'] >= 0.45)]
esmall=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] <= 0.45]

## 图的布局方式
pos=nx.circular_layout(G)

# 根据规则的置信度节点的大小
nx.draw_networkx_nodes(G,pos,alpha=0.4,node_size=rule2.confidence * 500)

# 设置边的形式
nx.draw_networkx_edges(G,pos,edgelist=elarge,
                    width=2,alpha=0.6,edge_color='r')
nx.draw_networkx_edges(G,pos,edgelist=emidle,
                    width=2,alpha=0.6,edge_color='g',style='dashdot')
nx.draw_networkx_edges(G,pos,edgelist=esmall,
                    width=2,alpha=0.6,edge_color='b',style='dashed')

# 为节点添加标签
nx.draw_networkx_labels(G,pos,font_size=10,font_family="STHeiti")

plt.axis('off')
plt.title("38个规则使用支持度连接",FontProperties = fonts)
plt.show() 

在这里插入图片描述

  • 5
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
进行数据可视化分析的过程可以分为以下几个步骤: 1. 数据清洗和处理:对于爬取到的数据,可能存在一些缺失值、异常值或者需要进行转换的数据类型等问题。因此我们需要对数据进行清洗和处理,使其变得更加规范化和易于分析。 2. 数据可视化:选择合适的图表类型,将数据可视化成图表,如折线图、柱状图、散点图等。数据可视化一般需要使用相关的可视化库,如 Matplotlib、Seaborn、Plotly 等。 3. 数据分析:通过对可视化图表的观察和分析,可以得出一些结论和发现,如数据的分布规律、趋势、关联性等。同时,根据分析的结果,可以提出一些问题和假设,进一步深入挖掘数据中的信息。 下面是一个示例代码,可以使用 Matplotlib 库将爬取到的豆瓣读书评分和评论人数进行可视化分析: ```python import matplotlib.pyplot as plt # 数据 ratings = [8.0, 7.7, 8.2, 6.2, 8.5] comments = [322, 210, 312, 98, 456] # 可视化 plt.scatter(comments, ratings) plt.xlabel("评论人数") plt.ylabel("评分") plt.title("豆瓣读书评分和评论人数分布") plt.show() ``` 上述代码中,我们将爬取到的豆瓣读书评分和评论人数分别保存在 ratings 和 comments 两个列表中,然后使用 Matplotlib 库中的 scatter 函数将数据可视化成散点图。其中,评论人数作为 x 轴,评分作为 y 轴,同时添加了 x 轴和 y 轴的标签,以及图表的标题。最后使用 show 函数展示图表。 需要注意的是,这只是一个简单的例子,实际进行数据可视化分析时,需要根据具体的问题和数据特征选择合适的图表类型和分析方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大桃子技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值