金融统计分析与挖掘实战6.1-6.3

金融统计分析与挖掘实战6.1-6.3,第1张

# 6.1 关联规则
import numpy as np
import pandas as pd
import os
os.chdir("C:\Users\Administrator\Desktop")  #更改工作路径,注意双\ 任何 *** 作前可以先将常用包和路径先设置好
# 6.2.1 一对一关联规则挖掘
# 将原始数据转化为布尔数值表
tiem = ['西红柿','排骨','鸡蛋','茄子','袜子','酸奶','土豆','鞋子']
data = pd.read_excel('tr.xlsx',header = None)
data = data.iloc[:,1:]   
D = dict()
for t in range(len(tiem)):
    z=np.zeros((len(data)))
    li=list()
    for k in range(len(data.iloc[0,:])):
        s=data.iloc[:,k]==tiem[t]
        li.extend(list(s[s.values==True].index))
    z[li]=1
    D.setdefault(tiem[t],z)
Data=pd.DataFrame(D)  #布尔值数据表
print(Data)
   西红柿   排骨   鸡蛋   茄子   袜子   酸奶   土豆   鞋子
0  1.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0
1  1.0  0.0  0.0  1.0  0.0  0.0  0.0  0.0
2  0.0  0.0  1.0  0.0  1.0  0.0  0.0  0.0
3  1.0  1.0  0.0  1.0  0.0  0.0  0.0  0.0
4  1.0  1.0  0.0  0.0  1.0  1.0  0.0  0.0
5  0.0  0.0  1.0  1.0  0.0  1.0  0.0  0.0
6  0.0  1.0  1.0  1.0  0.0  0.0  0.0  0.0
7  0.0  0.0  1.0  0.0  1.0  0.0  1.0  0.0
8  1.0  1.0  0.0  0.0  0.0  0.0  1.0  1.0
# 构造两两项之间的关联规则
#获取字段名称,并转化为列表
c=list(Data.columns) 
c0=0.5 #最小置信度
s0=0.2 #最小支持度
list1=[] #预定义定义列表list1,用于存放规则
list2=[] #预定义定义列表list2,用于存放规则的支持度
list3=[] #预定义定义列表list3,用于存放规则的置信度
for k in range(len(c)):
    for q in range(len(c)):
        #对第c[k]个项与第c[q]个项挖掘关联规则
        #规则的前件为c[k]
        #规则的后件为c[q]
        #要求前件和后件不相等
        if c[k]!=c[q]:
           c1=Data[c[k]]
           c2=Data[c[q]]
           I1=c1.values==1
           I2=c2.values==1
           t12=np.zeros((len(c1)))
           t1=np.zeros((len(c1)))
           t12[I1&I2]=1
           t1[I1]=1
           sp=sum(t12)/len(c1) #支持度
           co=sum(t12)/sum(t1) #置信度
           #取置信度大于等于c0的关联规则
           if co>=c0 and sp>=s0:
              list1.append(c[k]+'--'+c[q])
              list2.append(sp)
              list3.append(co)
#定义字典,用于存放关联规则及其置信度、支持度   
R={'rule':list1,'support':list2,'confidence':list3}
#将字典转化为数据框
R=pd.DataFrame(R)
print(R)
#将结果导出到Excel
R.to_excel('rule1.xlsx')
      rule   support  confidence
0  西红柿--排骨  0.444444    0.800000
1  排骨--西红柿  0.444444    0.800000
2  茄子--西红柿  0.222222    0.500000
3   茄子--排骨  0.222222    0.500000
4   茄子--鸡蛋  0.222222    0.500000
5   袜子--鸡蛋  0.222222    0.666667
# 6.2.2 多对一关联规则
# Apriori算法:挖掘频繁项集
import apriori                 #导入自行编写的apriori函数
outputfile = 'apriori_rules.xls'     #结果文件
support = 0.2                  #最小支持度
confidence = 0.4               #最小置信度
ms = '---'                      #连接符,默认'--',
apriori.find_rule(Data, support, confidence, ms).to_excel(outputfile)     #保存结果到Excel
正在进行第1次搜索...
数目:21...

正在进行第2次搜索...
数目:4...

结果为:
           support  confidence
西红柿---排骨  0.444444    0.800000
排骨---西红柿  0.444444    0.800000
袜子---鸡蛋   0.222222    0.666667
茄子---西红柿  0.222222    0.500000
茄子---排骨   0.222222    0.500000
茄子---鸡蛋   0.222222    0.500000


:8: FutureWarning: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. This is the only engine in pandas that supports writing in the xls format. Install openpyxl and write to an xlsx file instead. You can set the option io.excel.xls.writer to 'xlwt' to silence this warning. While this option is deprecated and will also raise a warning, it can be globally set and the warning suppressed.
  apriori.find_rule(Data, support, confidence, ms).to_excel(outputfile)     #保存结果到Excel
# 6.3 关联规则现实应用:国际股票指数的关联分析
# 1.转化成布尔数据表
data = pd.read_excel('国际股票价格指数日交易数据表.xlsx')
code=list(data.iloc[:,0].value_counts().index)
D = dict()
for c in code:
    dt=data.loc[data['Indexcd'].values==c,['Trddt','Clsidx']].sort_values('Trddt')
    p1=dt['Clsidx'].values[:-1]
    p2=dt['Clsidx'].values[1:]
    z=np.zeros(len(p1))
    z[(p2-p1)/p1<=-0.005]=1
    S=pd.Series(z,index=dt['Trddt'].values[1:])
    D.setdefault(c,S)
