使用关联规则进行数据挖掘的Python实现

Python 专栏收录该内容
¥59.90 ¥99.00
211篇文章

关联规则是数据挖掘中常用的技术之一,它可以帮助我们发现数据集中的频繁项集以及它们之间的关联关系。在Python中,我们可以使用一些库来实现关联规则挖掘,其中最常用的是mlxtend库。本文将介绍如何在Python中使用关联规则进行数据挖掘,并提供相应的源代码。

安装mlxtend库

首先,我们需要安装mlxtend库。可以通过以下命令使用pip进行安装:

pip install mlxtend

安装完成后,我们就可以开始使用该库进行关联规则挖掘了。

导入所需库和数据集

在使用mlxtend库之前,我们需要导入一些必要的库和数据集。在本例中,我们将使用一个虚拟的购物篮数据集。

import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
fro
# 机器学习数据挖掘关联规则挖掘Apriori算法python代码实现 机器学习之关联规则挖掘Apriori算法 阅读详情

相关推荐

如何在Python中使用关联规则进行数据挖掘?

在Python中,我们可以使用关联规则算法和评估指标来查找规则,评估规则,并根据结果进行分析和预测。关联规则是其中一种常见的数据挖掘技术,它旨在发现不同数据点之间的关联关系,以便更好地理解数据集。在这个例子中,我们使用了一个min_support参数,它指定支持度阈值,用于确定哪些项集是频繁的。其中,support(A and B)是同时出现A和B的交易数,support(A)是出现A的交易数。在这个例子中,我们使用了一个置信度阈值0.6,表示只有当规则的置信度高于0.6时才被视为有意义的规则。

wx_19970108018的博客 934

Python 实现关联规则挖掘

结果中,antecedents 和 consequents 分别表示关联规则的前件和后件,support 表示项集的支持度,confidence 表示关联规则的置信度,lift 表示关联规则的提升度。需要注意的是,在实际应用中,还需要结合业务背景进行规则的筛选和解释,以达到更好的效果。其中,metric 参数表示评价关联规则质量的指标,这里采用的是 lift(提升度),min_threshold 表示限制关联规则的最小阈值,这里设置为 1。得到频繁项集后,就可以生成关联规则了。

学习使你进步。 535

【Python数据挖掘课程】八.关联规则挖掘及Apriori实现购物推荐

这篇文章主要介绍三个知识点,也是我《数据挖掘与分析》课程讲课的内容。        1.关联规则挖掘概念及实现过程;        2.Apriori算法挖掘频繁项集;        3.Python实现关联规则挖掘及置信度、支持度计算。关联规则(Association Rules)是反映一个事物与其他事物之间的相互依存性和关联性,如果两个或多个事物之间存在一定的关联关系,那么,其中一个事物就能通过其他事物预测到。关联规则是数据挖掘的一个重要技术,用于从大量数据中挖掘出有价值的数据项之间的相关关系。

杨秀璋的专栏 3万+

数据挖掘实战:使用Python进行关联规则分析

数据挖掘是从大量、不完全、有噪声、模糊的实际数据中,提取隐藏在其中的、人们事先不知道的、有潜在使用价值的信息和知识的过程。它涉及到统计学、数据库技术和机器学习等多个领域的知识,旨在揭示数据背后深层次的模式和趋势。关联规则分析是数据挖掘中的一个重要分支,其目标是发现大量数据中项目间的有趣关系,这些关系通常表现为一个项目在某种条件下出现时,另一个项目也以一定的概率出现。关联规则广泛应用于零售市场篮子分析、生物信息学、网页点击流分析等领域。

weixin_33480380的博客 912

详解python实现FP-TREE进行关联规则挖掘

详解python实现FP-TREE进行关联规则挖掘 python3.2实现,可以生成每一步fp树的图片(需要安装PIL)

关联规则挖掘经典算法apriori标准代码实现

