数据挖掘基础知识(未完待续)

引言

首先要知道,数据挖掘都包括什么内容,对应是干什么的

以下为课程笔记:

why

    原因:数据爆炸
    解决办法
        数据仓库(Data warehouse)和在线分析处理(OLAP)
        数据挖掘

what

    定义
        数据挖掘:在大量数据中挖掘感兴趣的知识(规则、规律、模式、约束)
        数据库中的知识挖掘(KDD)


            
    知识挖掘的步骤:
        a)    了解应用领域——了解相关的知识和应用的目标
        b)    创建目标数据集: 选择数据
        c)    数据清理和预处理: (这个可能要占全过程60%的工作量)
        d)    数据缩减和变换
        e)    选择数据挖掘的功能
        f)    选择挖掘算法
        g)    数据挖掘: 寻找感兴趣的模式
        h)    模式评估和知识表示
        i)    运用发现的知识
    数据挖掘和商业智能
        

where

    5.    数据
        a)    结构化数据:二维表格的形式存储在关系数据库中
        b)    非结构化数据:文本数据 § 视频数据 § 音频数据 § 图像数据
    6.    数据来源:
        a)    关系数据库(ER图,Oracle,SQLServer,DB2,Mysql)
        b)    数据仓库 § 事务数据库 § 高级数据库系统和信息库 ü 空间数据库 ü 时间数据库和时间序列数据库 ü 流数据 ü 多媒体数据库 ü 面向对象数据库和对象-关系数据库 ü 异种数据库和遗产(legacy)数据库 ü 文本数据库和万维网
        c)    各个数据库的简介(略)

how

    1.    分类(classification)
        a)    是通过对【有类别】的对象的数据集进行学习,概括其主要特征,构建分类模型,根据该模型预测 对象的类别的一种数据挖掘和机器学习技术。
        b)    例如,电信公司的客户可以分为两类,一类是忠诚的,一类 是流失的。根据这两类客户的个人特征方面的数据以及在公 司的消费方面的数据,利用分类技术可以构建分类模型。
        c)
    2.    聚类(clustering)
        a)    是依据物以类聚的原理,将【没有类别的】对象根据对象的特征自动聚集成不同簇的过程,使得属于同 一个簇的对象之间非常相似,属于不同簇的对象之间不相似。 § 其典型应用是客户分群,根据客户特征把客聚成不同的客户群。
    3.    关联分析
        a)    关联分析最早用于分析超市中顾客一次购买的物品之间的关 联性。
        b)    例如,发现关联规则(association rule)“尿不湿®啤酒 (0.5%,60%)”,其含义为,0.5%的交易中会同时购买 尿不湿和啤酒,且买尿不湿的交易中有60%会同时买啤酒。
    4.    数值预测
        a)    数值预测用于预测连续变量的取值。 § 常用的预测方法是回归分析。
        b)    例如,可以根据客户个人特征,如年龄、工作类型、受 教育程度、婚姻状况等,来预测其每月的消费额度。
    5.    :异常点挖掘
        a)    § 孤立点分析(outlier analysis) § 一些与数据一般特点不一致的孤立点。
        b)    例如,信用卡客户欺诈检测。
    6.    序列分析
        a)    § 序列分析是对序列数据库进行分析,从中挖掘出有意义模式的 技术 § 序列模式(sequential pattern)的发现属于序列分析,它是 从序列数据库中发现的一种有序模式.
    7.    社会网络(social network)
        a)    是由个人或组织及其之间的 关系构成的网络。
        b)    社会网络分析(social network analysis)是对社会网 络的结构和属性进行分析,以发现其中的局部或全局特 点,发现其中有影响力的个人或组织,发现网络的动态 变化规律等。

apply

    1.    数据分析和决策支持 –
        a)    市场分析和管理 Ø 目标市场, 客户关系管理 (CRM),  市场占有量分析, 交叉销售, 市场分割 - 风险分析和管理
        b)    风险预测, 客户保持, 保险业的改良, 质量控制, 竞争分析 - 欺骗检测和异常模式的监测 (孤立点)
    2.    其他的应用 - 文本挖掘 (新闻组,电子邮件, 文档) 和WEB挖掘 - 流数据挖掘 - DNA 和生物数据分析
 

关联规则挖掘

关联规则挖掘是从交易数据库、关系数据库以及其他的数据集中发现项 或对象的频繁模式(frequent patterns)、关联( associations)的 过程。 

已有:一段时间内顾客购买的所有小票清单,为了方便我们把A:炸鸡,D:可乐

  • 假如有一条关联规则:炸鸡->可乐,——也可看为概率事件
    • 买炸鸡的占3/5,买可乐的占4/5, ——P(A)=3/5, P(D)=4/5
    • 支持度support:同时买炸鸡可乐的占3/5, ——P(AD)=3/5
    • 置信度confidence:买炸鸡的人中买可乐的占3/3,——P(AD)/P(A)

 