print(D)
{'FCHI': 2010-01-05    0.0
2010-01-06    0.0
2010-01-07    0.0
2010-01-08    0.0
2010-01-11    0.0
             ... 
2019-08-16    0.0
2019-08-19    0.0
2019-08-20    1.0
2019-08-21    0.0
2019-08-22    1.0
Length: 2469, dtype: float64, 'GDAXI': 2010-01-05    0.0
2010-01-06    0.0
2010-01-07    0.0
2010-01-08    0.0
2010-01-11    0.0
             ... 
2019-08-16    0.0
2019-08-19    0.0
2019-08-20    1.0
2019-08-21    0.0
2019-08-22    0.0
Length: 2453, dtype: float64, 'STI': 2010-01-05    0.0
2010-01-06    0.0
2010-01-07    1.0
2010-01-08    0.0
2010-01-11    0.0
             ... 
2019-08-16    0.0
2019-08-19    0.0
2019-08-20    0.0
2019-08-21    0.0
2019-08-22    0.0
Length: 2444, dtype: float64, 'FTSE': 2010-01-05    0.0
2010-01-06    0.0
2010-01-07    0.0
2010-01-08    0.0
2010-01-11    0.0
             ... 
2019-08-16    0.0
2019-08-19    0.0
2019-08-20    1.0
2019-08-21    0.0
2019-08-22    1.0
Length: 2434, dtype: float64, 'DJI': 2010-01-05    0.0
2010-01-06    0.0
2010-01-07    0.0
2010-01-08    0.0
2010-01-11    0.0
             ... 
2019-08-16    0.0
2019-08-19    0.0
2019-08-20    1.0
2019-08-21    0.0
2019-08-22    0.0
Length: 2426, dtype: float64, 'HSI': 2010-01-05    0.0
2010-01-06    0.0
2010-01-07    1.0
2010-01-08    0.0
2010-01-11    0.0
             ... 
2019-08-16    0.0
2019-08-19    0.0
2019-08-20    0.0
2019-08-21    0.0
2019-08-22    1.0
Length: 2399, dtype: float64, 'KS11': 2010-01-05    0.0
2010-01-06    0.0
2010-01-07    1.0
2010-01-08    0.0
2010-01-11    0.0
             ... 
2019-08-16    1.0
2019-08-19    0.0
2019-08-20    0.0
2019-08-21    0.0
2019-08-22    1.0
Length: 2381, dtype: float64, 'N225': 2010-01-05    0.0
2010-01-06    0.0
2010-01-07    0.0
2010-01-08    0.0
2010-01-12    0.0
             ... 
2019-08-16    0.0
2019-08-19    0.0
2019-08-20    0.0
2019-08-21    0.0
2019-08-22    0.0
Length: 2379, dtype: float64, 'TWII': 2010-01-05    0.0
2010-01-06    0.0
2010-01-07    1.0
2010-01-08    0.0
2010-01-11    0.0
             ... 
2019-08-16    0.0
2019-08-19    0.0
2019-08-20    0.0
2019-08-21    0.0
2019-08-22    0.0
Length: 2375, dtype: float64, '000300': 2010-01-05    0.0
2010-01-06    1.0
2010-01-07    1.0
2010-01-08    0.0
2010-01-11    0.0
             ... 
2019-08-19    0.0
2019-08-20    0.0
2019-08-21    0.0
2019-08-22    0.0
2019-08-23    0.0
Length: 2344, dtype: float64, 'SENSEX': 2010-01-05    0.0
2010-01-06    0.0
2010-01-07    0.0
2010-01-08    0.0
2010-01-11    0.0
             ... 
2018-12-19    0.0
2018-12-20    1.0
2018-12-21    1.0
2018-12-24    1.0
2018-12-26    0.0
Length: 2201, dtype: float64, 'MCIX': 2010-01-12    1.0
2010-01-13    0.0
2010-01-14    0.0
2010-01-15    0.0
2010-01-18    0.0
             ... 
2018-03-19    1.0
2018-03-20    0.0
2018-03-21    0.0
2018-03-22    1.0
2018-03-23    0.0
Length: 2038, dtype: float64}
# 为了便于比较,将日期作一致化处理(以沪深300为例)
d000300 = data.loc[data['Indexcd'].values=='000300',['Trddt','Clsidx']].sort_values('Trddt')
tdate = list(d000300['Trddt'].values[1:])
Tf=[]
for t in tdate:
    tz=True;
    for k in code:
        s=D.get(k)
        s=list(s.index)
        sz=t in s
        tz=tz and sz
    if tz==True:
       Tf.append(t)
