首页 > 其他分享 >Goods

Goods

时间:2023-03-19 23:35:17浏览次数:47  
标签:plt selected list supportData 项集 Goods data

selected = sort_links.loc[sort_links['Types'] == '果蔬']  # 挑选商品类别为“果蔬”并排序
child_nums = selected['id'].sum()  # 对所有的“果蔬”求和
selected['child_percent'] = selected.apply(lambda line: line['id']/child_nums,axis = 1)  # 求百分比
selected.rename(columns = {'id':'count'},inplace = True)
print('果蔬内部商品的销量及其占比:\n',selected)
outfile2 = 'E:/data/child_percent.csv'
sort_link.to_csv(outfile2,index = False,header = True,encoding='gbk')  # 输出结果

# 画饼图展示果蔬内部各商品的销量占比
import matplotlib.pyplot as plt
data = selected['child_percent']
labels = selected['Goods']
plt.figure(figsize = (15,9))  # 设置画布大小
explode = (0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.97,0.75,0.53,0.37)  # 设置每一块分割出的间隙大小
plt.pie(data,explode=explode,labels = labels,autopct = '%1.2f%%',
        pctdistance = 1.1,labeldistance = 1.2)
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.title("果蔬内部各商品的销量占比 3026")  # 设置标题
plt.axis('equal')
plt.legend()
# plt.savefig('../tmp/child_persent.png')  # 保存图形
plt.show()  # 展示图形

 

 

selected = sort_links.loc[sort_links['Types'] == '西点']  # 挑选商品类别为“西点”并排序
child_nums = selected['id'].sum()  # 对所有的“西点”求和
selected['child_percent'] = selected.apply(lambda line: line['id']/child_nums,axis = 1)  # 求百分比
selected.rename(columns = {'id':'count'},inplace = True)
print('西点内部商品的销量及其占比:\n',selected)
outfile2 = 'E:/data/child_percent.csv'
sort_link.to_csv(outfile2,index = False,header = True,encoding='gbk')  # 输出结果

# 画饼图展示西点内部各商品的销量占比
import matplotlib.pyplot as plt
data = selected['child_percent']
labels = selected['Goods']
plt.figure(figsize = (15,9))  # 设置画布大小
# explode = (0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.08,0.3,0.1,0.3)  # 设置每一块分割出的间隙大小
plt.pie(data,labels = labels,autopct = '%1.2f%%',
        pctdistance = 1.1,labeldistance = 1.2)
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.title("西点内部各商品的销量占比  3026")  # 设置标题
plt.axis('equal')
plt.legend()
# plt.savefig('../tmp/child_persent.png')  # 保存图形
plt.show()  # 展示图形

 

 

from numpy import *
 
def loadDataSet():
    return [['a', 'c', 'e'], ['b', 'd'], ['b', 'c'], ['a', 'b', 'c', 'd'], ['a', 'b'], ['b', 'c'], ['a', 'b'],
            ['a', 'b', 'c', 'e'], ['a', 'b', 'c'], ['a', 'c', 'e']]
 
def createC1(dataSet):
    C1 = []
    for transaction in dataSet:
        for item in transaction:
            if not [item] in C1:
                C1.append([item])
    C1.sort()
    # 映射为frozenset唯一性的,可使用其构造字典
    return list(map(frozenset, C1))     
    
# 从候选K项集到频繁K项集(支持度计算)
def scanD(D, Ck, minSupport):
    ssCnt = {}
    for tid in D:   # 遍历数据集
        for can in Ck:  # 遍历候选项
            if can.issubset(tid):  # 判断候选项中是否含数据集的各项
                if not can in ssCnt:
                    ssCnt[can] = 1  # 不含设为1
                else:
                    ssCnt[can] += 1  # 有则计数加1
    numItems = float(len(D))  # 数据集大小
    retList = []  # L1初始化
    supportData = {}  # 记录候选项中各个数据的支持度
    for key in ssCnt:
        support = ssCnt[key] / numItems  # 计算支持度
        if support >= minSupport:
            retList.insert(0, key)  # 满足条件加入L1中
            supportData[key] = support  
    return retList, supportData
 