频繁模式与关联规则

  • 频繁项集:没有先后或因果(经常一起买的商品,比如i面包+牛奶,炸鸡+可乐)
  • 关联规则:涉及时间和顺序(买此商品也同时购买,比如?)
    • (item, transaction)  (商品,交易) (词,段落/句子)
  • 交易数据库Transactional database
    • - 每个交易由顾客一次购买的商品items)组成
    • - 全部商品的集合,即所有项的集合I={i1, i2, …, im}
    • - 项集(Itemset): x={ij1,ij2,…,ijp}, iji属于I
    • - 每个项集包含的项的个数,称为项集的长度,一个长度为k的项集又称为k项集

 

支持度

【support(X),即在所有交易中,X出现的概率】

支持度指交易包含项集X的概率其中D的模表示交易的个数

 

  • 若support(X) >=最小支持度阈值minsup ,则X称为频繁项集 (frequent itemset),也可以说X是频繁的.
    • 设minsup = 50%,那么频繁项集为:{A:3, B:3, D:4, E:3, AD:3}
      • 其中A表示项集(itemset),3表示出现的次数,即count(A)
      • 该项集时频繁的,其自己也是频繁的,例如{A} {A, B} {A, AD}等

      • 如果一个项集时不频繁的,那么他的超集都是不频繁的

  • 一个频繁项集 X 被称为闭合频繁项集(closed frequent itemset)当 且仅当不存在任一个项集Y满足XÌY 且support(Y)=support(X)。闭 合频繁项集X被称为是闭合的。
    • 例如: - A是频繁的,但不是闭合的, 因为support(AD)=support(A),且 
    • 然而{AD}时闭合的,因为不存在一个与他具有相同支持度的超集
       

置信度

【support(X→Y)即在买X的情况下又买了Y的概率】

  • 关联规则
    • 给定两个项集X 和Y,关联规则是形如X→Y 的蕴含式 
      • 规则X→Y的支持度(support) support(X→Y)=support(X∪Y)
      • 规则X→Y的置信度(confidence)

提升度

【提升度是一种相关性度量,可用于评估一个项集的出现使得 另一个项集也出现的程度】
除了支持度和置信度外,还有 一个评判关联规则的重要标准是提升度。项集 A和项集B出现两者之间的提升度定义为

 
  • 提升度小于 1 表示 A 的出现和 B 的出现是负相关的,其中一个的出现 可导致另外一个不出现;
  • 提升度等于 1 表示 A B 是独立的,彼此之间无相关性;
  • 提升度大于 1表示 A B是正相关的,其中一个出现蕴涵另一个的出现。

(根据挖掘出数据的情况看,一般可以取3,如果想要多一点的数据也可以取2)

关联规则的挖掘

  •  给定如下阈值
    • - minimum support : minsup
    • - Minimum confidence :minconf
  • 发现所有形如X -> Y 的关联规则,满足
    • - Support(X -> Y)≥ minsup
    • - Confidence(X -> Y)≥ minconf
    • - 若上述两点同时满足,则称该规则在数据库中成立

频繁项集的典型挖掘算法

关联规则的挖掘步骤:发现所有的频繁项集,从频繁项集中发现关联方法

逐层发现算法Apriori 

  • 主要步骤(简单来说,先发现1项集,再发现2项集,,,发现n项集合)
    • 1. k=1
    • 2. 统计每个k项候选集的支持度,找出频繁的k项集(support(x)>minsip):Lk
    • 3. 利用频繁的k项集生成k+1项候选集(Candidate itemset ):Ck+1
      • 由于【如果一个项集时不频繁的,那么他的超集都是不频繁的】,可以判断其候选集是不是频繁的
    • 4. k=k+1; 转至步骤2
  • 例子:

代码示例:

方法一:调用库函数

安装命令(一)

【无法使用】

pip install apriori

import apriori
help(apriori)
# 我也不太清楚为啥是这个样子!生气!
'''
Traceback (most recent call last):

  File "E:\ProgramFiles\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-80-3c93fe04cb50>", line 1, in <module>
    import apriori

  File "E:\ProgramFiles\Anaconda3\lib\site-packages\apriori.py", line 79
    print freqSet-conseq,'-->',conseq,'conf:',conf
                ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(freqSet-conseq,'-->',conseq,'conf:',conf)?
'''

安装命(二)

【常用参数为:min_support, min_confidence, max_length】

其中无min_lift(提升度)的设置,但是输出数据简单明细,适合用于课程训练

