Untitled
unknown
python
a month ago
2.3 kB
2
Indexable
Never
1) import matplotlib.pyplot as pyplot x1=[2,2,0,-2,-2,0,4] x2=[1,2,6,10,0,0,-20] d=[1,1,1,-1,-1,-1,-1] w=[0,0,0] for i in range(1000): mod=False print("-------i=",i,"-------") for j in range(7): s=w[0]+x1[j]*w[1]+x2[j]*w[2] if s>0: y=1 else: y=-1 print(f"y={y} | d={d[j]}") if y!=d[j]: w[0]+=d[j] w[1]+=d[j]*x1[j] w[2]+=d[j]*x2[j] mod=True if mod==False: break a=-w[1]/w[2] b=-w[0]/w[2] y=[] for x in x1: y.append(a*x+b) pyplot.plot(x1,x2,'o') pyplot.plot(x1,y) pyplot.show() 2) import math import matplotlib.pyplot as pyplot x1=[1,1,1,-1,1,1,-1,1,1,-1,1,1,-1] x2=[1,-1,1,-1,-1,-1,-1,1,1,-1,1,1,-1] x=[x1,x2] w=[0,0,0,0,0,0,0,0,0,0,0,0,0] d=[1,-1] b=0.2 n=0.7 error=0.05 errMod=[] counter=0 mod=False while not mod: mod=False print("-------i=",counter,"-------") esum=0 for i in range(len(x)): s=0 for j in range(len(w)): s+=x[i][j]*w[j] y=(1-math.exp(-b*s))/(1+math.exp(-b*s)) print(f"y={y} | d={d[i]}") w[0]+=n*d[i] for k in range(1,len(w)): w[k]+=n*d[i]*x[i][k-1] e=(1/2)*pow(d[i]-y,2) esum+=e counter+=1 if esum<error: mod=True errMod.append(esum) pyplot.plot(errMod,'o') pyplot.show() 3) import math import matplotlib.pyplot as pyplot x1=[1,1,1,-1,1,1,-1,1,1,-1,1,1,-1] x2=[1,-1,1,-1,-1,-1,-1,1,1,-1,1,1,-1] x3=[1,1,1,-1,1,1,-1,1,-1,-1,1,1,-1] x4=[1,-1,-1,-1,-1,-1,-1,1,1,-1,1,1,-1] x=[x1,x2] xx=[x3,x4] w=[0,0,0,0,0,0,0,0,0,0,0,0,0] d=[1,0] b=0.2 n=0.7 error=0.05 errMod=[] counter=0 mod=False while not mod: mod=False print("-------i=",counter,"-------") esum=0 for i in range(len(x)): s=0 for j in range(len(w)): s+=x[i][j]*w[j] y=1/(1+math.exp(-b*s)) print(f"y={y} | d={d[i]}") for k in range(len(w)): w[k]+=n*(d[i]-y)*(b*y*(1-y))*x[i][k] e=(1/2)*pow(d[i]-y,2) esum+=e counter+=1 if esum<error: mod=True errMod.append(esum) pyplot.plot(errMod,'o') pyplot.show() print("Klasyfikacja zaszumionej 1 oraz 4") for l in range(2): s=0 for m in range(13): s+=x[l][m]+w[m] y=1/(1+math.exp(-b*s)) print(f"y={y}")