def calSupport(D, Ck, min_support):
    dict_sup = {}
    for i in D:
        for j in Ck:
            if j.issubset(i):
                if not j in dict_sup:
                    dict_sup[j] = 1
                else:
                    dict_sup[j] += 1
    sumCount = float(len(D))
    supportData = {}
    relist = []
    for i in dict_sup:
        temp_sup = dict_sup[i] / sumCount
        if temp_sup >= min_support:
            relist.append(i)
# 此处可设置返回全部的支持度数据(或者频繁项集的支持度数据)
            supportData[i] = temp_sup
    return relist, supportData
 
# 改进剪枝算法
def aprioriGen(Lk, k):
    retList = []
    lenLk = len(Lk)
    for i in range(lenLk):
        for j in range(i + 1, lenLk):  # 两两组合遍历
            L1 = list(Lk[i])[:k - 2]
            L2 = list(Lk[j])[:k - 2]
            L1.sort()
            L2.sort()
            if L1 == L2:  # 前k-1项相等,则可相乘,这样可防止重复项出现
                # 进行剪枝(a1为k项集中的一个元素,b为它的所有k-1项子集)
                a = Lk[i] | Lk[j]  # a为frozenset()集合
                a1 = list(a)
                b = []
                # 遍历取出每一个元素,转换为set,依次从a1中剔除该元素,并加入到b中
                for q in range(len(a1)):
                    t = [a1[q]]
                    tt = frozenset(set(a1) - set(t))
                    b.append(tt)
                t = 0
                for w in b:
                    # 当b(即所有k-1项子集)都是Lk(频繁的)的子集,则保留,否则删除。
                    if w in Lk:
                        t += 1
                if t == len(b):
                    retList.append(b[0] | b[1])
    return retList

def apriori(dataSet, minSupport=0.2):
# 前3条语句是对计算查找单个元素中的频繁项集
    C1 = createC1(dataSet)
    D = list(map(set, dataSet))  # 使用list()转换为列表
    L1, supportData = calSupport(D, C1, minSupport)
    L = [L1]  # 加列表框,使得1项集为一个单独元素
    k = 2
    while (len(L[k - 2]) > 0):  # 是否还有候选集
        Ck = aprioriGen(L[k - 2], k)
        Lk, supK = scanD(D, Ck, minSupport)  # scan DB to get Lk
        supportData.update(supK)  # 把supk的键值对添加到supportData里
        L.append(Lk)  # L最后一个值为空集
        k += 1
    del L[-1]  # 删除最后一个空集
    return L, supportData  # L为频繁项集,为一个列表,1,2,3项集分别为一个元素

# 生成集合的所有子集
def getSubset(fromList, toList):
    for i in range(len(fromList)):
        t = [fromList[i]]
        tt = frozenset(set(fromList) - set(t))
        if not tt in toList:
            toList.append(tt)
            tt = list(tt)
            if len(tt) > 1:
                getSubset(tt, toList)
 
def calcConf(freqSet, H, supportData, ruleList, minConf=0.7):
    for conseq in H:  #遍历H中的所有项集并计算它们的可信度值
        conf = supportData[freqSet] / supportData[freqSet - conseq]  # 可信度计算,结合支持度数据
        # 提升度lift计算lift = p(a & b) / p(a)*p(b)
        lift = supportData[freqSet] / (supportData[conseq] * supportData[freqSet - conseq])
 
        if conf >= minConf and lift > 1:
            print(freqSet - conseq, '-->', conseq, '支持度', round(supportData[freqSet], 6), '置信度:', round(conf, 6),
                  'lift值为:', round(lift, 6))
            ruleList.append((freqSet - conseq, conseq, conf))
 
