Пожалуйста, мне нужна помощь с этим.Перепробовал практически все что знаю и ничего не работает.Поэтому я работаю над оценкой производительности алгоритма дендритных ячеек, используемого для обнаружения аномалий путем приема входных сигналов (PAMP, сигналы опасности, безопасные сигналы), в основе проекта лежит использование настройки параметров для проведения этого анализа.Изменение параметров алгоритма и оценка каждого результата.
Получил код рубина алгоритма из книги "Умные алгоритмы" и преобразовал его в python.
Моя проблема заключается в том, что мне трудно вводить свой коднабор данных в коде Python, я использую набор данных Nsl-kdd'cup.
Кроме того, помощь по использованию этого с Matlab будет высоко ценится.
Пожалуйста, мне нужна помощь.Код питона
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)