print(Tf)
['2010-01-12', '2010-01-13', '2010-01-14', '2010-01-15', '2010-01-19', '2010-01-20', '2010-01-21', '2010-01-22', '2010-01-25', '2010-01-27', '2010-01-28', '2010-01-29', '2010-02-01', '2010-02-02', '2010-02-03', '2010-02-04', '2010-02-05', '2010-02-08', '2010-02-09', '2010-02-10', '2010-02-24', '2010-02-25', '2010-02-26', '2010-03-02', '2010-03-03', '2010-03-04', '2010-03-05', '2010-03-09', '2010-03-10', '2010-03-11', '2010-03-12', '2010-03-15', '2010-03-16', '2010-03-17', '2010-03-18', '2010-03-19', '2010-03-23', '2010-03-25', '2010-03-26', '2010-03-29', '2010-03-30', '2010-03-31', '2010-04-01', '2010-04-06', '2010-04-07', '2010-04-08', '2010-04-09', '2010-04-12', '2010-04-13', '2010-04-15', '2010-04-16', '2010-04-19', '2010-04-20', '2010-04-21', '2010-04-22', '2010-04-23', '2010-04-26', '2010-04-27', '2010-04-28', '2010-04-30', '2010-05-06', '2010-05-07', '2010-05-11', '2010-05-12', '2010-05-13', '2010-05-14', '2010-05-17', '2010-05-18', '2010-05-19', '2010-05-20', '2010-05-24', '2010-05-25', '2010-05-26', '2010-05-27', '2010-05-28', '2010-06-01', '2010-06-03', '2010-06-04', '2010-06-07', '2010-06-08', '2010-06-09', '2010-06-10', '2010-06-11', '2010-06-17', '2010-06-18', '2010-06-21', '2010-06-22', '2010-06-23', '2010-06-24', '2010-06-25', '2010-06-28', '2010-06-29', '2010-06-30', '2010-07-01', '2010-07-02', '2010-07-06', '2010-07-07', '2010-07-08', '2010-07-09', '2010-07-12', '2010-07-13', '2010-07-14', '2010-07-15', '2010-07-16', '2010-07-20', '2010-07-21', '2010-07-22', '2010-07-23', '2010-07-26', '2010-07-27', '2010-07-28', '2010-07-29', '2010-07-30', '2010-08-02', '2010-08-03', '2010-08-04', '2010-08-05', '2010-08-06', '2010-08-09', '2010-08-10', '2010-08-11', '2010-08-12', '2010-08-13', '2010-08-16', '2010-08-17', '2010-08-18', '2010-08-19', '2010-08-20', '2010-08-23', '2010-08-24', '2010-08-25', '2010-08-26', '2010-08-27', '2010-08-31', '2010-09-01', '2010-09-02', '2010-09-03', '2010-09-07', '2010-09-08', '2010-09-09', '2010-09-13', '2010-09-14', '2010-09-15', '2010-09-16', '2010-09-17', '2010-09-27', '2010-09-28', '2010-09-29', '2010-09-30', '2010-10-08', '2010-10-12', '2010-10-13', '2010-10-14', '2010-10-15', '2010-10-18', '2010-10-19', '2010-10-20', '2010-10-21', '2010-10-22', '2010-10-25', '2010-10-26', '2010-10-27', '2010-10-28', '2010-10-29', '2010-11-01', '2010-11-02', '2010-11-08', '2010-11-09', '2010-11-10', '2010-11-11', '2010-11-12', '2010-11-15', '2010-11-16', '2010-11-18', '2010-11-19', '2010-11-22', '2010-11-24', '2010-11-26', '2010-11-29', '2010-11-30', '2010-12-01', '2010-12-02', '2010-12-03', '2010-12-06', '2010-12-07', '2010-12-08', '2010-12-09', '2010-12-10', '2010-12-13', '2010-12-14', '2010-12-15', '2010-12-16', '2010-12-20', '2010-12-21', '2010-12-22', '2010-12-29', '2010-12-30', '2011-01-11', '2011-01-12', '2011-01-13', '2011-01-14', '2011-01-18', '2011-01-19', '2011-01-20', '2011-01-21', '2011-01-24', '2011-01-25', '2011-01-27', '2011-01-28', '2011-02-09', '2011-02-10', '2011-02-14', '2011-02-15', '2011-02-16', '2011-02-17', '2011-02-18', '2011-02-22', '2011-02-24', '2011-02-25', '2011-03-03', '2011-03-04', '2011-03-09', '2011-03-10', '2011-03-11', '2011-03-14', '2011-03-15', '2011-03-16', '2011-03-17', '2011-03-18', '2011-03-22', '2011-03-23', '2011-03-24', '2011-03-25', '2011-03-28', '2011-03-29', '2011-03-30', '2011-03-31', '2011-04-01', '2011-04-06', '2011-04-07', '2011-04-08', '2011-04-11', '2011-04-13', '2011-04-15', '2011-04-18', '2011-04-19', '2011-04-20', '2011-04-21', '2011-04-26', '2011-04-27', '2011-04-28', '2011-05-06', '2011-05-11', '2011-05-12', '2011-05-13', '2011-05-16', '2011-05-17', '2011-05-18', '2011-05-19', '2011-05-20', '2011-05-23', '2011-05-24', '2011-05-25', '2011-05-26', '2011-05-27', '2011-05-31', '2011-06-01', '2011-06-02', '2011-06-03', '2011-06-07', '2011-06-08', '2011-06-09', '2011-06-10', '2011-06-14', '2011-06-15', '2011-06-16', '2011-06-17', '2011-06-20', '2011-06-21', '2011-06-22', '2011-06-23', '2011-06-24', '2011-06-27', '2011-06-28', '2011-06-29', '2011-06-30', '2011-07-05', '2011-07-06', '2011-07-07', '2011-07-08', '2011-07-11', '2011-07-12', '2011-07-13', '2011-07-14', '2011-07-15', '2011-07-19', '2011-07-20', '2011-07-21', '2011-07-22', '2011-07-25', '2011-07-26', '2011-07-27', '2011-07-28', '2011-07-29', '2011-08-01', '2011-08-02', '2011-08-03', '2011-08-04', '2011-08-05', '2011-08-08', '2011-08-09', '2011-08-10', '2011-08-11', '2011-08-12', '2011-08-16', '2011-08-17', '2011-08-18', '2011-08-19', '2011-08-22', '2011-08-23', '2011-08-24', '2011-08-25', '2011-08-26', '2011-08-30', '2011-09-02', '2011-09-06', '2011-09-07', '2011-09-08', '2011-09-09', '2011-09-14', '2011-09-15', '2011-09-16', '2011-09-20', '2011-09-21', '2011-09-22', '2011-09-26', '2011-09-27', '2011-09-28', '2011-09-29', '2011-09-30', '2011-10-11', '2011-10-12', '2011-10-13', '2011-10-14', '2011-10-17', '2011-10-18', '2011-10-19', '2011-10-20', '2011-10-21', '2011-10-24', '2011-10-25', '2011-10-26', '2011-10-28', '2011-10-31', '2011-11-01', '2011-11-02', '2011-11-08', '2011-11-09', '2011-11-11', '2011-11-14', '2011-11-15', '2011-11-16', '2011-11-17', '2011-11-18', '2011-11-21', '2011-11-22', '2011-11-25', '2011-11-28', '2011-11-29', '2011-11-30', '2011-12-01', '2011-12-02', '2011-12-05', '2011-12-07', '2011-12-08', '2011-12-09', '2011-12-12', '2011-12-13', '2011-12-14', '2011-12-15', '2011-12-16', '2011-12-19', '2011-12-20', '2011-12-21', '2011-12-22', '2011-12-28', '2011-12-29', '2012-01-04', '2012-01-05', '2012-01-06', '2012-01-10', '2012-01-11', '2012-01-12', '2012-01-13', '2012-01-17', '2012-01-18', '2012-01-30', '2012-01-31', '2012-02-01', '2012-02-02', '2012-02-03', '2012-02-06', '2012-02-07', '2012-02-08', '2012-02-09', '2012-02-10', '2012-02-13', '2012-02-14', '2012-02-15', '2012-02-16', '2012-02-17', '2012-02-21', '2012-02-22', '2012-02-24', '2012-02-29', '2012-03-02', '2012-03-05', '2012-03-06', '2012-03-07', '2012-03-12', '2012-03-13', '2012-03-14', '2012-03-15', '2012-03-16', '2012-03-19', '2012-03-21', '2012-03-22', '2012-03-23', '2012-03-26', '2012-03-27', '2012-03-28', '2012-03-29', '2012-03-30', '2012-04-10', '2012-04-12', '2012-04-13', '2012-04-16', '2012-04-17', '2012-04-18', '2012-04-19', '2012-04-20', '2012-04-23', '2012-04-24', '2012-04-25', '2012-04-26', '2012-04-27', '2012-05-02', '2012-05-08', '2012-05-10', '2012-05-11', '2012-05-14', '2012-05-15', '2012-05-16', '2012-05-17', '2012-05-18', '2012-05-21', '2012-05-22', '2012-05-23', '2012-05-24', '2012-05-25', '2012-05-29', '2012-05-30', '2012-05-31', '2012-06-01', '2012-06-07', '2012-06-08', '2012-06-13', '2012-06-14', '2012-06-15', '2012-06-18', '2012-06-19', '2012-06-20', '2012-06-21', '2012-06-25', '2012-06-26', '2012-06-27', '2012-06-28', '2012-06-29', '2012-07-02', '2012-07-03', '2012-07-05', '2012-07-06', '2012-07-09', '2012-07-10', '2012-07-11', '2012-07-12', '2012-07-13', '2012-07-17', '2012-07-18', '2012-07-19', '2012-07-20', '2012-07-23', '2012-07-24', '2012-07-25', '2012-07-26', '2012-07-27', '2012-07-30', '2012-07-31', '2012-08-01', '2012-08-03', '2012-08-06', '2012-08-07', '2012-08-08', '2012-08-09', '2012-08-10', '2012-08-13', '2012-08-14', '2012-08-16', '2012-08-17', '2012-08-21', '2012-08-22', '2012-08-23', '2012-08-24', '2012-08-28', '2012-08-29', '2012-08-30', '2012-08-31', '2012-09-04', '2012-09-05', '2012-09-06', '2012-09-07', '2012-09-10', '2012-09-11', '2012-09-12', '2012-09-13', '2012-09-14', '2012-09-18', '2012-09-20', '2012-09-21', '2012-09-24', '2012-09-25', '2012-09-26', '2012-09-27', '2012-09-28', '2012-10-09', '2012-10-11', '2012-10-12', '2012-10-15', '2012-10-16', '2012-10-17', '2012-10-18', '2012-10-19', '2012-10-22', '2012-10-25', '2012-10-26', '2012-10-30', '2012-10-31', '2012-11-01', '2012-11-02', '2012-11-06', '2012-11-07', '2012-11-08', '2012-11-09', '2012-11-12', '2012-11-13', '2012-11-15', '2012-11-16', '2012-11-19', '2012-11-20', '2012-11-21', '2012-11-26', '2012-11-27', '2012-11-29', '2012-11-30', '2012-12-03', '2012-12-04', '2012-12-05', '2012-12-06', '2012-12-07', '2012-12-10', '2012-12-11', '2012-12-12', '2012-12-13', '2012-12-14', '2012-12-17', '2012-12-18', '2012-12-20', '2012-12-21', '2012-12-27', '2012-12-28', '2013-01-08', '2013-01-09', '2013-01-10', '2013-01-11', '2013-01-15', '2013-01-16', '2013-01-17', '2013-01-18', '2013-01-22', '2013-01-23', '2013-01-24', '2013-01-25', '2013-01-28', '2013-01-29', '2013-01-30', '2013-01-31', '2013-02-01', '2013-02-04', '2013-02-05', '2013-02-06', '2013-02-19', '2013-02-20', '2013-02-21', '2013-02-22', '2013-02-25', '2013-02-26', '2013-02-27', '2013-02-28', '2013-03-04', '2013-03-05', '2013-03-06', '2013-03-07', '2013-03-11', '2013-03-12', '2013-03-13', '2013-03-14', '2013-03-15', '2013-03-18', '2013-03-19', '2013-03-21', '2013-03-22', '2013-03-25', '2013-03-26', '2013-03-28', '2013-04-02', '2013-04-03', '2013-04-08', '2013-04-09', '2013-04-10', '2013-04-11', '2013-04-12', '2013-04-15', '2013-04-16', '2013-04-17', '2013-04-18', '2013-04-22', '2013-04-23', '2013-04-25', '2013-04-26', '2013-05-02', '2013-05-07', '2013-05-08', '2013-05-10', '2013-05-13', '2013-05-14', '2013-05-15', '2013-05-16', '2013-05-20', '2013-05-21', '2013-05-22', '2013-05-23', '2013-05-24', '2013-05-28', '2013-05-29', '2013-05-30', '2013-05-31', '2013-06-03', '2013-06-04', '2013-06-05', '2013-06-07', '2013-06-13', '2013-06-14', '2013-06-17', '2013-06-18', '2013-06-19', '2013-06-20', '2013-06-21', '2013-06-24', '2013-06-25', '2013-06-26', '2013-06-27', '2013-06-28', '2013-07-01', '2013-07-02', '2013-07-03', '2013-07-05', '2013-07-08', '2013-07-09', '2013-07-10', '2013-07-11', '2013-07-12', '2013-07-16', '2013-07-17', '2013-07-18', '2013-07-19', '2013-07-22', '2013-07-23', '2013-07-24', '2013-07-25', '2013-07-26', '2013-07-29', '2013-07-30', '2013-07-31', '2013-08-01', '2013-08-02', '2013-08-05', '2013-08-06', '2013-08-07', '2013-08-08', '2013-08-12', '2013-08-13', '2013-08-16', '2013-08-19', '2013-08-20', '2013-08-22', '2013-08-23', '2013-08-27', '2013-08-28', '2013-08-29', '2013-08-30', '2013-09-03', '2013-09-04', '2013-09-05', '2013-09-06', '2013-09-10', '2013-09-11', '2013-09-12', '2013-09-13', '2013-09-17', '2013-09-23', '2013-09-24', '2013-09-25', '2013-09-26', '2013-09-27', '2013-09-30', '2013-10-08', '2013-10-11', '2013-10-17', '2013-10-18', '2013-10-21', '2013-10-22', '2013-10-23', '2013-10-24', '2013-10-25', '2013-10-28', '2013-10-29', '2013-10-30', '2013-10-31', '2013-11-01', '2013-11-05', '2013-11-06', '2013-11-07', '2013-11-08', '2013-11-11', '2013-11-12', '2013-11-13', '2013-11-14', '2013-11-18', '2013-11-19', '2013-11-20', '2013-11-21', '2013-11-22', '2013-11-25', '2013-11-26', '2013-11-27', '2013-11-29', '2013-12-02', '2013-12-03', '2013-12-04', '2013-12-05', '2013-12-06', '2013-12-09', '2013-12-10', '2013-12-11', '2013-12-12', '2013-12-13', '2013-12-16', '2013-12-17', '2013-12-18', '2013-12-19', '2013-12-20', '2013-12-23', '2013-12-24', '2013-12-27', '2013-12-30', '2014-01-06', '2014-01-08', '2014-01-09', '2014-01-10', '2014-01-13', '2014-01-14', '2014-01-15', '2014-01-16', '2014-01-17', '2014-01-21', '2014-01-22', '2014-01-23', '2014-01-24', '2014-01-27', '2014-02-07', '2014-02-10', '2014-02-12', '2014-02-13', '2014-02-14', '2014-02-18', '2014-02-19', '2014-02-20', '2014-02-21', '2014-02-24', '2014-02-25', '2014-02-26', '2014-03-03', '2014-03-04', '2014-03-05', '2014-03-06', '2014-03-07', '2014-03-11', '2014-03-12', '2014-03-13', '2014-03-14', '2014-03-18', '2014-03-19', '2014-03-20', '2014-03-24', '2014-03-25', '2014-03-26', '2014-03-27', '2014-03-28', '2014-03-31', '2014-04-01', '2014-04-02', '2014-04-03', '2014-04-09', '2014-04-10', '2014-04-11', '2014-04-15', '2014-04-16', '2014-04-17', '2014-04-22', '2014-04-23', '2014-04-25', '2014-04-28', '2014-04-29', '2014-04-30', '2014-05-07', '2014-05-08', '2014-05-12', '2014-05-14', '2014-05-15', '2014-05-16', '2014-05-19', '2014-05-20', '2014-05-21', '2014-05-22', '2014-05-23', '2014-05-27', '2014-05-28', '2014-05-29', '2014-05-30', '2014-06-03', '2014-06-05', '2014-06-09', '2014-06-10', '2014-06-11', '2014-06-16', '2014-06-17', '2014-06-18', '2014-06-19', '2014-06-20', '2014-06-23', '2014-06-24', '2014-06-25', '2014-06-26', '2014-06-27', '2014-06-30', '2014-07-02', '2014-07-03', '2014-07-07', '2014-07-08', '2014-07-09', '2014-07-10', '2014-07-11', '2014-07-14', '2014-07-15', '2014-07-16', '2014-07-17', '2014-07-18', '2014-07-21', '2014-07-22', '2014-07-23', '2014-07-24', '2014-07-25', '2014-07-30', '2014-07-31', '2014-08-01', '2014-08-04', '2014-08-05', '2014-08-06', '2014-08-07', '2014-08-08', '2014-08-11', '2014-08-12', '2014-08-13', '2014-08-14', '2014-08-18', '2014-08-19', '2014-08-20', '2014-08-21', '2014-08-22', '2014-08-26', '2014-08-27', '2014-08-28', '2014-09-02', '2014-09-03', '2014-09-04', '2014-09-05', '2014-09-11', '2014-09-12', '2014-09-15', '2014-09-16', '2014-09-17', '2014-09-18', '2014-09-19', '2014-09-22', '2014-09-24', '2014-09-25', '2014-09-26', '2014-09-29', '2014-09-30', '2014-10-08', '2014-10-13', '2014-10-14', '2014-10-16', '2014-10-17', '2014-10-20', '2014-10-21', '2014-10-23', '2014-10-27', '2014-10-28', '2014-10-29', '2014-10-30', '2014-10-31', '2014-11-05', '2014-11-07', '2014-11-10', '2014-11-11', '2014-11-12', '2014-11-13', '2014-11-14', '2014-11-17', '2014-11-18', '2014-11-19', '2014-11-20', '2014-11-21', '2014-11-24', '2014-11-25', '2014-11-26', '2014-11-28', '2014-12-01', '2014-12-02', '2014-12-03', '2014-12-04', '2014-12-05', '2014-12-08', '2014-12-09', '2014-12-10', '2014-12-11', '2014-12-12', '2014-12-15', '2014-12-16', '2014-12-17', '2014-12-18', '2014-12-19', '2014-12-22', '2014-12-29', '2014-12-30', '2015-01-05', '2015-01-06', '2015-01-08', '2015-01-09', '2015-01-13', '2015-01-14', '2015-01-15', '2015-01-16', '2015-01-20', '2015-01-21', '2015-01-22', '2015-01-23', '2015-01-27', '2015-01-28', '2015-01-29', '2015-01-30', '2015-02-02', '2015-02-03', '2015-02-04', '2015-02-05', '2015-02-06', '2015-02-09', '2015-02-10', '2015-02-11', '2015-02-12', '2015-02-13', '2015-02-25', '2015-02-26', '2015-02-27', '2015-03-02', '2015-03-03', '2015-03-04', '2015-03-05', '2015-03-10', '2015-03-11', '2015-03-12', '2015-03-13', '2015-03-16', '2015-03-17', '2015-03-18', '2015-03-19', '2015-03-20', '2015-03-23', '2015-03-24', '2015-03-25', '2015-03-26', '2015-03-27', '2015-03-30', '2015-03-31', '2015-04-01', '2015-04-07', '2015-04-08', '2015-04-09', '2015-04-10', '2015-04-13', '2015-04-15', '2015-04-16', '2015-04-17', '2015-04-20', '2015-04-21', '2015-04-22', '2015-04-23', '2015-04-24', '2015-04-27', '2015-04-28', '2015-04-30', '2015-05-06', '2015-05-07', '2015-05-08', '2015-05-12', '2015-05-13', '2015-05-14', '2015-05-15', '2015-05-18', '2015-05-19', '2015-05-20', '2015-05-21', '2015-05-22', '2015-05-26', '2015-05-27', '2015-05-28', '2015-05-29', '2015-06-02', '2015-06-03', '2015-06-04', '2015-06-05', '2015-06-08', '2015-06-09', '2015-06-10', '2015-06-11', '2015-06-15', '2015-06-16', '2015-06-17', '2015-06-18', '2015-06-23', '2015-06-24', '2015-06-25', '2015-06-26', '2015-06-29', '2015-06-30', '2015-07-02', '2015-07-06', '2015-07-07', '2015-07-08', '2015-07-09', '2015-07-13', '2015-07-14', '2015-07-15', '2015-07-16', '2015-07-21', '2015-07-22', '2015-07-23', '2015-07-24', '2015-07-27', '2015-07-28', '2015-07-29', '2015-07-30', '2015-07-31', '2015-08-03', '2015-08-04', '2015-08-05', '2015-08-06', '2015-08-11', '2015-08-12', '2015-08-13', '2015-08-17', '2015-08-18', '2015-08-19', '2015-08-20', '2015-08-21', '2015-08-24', '2015-08-25', '2015-08-26', '2015-08-27', '2015-08-28', '2015-09-01', '2015-09-02', '2015-09-08', '2015-09-09', '2015-09-10', '2015-09-14', '2015-09-15', '2015-09-16', '2015-09-18', '2015-09-30', '2015-10-08', '2015-10-13', '2015-10-14', '2015-10-15', '2015-10-16', '2015-10-19', '2015-10-20', '2015-10-23', '2015-10-26', '2015-10-27', '2015-10-28', '2015-10-29', '2015-10-30', '2015-11-02', '2015-11-05', '2015-11-06', '2015-11-09', '2015-11-11', '2015-11-13', '2015-11-17', '2015-11-18', '2015-11-19', '2015-11-20', '2015-11-24', '2015-11-27', '2015-11-30', '2015-12-01', '2015-12-02', '2015-12-03', '2015-12-04', '2015-12-07', '2015-12-08', '2015-12-09', '2015-12-10', '2015-12-11', '2015-12-14', '2015-12-15', '2015-12-16', '2015-12-17', '2015-12-18', '2015-12-21', '2015-12-22', '2015-12-29', '2015-12-30', '2016-01-04', '2016-01-05', '2016-01-06', '2016-01-12', '2016-01-13', '2016-01-14', '2016-01-15', '2016-01-19', '2016-01-20', '2016-01-21', '2016-01-22', '2016-01-25', '2016-01-27', '2016-01-28', '2016-01-29', '2016-02-01', '2016-02-02', '2016-02-03', '2016-02-16', '2016-02-17', '2016-02-18', '2016-02-19', '2016-02-22', '2016-02-24', '2016-02-25', '2016-02-26', '2016-03-02', '2016-03-03', '2016-03-04', '2016-03-09', '2016-03-10', '2016-03-11', '2016-03-14', '2016-03-15', '2016-03-16', '2016-03-17', '2016-03-18', '2016-03-22', '2016-03-23', '2016-03-29', '2016-03-30', '2016-03-31', '2016-04-01', '2016-04-06', '2016-04-07', '2016-04-08', '2016-04-11', '2016-04-12', '2016-04-18', '2016-04-20', '2016-04-21', '2016-04-22', '2016-04-25', '2016-04-26', '2016-04-27', '2016-04-28', '2016-05-10', '2016-05-11', '2016-05-12', '2016-05-13', '2016-05-17', '2016-05-18', '2016-05-19', '2016-05-20', '2016-05-23', '2016-05-24', '2016-05-25', '2016-05-26', '2016-05-27', '2016-05-31', '2016-06-01', '2016-06-02', '2016-06-03', '2016-06-07', '2016-06-08', '2016-06-14', '2016-06-15', '2016-06-16', '2016-06-17', '2016-06-20', '2016-06-21', '2016-06-22', '2016-06-23', '2016-06-24', '2016-06-27', '2016-06-28', '2016-06-29', '2016-06-30', '2016-07-05', '2016-07-07', '2016-07-11', '2016-07-12', '2016-07-13', '2016-07-14', '2016-07-15', '2016-07-19', '2016-07-20', '2016-07-21', '2016-07-22', '2016-07-25', '2016-07-26', '2016-07-27', '2016-07-28', '2016-07-29', '2016-08-01', '2016-08-03', '2016-08-04', '2016-08-05', '2016-08-08', '2016-08-10', '2016-08-12', '2016-08-16', '2016-08-17', '2016-08-18', '2016-08-19', '2016-08-22', '2016-08-23', '2016-08-24', '2016-08-25', '2016-08-26', '2016-08-30', '2016-08-31', '2016-09-01', '2016-09-02', '2016-09-06', '2016-09-07', '2016-09-08', '2016-09-09', '2016-09-20', '2016-09-21', '2016-09-23', '2016-09-26', '2016-09-29', '2016-09-30', '2016-10-13', '2016-10-14', '2016-10-17', '2016-10-19', '2016-10-20', '2016-10-24', '2016-10-25', '2016-10-26', '2016-10-27', '2016-10-28', '2016-11-01', '2016-11-02', '2016-11-07', '2016-11-08', '2016-11-09', '2016-11-10', '2016-11-11', '2016-11-15', '2016-11-16', '2016-11-17', '2016-11-21', '2016-11-22', '2016-11-25', '2016-11-28', '2016-11-29', '2016-11-30', '2016-12-01', '2016-12-02', '2016-12-05', '2016-12-06', '2016-12-07', '2016-12-08', '2016-12-09', '2016-12-12', '2016-12-13', '2016-12-14', '2016-12-15', '2016-12-16', '2016-12-19', '2016-12-20', '2016-12-21', '2016-12-22', '2016-12-28', '2016-12-29', '2017-01-04', '2017-01-05', '2017-01-06', '2017-01-10', '2017-01-11', '2017-01-12', '2017-01-13', '2017-01-17', '2017-01-18', '2017-01-19', '2017-01-20', '2017-01-23', '2017-01-24', '2017-02-03', '2017-02-07', '2017-02-08', '2017-02-09', '2017-02-10', '2017-02-13', '2017-02-14', '2017-02-15', '2017-02-16', '2017-02-17', '2017-02-21', '2017-02-22', '2017-03-02', '2017-03-03', '2017-03-06', '2017-03-07', '2017-03-09', '2017-03-10', '2017-03-14', '2017-03-15', '2017-03-16', '2017-03-17', '2017-03-20', '2017-03-21', '2017-03-22', '2017-03-23', '2017-03-24', '2017-03-27', '2017-03-28', '2017-03-29', '2017-03-30', '2017-03-31', '2017-04-05', '2017-04-06', '2017-04-07', '2017-04-10', '2017-04-11', '2017-04-12', '2017-04-13', '2017-04-18', '2017-04-19', '2017-04-20', '2017-04-21', '2017-04-24', '2017-04-25', '2017-04-26', '2017-04-27', '2017-04-28', '2017-05-02', '2017-05-11', '2017-05-12', '2017-05-15', '2017-05-16', '2017-05-17', '2017-05-18', '2017-05-19', '2017-05-22', '2017-05-23', '2017-05-24', '2017-05-25', '2017-05-26', '2017-05-31', '2017-06-01', '2017-06-02', '2017-06-07', '2017-06-08', '2017-06-09', '2017-06-13', '2017-06-14', '2017-06-15', '2017-06-16', '2017-06-19', '2017-06-20', '2017-06-21', '2017-06-22', '2017-06-23', '2017-06-27', '2017-06-28', '2017-06-29', '2017-06-30', '2017-07-03', '2017-07-05', '2017-07-06', '2017-07-10', '2017-07-11', '2017-07-12', '2017-07-13', '2017-07-14', '2017-07-18', '2017-07-19', '2017-07-20', '2017-07-21', '2017-07-24', '2017-07-25', '2017-07-26', '2017-07-27', '2017-07-28', '2017-07-31', '2017-08-01', '2017-08-02', '2017-08-03', '2017-08-04', '2017-08-07', '2017-08-08', '2017-08-10', '2017-08-11', '2017-08-14', '2017-08-16', '2017-08-17', '2017-08-18', '2017-08-21', '2017-08-22', '2017-08-24', '2017-08-29', '2017-08-30', '2017-08-31', '2017-09-05', '2017-09-06', '2017-09-07', '2017-09-08', '2017-09-11', '2017-09-12', '2017-09-13', '2017-09-14', '2017-09-15', '2017-09-19', '2017-09-20', '2017-09-21', '2017-09-22', '2017-09-25', '2017-09-26', '2017-09-27', '2017-09-28', '2017-09-29', '2017-10-11', '2017-10-12', '2017-10-13', '2017-10-16', '2017-10-17', '2017-10-19', '2017-10-23', '2017-10-24', '2017-10-25', '2017-10-30', '2017-11-01', '2017-11-02', '2017-11-03', '2017-11-07', '2017-11-08', '2017-11-09', '2017-11-10', '2017-11-13', '2017-11-14', '2017-11-15', '2017-11-16', '2017-11-17', '2017-11-20', '2017-11-21', '2017-11-22', '2017-11-24', '2017-11-27', '2017-11-28', '2017-11-29', '2017-11-30', '2017-12-01', '2017-12-04', '2017-12-05', '2017-12-06', '2017-12-07', '2017-12-08', '2017-12-11', '2017-12-12', '2017-12-13', '2017-12-14', '2017-12-15', '2017-12-18', '2017-12-19', '2017-12-21', '2017-12-22', '2017-12-27', '2017-12-28', '2018-01-04', '2018-01-05', '2018-01-09', '2018-01-10', '2018-01-11', '2018-01-12', '2018-01-16', '2018-01-17', '2018-01-18', '2018-01-19', '2018-01-22', '2018-01-23', '2018-01-24', '2018-01-25', '2018-01-29', '2018-01-30', '2018-01-31', '2018-02-01', '2018-02-02', '2018-02-05', '2018-02-06', '2018-02-07', '2018-02-08', '2018-02-09', '2018-02-14', '2018-02-22', '2018-02-26', '2018-02-27', '2018-02-28', '2018-03-05', '2018-03-06', '2018-03-07', '2018-03-09', '2018-03-12', '2018-03-13', '2018-03-14', '2018-03-15', '2018-03-16', '2018-03-19', '2018-03-20', '2018-03-22', '2018-03-23']
# 转化为数据框结构
DA=dict()
for k in code:
  s=D.get(k)
  DA.setdefault(k,s[Tf].values)
