import numpy as np
minicolumn=12
cell=4
segment=5
threshold=0.9
thresholdcount=1
 #activation matrix initialization
a = np.zeros((minicolumn,cell ))
prediction= np.zeros((minicolumn,cell ))
permanence= np.random.rand(minicolumn,cell,segment,minicolumn,cell )
connection=np.where(permanence > threshold, 1, 0)
s=np.zeros((minicolumn,cell,segment),dtype = int)
s=s-1
import numpy as np
sample=1000
sequencelength=3
input=np.zeros((sample,sequencelength),dtype=int)
dummy=np.array([[1,2,3],[6,2,7],[9,3,4]])
for i in range (0,sample):
 random=np.random.randint(0,2)
 input[i,:]=dummy[random,:]
input

def winner(i,j,input):
 R=np.array([0])
 R[:]=input[i,j]
 return R
winner(1,2,input)

def predict(connection,permanence,minicolumn,cell,segment,threshold,thresholdcount,prediction,a):
 
 s=np.zeros((minicolumn,cell,segment))
 s=s-1
 #prediction of cell
 #reset prediciton matrix first
 prediction= np.zeros((minicolumn,cell ),dtype = int)
 for y in range(0,minicolumn):
 for u in range(0,cell):
 flag=0
 o=0
 for m in range(0,segment):
 sum=0;
 for y2 in range(0,minicolumn):
 for j2 in range(0,cell):
 if a[y2,j2]== 1 and connection[y2,j2,m,y,u]==1:
 sum=sum+1
 if sum>thresholdcount:
 flag=1
 s[y,u,o]=m
 o=o+1
 if flag==1:
 prediction[y,u]=1
 return prediction

def delearning(minicolumn,prediction,permanence,a,cell,segment,la,s):
 for j in range(0,minicolumn):
 for k in range(0,cell):
 if (prediction[j,k]==1 and a[j,k]==0):
 for e in s[j,k,:]:
 e=int(e)
 if (e>-1):
 for M in range(0,minicolumn):
 for f in range(0,cell):
 if la[M,f]==1:
 diff=permanence[M,f,e,j,k]\*0.3
 permanence[M,f,e,j,k]=permanence[M,f,e,j,k] - diff 
 return permanence 
def synaptic\_learning(permanence,connection,minicolumn,cell,segment,a,j1,W,prediction,s):
 #synaptic Learning if not first element of a sequence
 outputcount=0
 for j in W:
 count=0
 for k2 in range(0,cell):
 if prediction[j,k2]==1:
 count=count+1
 if count>0:
 #synaptic Learning of prediciton
 for k in range(0,cell):
 if (prediction[j,k]==1 and a[j,k]==1) :
 outputcount=outputcount+1
 for e in s[j,k,:]:
 e=int(e)
 if e>-1:
 for M in range(0,minicolumn):
 for f in range(0,cell):
 if la[M,f]==1:
 diff=(1-permanence[M,f,e,j,k])\*0.3
 diff1=(1-permanence[M,f,e,j,k])\*0.1
 permanence[M,f,e,j,k]=permanence[M,f,e,j,k] + diff-diff1
 else:
 diff=(1-permanence[M,f,e,j,k])\*0.3
 permanence[M,f,e,j,k]=permanence[M,f,e,j,k] - diff 
 else:
 #synaptic Learning if no prediciton choose one cell having segment is having highest value
 segcount=np.zeros(cell,dtype=int)
 seg1=np.zeros(cell,dtype=int)
 for h in range(0,cell):
 segcount[h]=0
 max=-1
 for h1 in range(0,segment):
 sum=0;
 for b in range(0,minicolumn):
 for b1 in range(0,cell):
 if connection[b,b1,h1,j,h]==1 and la[b,b1]==1:
 sum=sum+1
 if max<=sum:
 max=sum
 seg1[h]=h1
 segcount[h]=max
 
 index\_cell=np.argmax(segcount)
 for g in range(0,cell):
 if g==index\_cell:
 for b in range(0,minicolumn):
 for b1 in range(0,cell):
 segmentforlearning=seg1[index\_cell]
 if la[b,b1]==1:
 diff=(1-permanence[b,b1,segmentforlearning,j,g])\*0.6
 diff1=(1-permanence[b,b1,segmentforlearning,j,g])\*0.001
 permanence[b,b1,segmentforlearning,j,g]=permanence[b,b1,segmentforlearning,j,g] + diff-diff1
 else:
 diff=permanence[b,b1,segmentforlearning,j,g]\*0.6
 permanence[b,b1,segmentforlearning,j,g]=permanence[b,b1,segmentforlearning,j,g] - diff 
 else:
 for b in range(0,minicolumn):
 for b1 in range(0,cell):
 if la[b,b1]==1:
 for g1 in range(0,segment):
 diff=permanence[b,b1,g1,j,g]\*0.6
 permanence[b,b1,g1,j,g]=permanence[b,b1,g1,j,g] - diff
 
 return [permanence,outputcount] 
