# 多層感知器（二）

``````print(p1.coef_, p1.intercept_)  # [[-60. 107.]] [0.]
print(p2.coef_, p2.intercept_)  # [[ 321. -800.]] [-1.]
print(p3.coef_, p3.intercept_)  # [[2. 2.]] [-1.]
``````

``````import numpy as np
import matplotlib.pyplot as plt

def a(x):
return 1 if x > 0 else 0

def p1_predict(height, waist):
w1 = np.array([-60, 107])
b1 = 0
return a(np.dot(w1, [height, waist]) + b1)

def p2_predict(height, waist):
w2 = np.array([321, -800])
b2 = -1
return a(np.dot(w2, [height, waist]) + b2)

height = 165
waist = 85

print(p1_predict(height, waist)) # 顯示 0
print(p2_predict(height, waist)) # 顯示 0
``````

``````def p3_predict(p1_o, p2_o):
w3 = np.array([2, 2])
b3 = -1
return a(np.dot(w3, [p1_o, p2_o]) + b3)

# 顯示 0
print(
p3_predict(
p1_predict(height, waist),
p2_predict(height, waist)
)
)
``````

``````import numpy as np
import matplotlib.pyplot as plt

def a(x):
return np.where(x > 0, 1, 0)

def p1p2_predict(height, waist):
# 建立矩陣
w = np.array([
[-60, 107],
[321, -800]
])
b = np.array([0, -1])
# @ 是矩陣相乘
return a(w @ [height, waist] + b)

def p3_predict(p1p2_o):
w3 = np.array([2, 2])
b3 = -1
return a(np.dot(w3, p1p2_o) + b3)

height = 165
waist = 85

print(
p3_predict(
p1p2_predict(height, waist)
)
)
``````