Apriori算法是一种挖掘关联规则的频繁项集算法,其核心思想是通过候选集生成和情节的向下封闭检测两个阶段来挖掘频繁项集。 该算法的基本思想是:首先找出所有的频集,这些项集出现的频繁性至少和预定义的最小支持度一样。然后由频集产生强关联规则,这些规则必须满足最小支持度和最小可信度。然后使用第1步找到的频集产生期望的规则,产生只包含集合的项的所有规则,其中每一条规则的右部只有一项,这里采用的是中规则的定义。一旦这些规则被生成,那么只有那些大于用户给定的最小可信度的规则才被留下来。为了生成所有频集,使用了递归的方法。 (1) L1 = find_frequent_1-itemsets(D); (2) for (k=2;Lk-1 ≠Φ ;k++) { (3) Ck = apriori_gen(Lk-1 ,min_sup); (4) for each transaction t ∈ D {//scan D for counts (5) Ct = subset(Ck,t);//get the subsets of t that are candidates (6) for each candidate c ∈ Ct (7) c.count++; (8) } (9) Lk ={c ∈ Ck|c.count≥min_sup} (10) } (11) return L= ∪ k Lk; 可能产生大量的候选集,以及可能需要重复扫描数据库,是Apriori算法的两大缺点。

Association-Rule-Mining-Python:关联规则挖掘的Python实现

#Python中的关联规则挖掘 文件描述: apriori.py:apriori算法的Python实现。 此代码读取用户指定的事务数据库文件,并根据用户指定的支持和置信度值生成频繁的项目集和关联规则。 DataSetx.txt:(x:1,2,3,4,5)五个不同的包含事务的数据集文件。 用法: 修改apriori.py的第14行,并指定要使用的数据集文件的名称。 从命令行运行程序:python apriori.py 出现提示时,以百分比形式提供支持和置信度值 生成频繁的项目集和关联规则

群晖NAS实战:从零部署Container Manager与Docker代理配置全攻略

本文详细介绍了在群晖NAS上从零部署Container Manager与配置Docker代理的完整流程。针对国内用户常见的镜像拉取问题,提供了图形化界面与SSH命令行两种解决方案,并分享了MySQL容器部署的实战案例。文章特别强调了网络配置技巧和日常维护方法,帮助用户高效管理NAS上的容器应用。

weixin_29254673的博客 1313

关于华为高斯数据库出现Invalid or unsupported by client SCRAM mechanisms定位解决的过程

最近公司有个新项目使用的是高斯数据库,因为我们的项目是直接从别的项目组拿来的,配置都是已经配好的,但是在放到正式环境上之后出现报错。起初我们以为是测试和正式的版本不一致,但是结果是版本一致的。最后这个问题搞了我们几个小时,都没法解决。

Curtisjia的博客 3188

手把手教你用fusb302实现PD快充:从驱动移植到实战调试(附源码)

本文详细解析了基于FUSB302芯片实现USB PD快充的完整开发流程。内容涵盖芯片选型、硬件设计要点、Linux驱动移植与内核集成,并重点剖析了PD协议交互中的关键调试技巧,如“死亡15ms”时序问题与中断模式选择。文章通过实战经验,帮助嵌入式开发者高效完成从驱动移植到系统集成的全链路开发,并附有源码参考。

sky77的博客 378

深入解析五大ADC芯片架构及其应用场景

本文深入解析了五大ADC芯片架构及其应用场景,包括逐次逼近型ADC、Σ-Δ型ADC、流水线型ADC、闪存型ADC和积分型ADC。通过详细的架构分析和实际应用案例,帮助工程师理解不同ADC芯片的特点和适用场景,提升电路设计效率。

weixin_28702105的博客 357

iOS安全攻防:从DarkSword漏洞利用链剖析到纵深防御实践

在移动安全领域,内存破坏漏洞(如堆溢出、释放后使用)是攻击者突破系统防线、实现远程代码执行和权限提升的核心技术原理。这些漏洞利用技术通过精心构造的攻击链,能够绕过沙箱、地址空间布局随机化等安全机制,最终威胁设备内核安全,其技术价值在于揭示了系统安全架构的潜在薄弱环节。在iOS应用开发与系统防护中,理解此类攻击链对于构建有效的纵深防御体系至关重要。应用场景涵盖从安全编码、网络通信加固到运行时应用程序自保护等多个层面。本文以DarkSword工具包为例,深入拆解其利用链,并分享如何通过内存安全实践、代码签名强化

weixin_30357231的博客 481

Windows Server 2019整机备份与异机还原实战:从虚拟机到物理机的完整迁移指南

本文详细介绍了Windows Server 2019整机备份与异机还原的实战操作,涵盖从虚拟机到物理机的完整迁移流程。通过环境准备、备份策略设计、具体操作步骤及迁移后优化等内容,帮助用户实现系统环境的无缝迁移,特别适用于服务器硬件升级、数据中心迁移等场景。

weixin_30555125的博客 135

大话西游2单机版V8一键架设教程:5分钟搞定GM权限和仙玉修改(附完整物品代码表)

本文详细讲解了大话西游2单机版V8的一键架设教程,包括5分钟快速配置GM权限、仙玉修改及物品代码使用技巧。通过分步指导和服务端配置详解,帮助玩家轻松掌握游戏管理功能,重温经典回合制体验。附完整物品代码表,助你快速获取稀有道具和神兽。

weixin_42557830的博客 1239

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: Python中strip函数的用法及示例代码
下一篇: Python中如何重命名文件
普通网友
博客等级 码龄3年 341粉丝 659原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值