Data=pd.DataFrame(DA,index=Tf)
print(Data.head(20)) #打印前20行
            FCHI  GDAXI  STI  FTSE  DJI  HSI  KS11  N225  TWII  000300  \
2010-01-12   1.0    1.0  1.0   1.0  0.0  0.0   0.0   0.0   0.0     0.0   
2010-01-13   0.0    0.0  1.0   0.0  0.0  1.0   1.0   1.0   1.0     1.0   
2010-01-14   0.0    0.0  0.0   0.0  0.0  0.0   0.0   0.0   0.0     0.0   
2010-01-15   1.0    1.0  0.0   1.0  1.0  0.0   0.0   0.0   0.0     0.0   
2010-01-19   0.0    0.0  0.0   0.0  0.0  0.0   0.0   1.0   1.0     0.0   
2010-01-20   1.0    1.0  1.0   1.0  1.0  1.0   0.0   0.0   0.0     1.0   
2010-01-21   1.0    1.0  1.0   1.0  1.0  1.0   0.0   0.0   1.0     0.0   
2010-01-22   1.0    1.0  1.0   1.0  1.0  1.0   1.0   1.0   1.0     1.0   
2010-01-25   1.0    1.0  0.0   1.0  0.0  1.0   1.0   1.0   1.0     1.0   
2010-01-27   1.0    0.0  1.0   1.0  0.0  0.0   1.0   1.0   1.0     1.0   
2010-01-28   1.0    1.0  0.0   1.0  1.0  0.0   0.0   0.0   0.0     0.0   
2010-01-29   0.0    0.0  0.0   0.0  1.0  1.0   1.0   1.0   1.0     0.0   
2010-02-01   0.0    0.0  0.0   0.0  0.0  0.0   0.0   0.0   1.0     1.0   
2010-02-02   0.0    0.0  1.0   0.0  0.0  0.0   1.0   0.0   1.0     0.0   
2010-02-03   0.0    1.0  0.0   1.0  0.0  0.0   0.0   0.0   0.0     0.0   
2010-02-04   1.0    1.0  1.0   1.0  1.0  1.0   0.0   0.0   0.0     0.0   
2010-02-05   1.0    1.0  1.0   1.0  0.0  1.0   1.0   1.0   1.0     1.0   
2010-02-08   0.0    0.0  0.0   0.0  1.0  1.0   1.0   1.0   0.0     0.0   
2010-02-09   0.0    0.0  0.0   0.0  0.0  0.0   0.0   0.0   0.0     0.0   
2010-02-10   0.0    0.0  0.0   0.0  0.0  0.0   0.0   0.0   0.0     0.0   

            SENSEX  MCIX  
