资源简介

K-Anonymity要求对于任意一行纪录,其所属的相等集内纪录数量不小于k,即至少有k-1条纪录半标识列属性值与该条纪录相同。

资源截图

代码片段和文件信息

from time import sleep

from model import *
import os
data=[] #数据元组
attributes = (‘job‘ ‘sex‘ ‘age‘ ‘disease‘)  #数据的所有属性值
QID = (012) #要进行匿名的属性值
Anonymity_data=[]
Trees = GetTrees()   #保存属性的树
Th = 0.4   #阈值:单个属性的信息损失量


def Init():
    ‘‘‘
    初始化,生成树设置阈值,初始化要匿名的属性值
    :return: 所有数据元祖,阈值,所有属性的树
    ‘‘‘

    #第一步:读取数据,生成元组
    path = os.path.abspath(‘..‘)  # 表示当前所处的文件夹上一级文件夹
    data_path = path + ‘\\data\\data.txt‘
    data_file = open(data_path ‘r‘)
    lines = data_file.readlines()
    for line in lines:
        i = line[:-1].split(‘‘)
        data.append(i)



def Anonymity():
    Init()
    PayOffs=1
    Loss = 0
    length=len(data)
    for i in range(length):
        for num in QID:
            Loss = GetLoss(Trees[num]data[i][num])
            PayOff= GetPayOff(Trees[num]data[i][num])
            while Loss                tmp_attribute=climb(Trees[num]data[i][num])
                PayOff = GetPayOff(Trees[num]tmp_attribute)    #根据属性匿名程度不断更新PayOff
                data[i][num]=tmp_attribute  #更新匿名属性
                Loss=GetLoss(Trees[num]data[i][num])   #根据匿名程度更新属性
            PayOffs=PayOff*PayOffs  #更新PayOff
            data[i].append(PayOffs)
            PayOffs=1
    tmp_data=data[:]
    while tmp_data: #执行到原始数据库中没有数据为止
        EQ=tmp_data[0]
        EQ.append(1)    #计数
        tmp_data.remove(tmp_data[0])
        Anonymity_data.append(EQ)
        for i in range(len(tmp_data)):
            each=tmp_data[i]
            if Equal(EQ[:-1] each QID):
                Anonymity_data.append(each)
                EQ[-1] = EQ[-1] + 1  # 更新K值
        tmp_data=Remove(tmp_dataEQ[:-1]QID)

    return Anonymity_data

if __name__ == ‘__main__‘:
    ‘‘‘
      list=[123442]
      list.remove(4)
      print(list)‘‘‘

    Anonymity_data=Anonymity()
    Save2File(Anonymity_data)






 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-10-05 13:33  K-Anonymity-master\
     文件          66  2018-10-05 13:33  K-Anonymity-master\.gitattributes
     文件        1203  2018-10-05 13:33  K-Anonymity-master\.gitignore
     目录           0  2018-10-05 13:33  K-Anonymity-master\.idea\
     文件         606  2018-10-05 13:33  K-Anonymity-master\.idea\boyi.iml
     文件         212  2018-10-05 13:33  K-Anonymity-master\.idea\misc.xml
     文件         260  2018-10-05 13:33  K-Anonymity-master\.idea\modules.xml
     文件         180  2018-10-05 13:33  K-Anonymity-master\.idea\vcs.xml
     文件       47910  2018-10-05 13:33  K-Anonymity-master\.idea\workspace.xml
     文件          14  2018-10-05 13:33  K-Anonymity-master\README.md
     目录           0  2018-10-05 13:33  K-Anonymity-master\data\
     文件         446  2018-10-05 13:33  K-Anonymity-master\data\Anonymity.txt
     文件          12  2018-10-05 13:33  K-Anonymity-master\data\attribute
     文件          48  2018-10-05 13:33  K-Anonymity-master\data\attribute_age
     文件          88  2018-10-05 13:33  K-Anonymity-master\data\attribute_job
     文件          19  2018-10-05 13:33  K-Anonymity-master\data\attribute_sex
     文件         145  2018-10-05 13:33  K-Anonymity-master\data\data.txt
     目录           0  2018-10-05 13:33  K-Anonymity-master\src\
     文件        2145  2018-10-05 13:33  K-Anonymity-master\src\K_Anonymity.py
     文件           0  2018-10-05 13:33  K-Anonymity-master\src\__init__.py
     文件        6190  2018-10-05 13:33  K-Anonymity-master\src\model.py
     文件         735  2018-10-05 13:33  K-Anonymity-master\src\tree.py
     目录           0  2018-10-05 13:33  K-Anonymity-master\test\
     目录           0  2018-10-05 13:33  K-Anonymity-master\test\Disease\
     目录           0  2018-10-05 13:33  K-Anonymity-master\test\Disease\data\
     文件      118122  2018-10-05 13:33  K-Anonymity-master\test\Disease\data\Anonymity.txt
     文件      118184  2018-10-05 13:33  K-Anonymity-master\test\Disease\data\Anonymity_age.txt
     文件      111352  2018-10-05 13:33  K-Anonymity-master\test\Disease\data\Anonymity_married.txt
     文件      118176  2018-10-05 13:33  K-Anonymity-master\test\Disease\data\Anonymity_sex.txt
     文件      117991  2018-10-05 13:33  K-Anonymity-master\test\Disease\data\Anonymity_weight.txt
     文件      118161  2018-10-05 13:33  K-Anonymity-master\test\Disease\data\Anonymity_workcalss.txt
............此处省略36个文件信息

评论

共有 条评论