## Aplicación del problema de identificación de la red neuronal BP (1) -TC

``````import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
c1 = input('请选择T或者C：')
if c1 == 'T':
x1 = [4, 4, 4, 5, 6]
y1 = [1, 2, 3, 2, 2]
else:
x1 = [1, 1, 1, 2, 2]
y1 = [0, 1, 2, 0, 2]
ax = plt.axes([0.025, 0.025, 0.95, 0.95]) #[xmin,ymin,xmax,ymax]
ax.set_xlim(0, 9)
ax.set_ylim(0, 9)
ax.xaxis.set_major_locator(plt.MultipleLocator(9))#设置x主坐标间隔 1
ax.xaxis.set_minor_locator(plt.MultipleLocator(1))#设置x从坐标间隔 0.1
ax.yaxis.set_major_locator(plt.MultipleLocator(9))#设置y主坐标间隔 1
ax.yaxis.set_minor_locator(plt.MultipleLocator(1))#设置y从坐标间隔 0.1
ax.grid(which='major', axis='x', linewidth=1, linestyle='-', color='0')#由每个x主坐标出发对x主坐标画垂直于x轴的线段
ax.grid(which='minor', axis='x', linewidth=1, linestyle='-', color='0')#由每个x主坐标出发对x主坐标画垂直于x轴的线段
ax.grid(which='major', axis='y', linewidth=1, linestyle='-', color='0')
ax.grid(which='minor', axis='y', linewidth=1, linestyle='-', color='0')
ax.set_xticklabels([])#标记x轴主坐标的值,在这里设为空值，则表示坐标无数值标定；
for i in range(len(x1)):
ax.set_yticklabels([])
plt.show()
chioce  = input('请输入选择器类型：A或者B：')
if chioce == 'A':
X = np.zeros((11, 9))
for i in range(len(x1)):
X[x1[i], y1[i]] = 1
Z = np.zeros((11, 9))
w = np.array([[-1, 1, -1],
[-1, 1, -1],
[-1, 1, -1]])
for i in range(9):
for j in range(7):
T = X[i:i+3, j:j+3]
s = 0
for m in range(3):
for n in range(3):
s = s + w[m, n]*T[m, n]
Z[i, j] = s
for i in range(9):
for j in range(7):
if Z[i, j] > 1.5:
Z[i, j] = 1
else:
Z[i, j] = 0
if np.sum(Z)-0.5 > 0:
print('输入的是T')
else:
print('输入的是C')
else:
X = np.zeros((11, 9))
for i in range(len(x1)):
X[x1[i], y1[i]] = 1
Z = np.zeros((9, 7))
w = np.array([[-1, -1, -1],
[-1, 2, -1],
[-1, -1, -1]])
for i in range(9):
for j in range(7):
T = X[i:i+3, j:j+3]
s = 0
for m in range(3):
for n in range(3):
s = s + w[m, n]*T[m, n]
Z[i, j] = s
for i in range(9):
for j in range(7):
if Z[i, j] > 0.5:
Z[i, j] = 1
else:
Z[i, j] = 0
if np.sum(Z)-0.5 > 0:
print('输入的是T')
else:
print('输入的是C')

``````

### Supongo que te gusta

Origin blog.csdn.net/weixin_40653652/article/details/112612867