def activation (j1,connection,minicolumn,cell,segment,a,W,prediction):
 if j1==0:
 for j in W:
 a[j,:]=1
 else:
 for j in W:
 count=0
 for k in range(0,cell):
 if prediction[j,k]==1:
 a[j,k]=1
 count=count+1
 if count==0:
 a[j,:]=1
 return prediction
output1=np.zeros(1000)
(l,l2)=input.shape
for i in range(0,l):
 a = np.zeros((minicolumn,cell ))
 for j1 in range(0,l2):
 W=winner(i,j1,input)
 la=a
 a = np.zeros((minicolumn,cell ))
 activation(j1,connection,minicolumn,cell,segment,a,W,prediction)
 if j1!=0:
 [permanence,output1[i]]=synaptic\_learning(permanence,connection,minicolumn,cell,segment,a,j1,W,prediction,s)
 permanence=delearning(minicolumn,prediction,permanence,a,cell,segment,la,s)
 connection=np.where(permanence > threshold, 1, 0)
 prediction=np.zeros((minicolumn,cell ))
 prediction=predict(connection,permanence,minicolumn,cell,segment,threshold,thresholdcount,prediction,a)
 
 else:
 prediction=np.zeros((minicolumn,cell ))
 prediction=predict(connection,permanence,minicolumn,cell,segment,threshold,thresholdcount,prediction,a)
import matplotlib.pyplot as plt

p=np.zeros(1000,dtype=int)
for i in range(1,999):
 p[i]=i
plt.plot(p,output1)
plt.xlabel('x-values')
plt.ylabel('x^2-values')

plt.show()
plt.show()

negativecount=0
thresholdcount=1
totalcount=0
correct=0
(t1,t2)=input.shape
output=np.zeros((t2))
recall=np.zeros((t1))
output=output-1
correct=0
sequence\_prediction=0
for i in range(0,t1):
 classification=0
 for j in range(0,t2):
 if j ==0:
 W= winner(i,j,input)
 for k in W:
 a[k,:]=1
 else:
 
 prediction =np.zeros((minicolumn,cell))
 
#prediciton
 
 for k1 in range(0,minicolumn):
 flag=0
 for k2 in range(0,cell):
 o=0
 for k3 in range (0,segment):
 count=0
 for k in range (0,minicolumn):
 for k4 in range(0,cell):
 if connection[k,k4,k3,k1,k2]==1 and a[k,k4]==1:
 count=count+1
 if count>thresholdcount:
 prediction[k1,k2]=1
 a=np.zeros((minicolumn,cell))
 #check for correct predicted column
 bigflag=0
 for k1 in range(0,minicolumn):
 flag=0
 for k2 in range(0,cell):
 if prediction[k1,k2]==1:
 flag=1
 if flag==1:
 print(k1)
 if flag==1 and input[i,j+1]==k1:
 correct=correct+1
 
 bigflag=1
 for k2 in range(0,cell):
 if prediction[k1,k2]==1:
 a[k1,k2]=1
 
 #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* 
 if bigflag==0:
 negativecount=negativecount+(t2-(j+2))
 break 
 
precision1=correct/(t1\*(t2-1))
 

negativecount

precision1=correct/((t1\*(t2-1))-negativecount)
precision1

