用python代码难以输入训练数据集

时间:2018-09-25 11:50:13

标签: python-3.x matlab machine-learning

请,我需要帮助。尝试了几乎所有我知道的东西,但没有任何效果。 因此,我正在对树状细胞算法进行性能评估,该算法用于通过接受输入信号(PAMP,危险信号,安全信号)来检测异常,该项目基于使用参数调整进行此分析。更改算法参数并评估每个结果。

从“聪明的算法”一书中获得算法的红宝石代码,并将其转换为python。

我的问题是我发现在python代码上输入数据集很困难,我正在使用Nsl-kdd'cup数据集。

此外,将非常感谢您将其与Matlab配合使用。

请,我需要帮助。 python代码

from random import randrange,random,randint
def rand_in_bounds(min,max):
    return randrange(min, max)

# def random_vector(search_space): unused function
#   arr=np.zeroes(5)
#   return arr

def construct_pattern(class_label,domain,p_safe,p_danger):
    set_=domain[class_label]
    selection=randrange(len(set_))
    pattern={}
    pattern["class_label"]=class_label
    pattern["input_"]=set_[selection]
    pattern["safe"]=(random()*p_safe*100)
    pattern["danger"]=(random()*p_danger*100)
    return pattern

def generate_pattern(domain,p_anomaly,p_normal,prob_create_anom=0.5):
    pattern=None
    if random() < prob_create_anom:
        pattern=construct_pattern("Anomaly",domain,1-p_normal,p_anomaly)
        print(">Generated Anomaly",pattern["input_"])
    else:
        pattern=construct_pattern("Normal",domain,p_normal,1-p_anomaly)
    return pattern

def initialize_cell(thresh):
    cell={}
    cell["lifespan"]=1000
    cell["k"]=0
    cell["cms"]=0
    cell["migration_threshold"]=thresh[0]+((thresh[1]-thresh[0])*random())
    cell["antigen"]={}
   # print(cell)
    return cell

def store_antigen(cell,input_):
    cell["antigen"].setdefault(input_,0)
    cell["antigen"][input_]+=1


def expose_cell(cell,cms,k,pattern,threshold):
    cell["cms"]+=cms
    cell["k"]+=k
    cell["lifespan"]-=cms
    store_antigen(cell,pattern["input_"])
    if cell["lifespan"]<=0 : cell= initialize_cell(threshold)

def can_cell_migrate(cell):
    return (cell["cms"]>=cell["migration_threshold"]) and 
(len((cell["antigen"]))!=0)

def expose_all_cells(cells,pattern,threshold):
    migrate=[]
    cms=(pattern["safe"]+pattern["danger"])
    k=pattern["danger"]-(pattern["safe"]*2)
    for cell in cells:
        expose_cell(cell,cms,k,pattern,threshold)
        if can_cell_migrate(cell):
            cell["class_label"]=("Anomaly" if cell["k"]>0 else "Normal")
            migrate.append(cell)
    print("________________________")
    return migrate

def train_system(domain,max_iter,num_cells,p_anomaly,p_normal,thresh):
    immature_cells=[] 
    immature_cells=[initialize_cell(thresh) for x in range(num_cells)]
    migrated=[]
    c=0
    for x in range(max_iter):
        pattern=generate_pattern(domain,p_anomaly,p_normal)
        migrants=expose_all_cells(immature_cells,pattern,thresh)
        for cell in migrants:
            immature_cells=[ x for x in immature_cells if x is not cell]
            immature_cells.append(initialize_cell(thresh)) 
            migrated.append(cell) 

        c+=1
        print(f'> iter= {c}  new={len(migrants)}  migrated={len(migrated)}')  

    return migrated

def classify_pattern(migrated,pattern):
    input_=pattern["input_"]
    num_cells,num_antigen=0,0
    for cell in migrated:
        if ((cell["class_label"]=="Anomaly") and (input_ in cell["antigen"])):
            num_cells+=1
            num_antigen+=cell["antigen"][input_]

    if num_antigen==0:
        return "Normal"

    mcav=num_cells/num_antigen
    return "Anomaly" if mcav>0.5 else "Normal"

def test_system(migrated,domain,p_anomaly,p_normal,num_trial=100):
    correct_norm=0
    for _ in range(num_trial):
        pattern=construct_pattern("Normal",domain,p_normal,1-p_anomaly)
        class_label=classify_pattern(migrated,pattern)
        correct_norm += 1 if class_label=="Normal" else 0
    print(f"Finished testing Normal inputs {correct_norm}/{num_trial}")
    correct_anom=0
    for _ in range(num_trial):
        pattern=construct_pattern("Anomaly",domain,1-p_normal,p_anomaly)
        class_label=classify_pattern(migrated,pattern)
        correct_anom += 1 if class_label=="Anomaly" else 0
    print(f"Finished testing Anomaly inputs {correct_anom}/{num_trial}")
    return [correct_norm,correct_anom]

def execute(domain,max_iter,num_cells,p_anom,p_norm,thresh):
    migrated=train_system(domain,max_iter,num_cells,p_anom,p_norm,thresh)
    test_system(migrated,domain,p_anom,p_norm)
    return migrated

if __name__ =="__main__":
    # problem configuration
    domain = {}
    domain["Normal"]=[x for x in range(1,51)]
    domain["Anomaly"]=[x*10 for x in range(1,6)]
    domain["Normal"]=[x for x in domain["Normal"] if x not in domain["Anomaly"]]
    p_anomaly=0.70
    p_normal=0.95

    #algorithm configuration
    iterations=100
    num_cells=10
    thresh=[5,15]
    execute(domain,iterations,num_cells,p_anomaly,p_normal,thresh)

0 个答案:

没有答案