# 生成规则
def gen_rule(L, supportData, minConf = 0.7):
    bigRuleList = []
    for i in range(1, len(L)):  # 从二项集开始计算
        for freqSet in L[i]:  # freqSet为所有的k项集
            # 求该三项集的所有非空子集,1项集,2项集,直到k-1项集,用H1表示,为list类型,里面为frozenset类型,
            H1 = list(freqSet)
            all_subset = []
            getSubset(H1, all_subset)  # 生成所有的子集
            calcConf(freqSet, all_subset, supportData, bigRuleList, minConf)
    return bigRuleList
 
if __name__ == '__main__':
    import pandas as pd

    inputfile = 'E:/data/GoodsOrder.csv'
    data = pd.read_csv(inputfile, encoding='gbk')

    # 根据id对“Goods”列合并,并使用“,”将各商品隔开
    data['Goods'] = data['Goods'].apply(lambda x: ',' + x)
    data = data.groupby('id').sum().reset_index()

    # 对合并的商品列转换数据格式
    data['Goods'] = data['Goods'].apply(lambda x: [x[1:]])
    data_list = list(data['Goods'])

    # 分割商品名为每个元素
    data_translation = []
    for i in data_list:
        p = i[0].split(',')
        data_translation.append(p)
    print('数据转换结果的前5个元素:\n', data_translation[0:5])
    dataSet = data_translation
    L, supportData = apriori(dataSet, minSupport = 0.02)
    rule = gen_rule(L, supportData, minConf = 0.35)

 

标签:plt,selected,list,supportData,项集,Goods,data
From: https://www.cnblogs.com/20020420zeng/p/17234828.html

相关文章

  • CF724E Goods transportation
    链接:https://www.luogu.com.cn/problem/CF724E题目描述:有\(n\)个城市,每个城市生产了\(p_{i}\)个货物,最多可以卖掉\(s_{i}\)个货物。对于每两个城市\((i,j)\),如果\(i<j\),则......
  • 冲销已过账外向交货单BAPI:WS_REVERSE_GOODS_ISSUE
    前台操作:VL09填写装运点和交货单点击定义日期,将输入的实际过账日期输入到本地日期中。点勾然后点击冲销点击绿色勾,冲销成功或错误,则均会出现如果对话框。......
  • DEMO:冲销交货单过账凭证WS_REVERSE_GOODS_ISSUE
    reportzdemo_vl09.parametersp_vbelntypevbeln_vl.data:lt_likptypetableoflikp.data:ls_likplikelineoflt_likp.data:lt_mesg......
  • DEMO:MB1B 311 移库 BAPI_GOODSMVT_CREATE
    *&---------------------------------------------------------------------**&ReportZDEMO_MB1B*&*&---------------------------------------------------------------......
  • HU_CREATE_GOODS_MOVEMENT报错:对象清单抬头数据中的差异
    对于已经创建HU的物料,调用HU_CREATE_GOODS_MOVEMENT 创建凭证的时候遇到了下面的问题情景是这样:先对ct00工厂的数据进行了bapi调用commit后又对CT20工厂数据进行操作这个......
  • BAPI_GOODSMVT_CREATE 带序列号
     API_GOODSMVT_CREATE物料移动,比如MB1B'343'"unblock'344'"block参考代码*&BAPIDATA:goodsmvt_headerLIKEbapi2017_gm_head_01,goodsmvt_codeLIKE......
  • BAPI_GOODSMVT_CREATE物料凭证创建…
    'BAPI_GOODSMVT_CREATE可以实现物料凭证创建和部分冲销全部冲销可以使用BAPI_GOODSMVT_CANCELFUNCTION 'BAPI_GOODSMVT_CREATE'        EXPORTING     ......
  • 520813 - FAQ: BAPIs for goods
    SymptomThisnotecontainsfrequentlyaskedquestions/answersregarding'BAPIsforgoodsmovements'.Questions1.WherecanIfindthedocumentationforcallingth......