2010-01-12     1.0   1.0  
2010-01-13     0.0   0.0  
2010-01-14     0.0   0.0  
2010-01-15     0.0   0.0  
2010-01-19     1.0   0.0  
2010-01-20     0.0   1.0  
2010-01-21     1.0   1.0  
2010-01-22     1.0   1.0  
2010-01-25     0.0   0.0  
2010-01-27     1.0   0.0  
2010-01-28     0.0   0.0  
2010-01-29     0.0   0.0  
2010-02-01     0.0   0.0  
2010-02-02     1.0   0.0  
2010-02-03     0.0   0.0  
2010-02-04     1.0   1.0  
2010-02-05     1.0   1.0  
2010-02-08     0.0   1.0  
2010-02-09     0.0   0.0  
2010-02-10     1.0   1.0  
# 关联规则设置(一对一关联)
c=list(Data.columns) 
c0=0.6 #最小置信度
s0=0.1 #最小支持度
list1=[] #预定义定义列表list1,用于存放规则
list2=[] #预定义定义列表list2,用于存放规则的支持度
list3=[] #预定义定义列表list3,用于存放规则的置信度
for k in range(len(c)):
    for q in range(len(c)):
        #对第c[k]个项与第c[q]个项挖掘关联规则
        #规则的前件为c[k]
        #规则的后件为c[q]
        #要求前件和后件不相等
        if c[k]!=c[q]:
           c1=Data[c[k]]
           c2=Data[c[q]]
           I1=c1.values==1
           I2=c2.values==1
           t12=np.zeros((len(c1)))
           t1=np.zeros((len(c1)))
           t12[I1&I2]=1
           t1[I1]=1
           sp=sum(t12)/len(c1) #支持度
           co=sum(t12)/sum(t1) #置信度
           #取置信度大于等于c0的关联规则
           if co>=c0 and sp>=s0:
              list1.append(c[k]+'--'+c[q])
              list2.append(sp)
              list3.append(co)