give a long descriptive explantion for above code
numpy를 np로 가져옵니다.minicolumn=12cell=4segment=5threshold=0.9임계값 카운트=1 #활성화 행렬 초기화a = np.0((미니컬럼,셀 ))예측 = np.0((미니컬럼,셀 ))영속성= np.random.rand(미니컬럼,셀,세그먼트,미니컬럼,셀 )connection=np.where(영구성 > 임계값, 1, 0)s=np.0((미니컬럼,셀,세그먼트),dtype = int)s=s-1numpy를 np로 가져옵니다.sample=1000시퀀스 길이=3입력=np.0((샘플,시퀀스길이),dtype=int)dummy=np.array([[1,2,3],[6,2,7],[9,3,4]])범위 (0,샘플)의 i에 대해: random=np.random.randint(0,2) input[i,:]=dummy[random,:]inputdef winner(i,j,input): R=np.array([0]) R[:]=input[i,j] 반환 Rwinner(1,2,input)def predict(connection,permanence,minicolumn,cell,segment,threshold,thresholdcount,prediction,a):  s=np.0((미니컬럼,셀,세그먼트)) s=s-1 #셀 예측 #예측 행렬 먼저 재설정 prediction= np.zeros((미니컬럼,셀 ),dtype = int) for y in range(0,minicolumn): FOR U IN RANGE(0,CELL): flag=0 o=0 for m in range(0,segment): sum=0; FOR Y2 IN RANGE(0,MINICOLUMN): FOR J2 IN RANGE(0,CELL): if a[y2,j2]== 1 and connection[y2,j2,m,y,u]==1: sum=sum+1 합계가 임계값 수보다 크면 flag=1 s[y,u,o]=m o=o+1 if flag==1: prediction[y,u]=1 예측 반환def delearning(minicolumn,prediction,permanence,a,cell,segment,la,s): for j in range(0,minicolumn): for k in range(0,cell): if (prediction[j,k]==1 and a[j,k]==0): for e in s[j,k,:]: e=int(e) if (e>-1): for M in range(0,minicolumn): for f in range(0,cell): if la[M,f]==1: diff=permanence[M,f,e,j,k]\*0.3 permanence[M,f,e,j,k]=permanence[M,f,e,j,k] - diff  영구성 반환 def synaptic\_learning(영구성,연결,미니컬럼,셀,세그먼트,a,j1,W,예측,s): 시퀀스의 첫 번째 요소가 아닌 경우 #시냅스 학습 outputcount=0 W의 j에 대해 count=0 for k2 in range(0,cell): if prediction[j,k2]==1: count=count+1 count>0이면 예측의 #시냅스 학습 범위(0,셀) 내 k에 대해: if (prediction[j,k]==1 and a[j,k]==1) : outputcount=outputcount+1 for e in s[j,k,:]: e=int(e) if e>-1: for M in range(0,minicolumn): for f in range(0,cell): if la[M,f]==1: diff=(1-permanence[M,f,e,j,k])\*0.3 diff1=(1-permanence[M,f,e,j,k])\*0.1 permanence[M,f,e,j,k]=permanence[M,f,e,j,k] + diff-diff1 else: diff=(1-permanence[M,f,e,j,k])\*0.3 permanence[M,f,e,j,k]=permanence[M,f,e,j,k] - diff  else: 전제 조건이 없는 경우 #시냅스 학습 세그먼트가 가장 높은 값을 갖는 셀 하나를 선택합니다. segcount=np.0(cell,dtype=int) seg1=np.0(cell,dtype=int) 범위(0,셀)의 h에 대해: segcount[h]=0 max=-1 for h1 in range(0,segment): sum=0; for b in range(0,minicolumn): FOR B1 IN RANGE(0,CELL): if connection[b,b1,h1,j,h]==1 and la[b,b1]==1: 합계=합계+1 if max<=sum: max=sum seg1[h]=h1 segcount[h]=최대  index\_cell=np.argmax(segcount) 범위(0,셀)의 g에 대해: IF G==INDEX\_CELL: for b in range(0,minicolumn): FOR B1 IN RANGE(0,CELL): SEGMENTFORLEARNING=SEG1[INDEX\_CELL] if la[b,b1]==1: diff=(1-permanence[b,b1,segmentforlearning,j,g])\*0.6 diff1=(1-permanence[b,b1,segmentforlearning,j,g])\*0.001 permanence[b,b1,segmentforlearning,j,g]=permanence[b,b1,segmentforlearning,j,g] + diff-diff1 else: diff=permanence[b,b1,segmentforlearning,j,g]\*0.6 permanence[b,b1,segmentforlearning,j,g]=permanence[b,b1,segmentforlearning,j,g] - diff  else: FOR B IN RANGE(0,MINICOLUMN): FOR B1 IN RANGE(0,CELL): IF LA[B,B1]==1: FOR G1 IN RANGE(0,SEGMENT): diff=permanence[b,b1,g1,j,g]\*0.6 permanence[b,b1,g1,j,g]=permanence[b,b1,g1,j,g] - diff  반환 [영구성,출력 횟수] def 활성화 (j1,연결,미니컬럼,셀,세그먼트,a,W,예측): if j1==0: for j in W: a[j,:]=1 else: for j in W: count=0 for k in range(0,cell): if prediction[j,k]==1: a[j,k]=1 count=count+1 if count==0: a[j,:]=1 예측 반환output1=np.0(1000)(l,l2)=input.shapefor i in range(0,l): a = np.0((미니컬럼,셀 )) for j1 in range(0,l2): W=winner(i,j1,input) la=a a = np.0((미니컬럼,셀 )) 활성화(j1,연결,미니컬럼,셀,세그먼트,a,W,예측) if j1!=0: [영구성,출력1[i]]=시냅스\_학습(영구성,연결,미니컬럼,셀,세그먼트,a,j1,W,예측,s) 영구성=디러닝(미니컬럼,예측,영구성,a,셀,세그먼트,라,s) 연결=np.where(영속성> 임계값, 1, 0) prediction=np.0((미니컬럼,셀 )) prediction=predict(connection,permanence,minicolumn,cell,segment,threshold,thresholdcount,prediction,a)  else: prediction=np.0((미니컬럼,셀 )) prediction=predict(connection,permanence,minicolumn,cell,segment,threshold,thresholdcount,prediction,a)matplotlib.pyplot을 plt로 가져옵니다.p=np.zeros(1000,dtype=int)for i in range(1,999): p[i]=iplt.plot(p,output1)plt.xlabel('x-values')plt.ylabel('x^2-값')plt.show()plt.show()음수 카운트=0임계값 카운트=1totalcount=0correct=0(t1,t2)=입력.모양OUTPUT=NP.ZEROS((T2))RECALL=NP.ZEROS((T1))출력=출력-1correct=0시퀀스\_예측=0FOR I IN RANGE(0