pip install efficient-apriori

from efficient_apriori import apriori
help(apriori)

'''
Help on function apriori in module efficient_apriori.apriori:

apriori(transactions: Union[List[tuple], Callable], min_support: float = 0.5, min_confidence: float = 0.5, max_length: int = 8, verbosity: int = 0, output_transaction_ids: bool = False)
    The classic apriori algorithm as described in 1994 by Agrawal et al.
    
    The Apriori algorithm works in two phases. Phase 1 iterates over the 
    transactions several times to build up itemsets of the desired support
    level. Phase 2 builds association rules of the desired confidence given the
    itemsets found in Phase 1. Both of these phases may be correctly
    implemented by exhausting the search space, i.e. generating every possible
    itemset and checking it's support. The Apriori prunes the search space
    efficiently by deciding apriori if an itemset possibly has the desired
    support, before iterating over the entire dataset and checking.
    
    Parameters
    ----------
    transactions : list of tuples, list of itemsets.TransactionWithId,
        or a callable returning a generator. Use TransactionWithId's when
        the transactions have ids which should appear in the outputs.
        The transactions may be either a list of tuples, where the tuples must
        contain hashable items. Alternatively, a callable returning a generator
        may be passed. A generator is not sufficient, since the algorithm will
        exhaust it, and it needs to iterate over it several times. Therefore,
        a callable returning a generator must be passed.
    min_support : float
        The minimum support of the rules returned. The support is frequency of
        which the items in the rule appear together in the data set.
    min_confidence : float
        The minimum confidence of the rules returned. Given a rule X -> Y, the
        confidence is the probability of Y, given X, i.e. P(Y|X) = conf(X -> Y)
    max_length : int
        The maximum length of the itemsets and the rules.
    verbosity : int
        The level of detail printing when the algorithm runs. Either 0, 1 or 2.
    output_transaction_ids : bool
        If set to true, the output contains the ids of transactions that
        contain a frequent itemset. The ids are the enumeration of the
        transactions in the sequence they appear.
    Examples
    --------
    >>> transactions = [('a', 'b', 'c'), ('a', 'b', 'd'), ('f', 'b', 'g')]
    >>> itemsets, rules = apriori(transactions, min_confidence=1)
    >>> rules
    [{a} -> {b}]
'''

 

测试代码

from efficient_apriori import apriori
transactions = [('A', 'C', 'D'),
 ('B', 'C', 'E'),
 ('A', 'B', 'C', 'E'),
 ('B', 'E')]
itemsets, rules = apriori(transactions, min_support=0.5, min_confidence=1)

 

安装命令(三)

【相比(二)多了个min_lift等,适用于项目】

pip install apyori

from apyori import apriori
help(apriori)
'''
Help on function apriori in module apyori:

apriori(transactions, **kwargs)
    Executes Apriori algorithm and returns a RelationRecord generator.
    
    Arguments:
        transactions -- A transaction iterable object
                        (eg. [['A', 'B'], ['B', 'C']]).
    
    Keyword arguments:
        min_support -- The minimum support of relations (float).
        min_confidence -- The minimum confidence of relations (float).
        min_lift -- The minimum lift of relations (float).
        max_length -- The maximum length of the relation (integer).
'''

但是输出就比较丑了

#只有一个返回值
results = apriori(gradeF, min_support=0.06, min_confidence=0.6, min_lift=3)
#print(rules) #<generator object apriori at 0x0000021839200548>
for result in results:
    print(result)

  • frozenset:项集
  • support:支持度
  • Ordered Statistic:关联关系
    • item_base => item_add
    • confidence:置信度
    • lift:提升度

 

可以自己写个代码美化一下,看着就顺眼多了

results = apriori(gradeF, min_support=0.06, min_confidence=0.6, min_lift=3)
for item, support,OrderedStatistic in sorted(results,key=lambda x:x[1],reverse=True):
    print("【项集】",item)
    print("【支持度】",support)
    print("【关联规则】")
    for a,b,con,lift in OrderedStatistic:
        #其中a,b的结构为:frozenset({'C/C++语言程序设计实验(2)'})
        a=str(a)
        b=str(b)
        print('*',a[12:-3],'==>',b[12:-3],'con:',con,'lift:',lift)
    print('____________________________')

也可以保存成csv,方便粘贴到论文里

……

方法二:自己写代码

……

无候选集发现算法FP-growth

关联规则的生成方法

关联规则的其他类型

多层次关联规则

- 什么品牌的啤酒和尿片有关联?

负关联规则、无关规则(dissociation rule)

play basketball -> not eat cereal [20%, 33.3%]

结构化数据中的关联分析

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值