#定义字典,用于存放关联规则及其置信度、支持度   
R={'rule':list1,'support':list2,'confidence':list3}
#将字典转化为数据框
R=pd.DataFrame(R)
#将结果导出到Excel
R.to_excel('rule2.xlsx')
print(R) 
           rule   support  confidence
0   FCHI--GDAXI  0.224984    0.779249
1    FCHI--FTSE  0.191842    0.664459
2   GDAXI--FCHI  0.224984    0.836493
3   GDAXI--FTSE  0.184194    0.684834
4      STI--HSI  0.144041    0.704050
5    FTSE--FCHI  0.191842    0.775773
6   FTSE--GDAXI  0.184194    0.744845
7     DJI--FCHI  0.130019    0.680000
8    DJI--GDAXI  0.121096    0.633333
9     KS11--HSI  0.139579    0.636628
10   KS11--N225  0.138942    0.633721
# 多对一的关联
import apriori as ap
support = 0.08 #最小支持度
confidence = 0.9 #最小置信度
ms = '--' #连接符,
outputfile = 'apriori_rules1.xls' #结果文件
ap.find_rule(Data, support, confidence, ms).to_excel(outputfile) #联动
正在进行第1次搜索...
数目:66...

正在进行第2次搜索...
数目:186...

正在进行第3次搜索...
数目:85...

正在进行第4次搜索...
数目:6...

结果为:
                            support  confidence
DJI--FTSE--GDAXI--FCHI     0.097514    0.968354
FTSE--GDAXI--SENSEX--FCHI  0.087317    0.958042
DJI--FTSE--FCHI            0.104525    0.937143
DJI--GDAXI--FCHI           0.113448    0.936842
DJI--FCHI--FTSE--GDAXI     0.097514    0.932927
FTSE--GDAXI--N225--FCHI    0.085405    0.930556
FTSE--GDAXI--MCIX--FCHI    0.109624    0.929730
FTSE--GDAXI--HSI--FCHI     0.095602    0.914634
FCHI--FTSE--HSI--GDAXI     0.095602    0.914634
FTSE--GDAXI--FCHI          0.168260    0.913495
FCHI--FTSE--N225--GDAXI    0.085405    0.911565
GDAXI--SENSEX--FCHI        0.105163    0.906593
DJI--FTSE--GDAXI           0.100701    0.902857


:7: FutureWarning: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. This is the only engine in pandas that supports writing in the xls format. Install openpyxl and write to an xlsx file instead. You can set the option io.excel.xls.writer to 'xlwt' to silence this warning. While this option is deprecated and will also raise a warning, it can be globally set and the warning suppressed.
  ap.find_rule(Data, support, confidence, ms).to_excel(outputfile) #联动

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/796973.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存