題目:程式設計實現對率回歸,並給出西瓜資料集。
由於給定的資料集只有17個資料,所以決定直接使用乙個單層的神經網路。
注:對數機率函式y=1
1+e−
(wtx
+b
)y=\frac}
y=1+e−
(wtx
+b)1
#!/usr/bin/env python
# coding: utf-8
# in[80]:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
# in[81]:
data = np.array([[0.697, 0.460, 1],
[0.774, 0.376, 1],
[0.634, 0.264, 1],
[0.608, 0.318, 1],
[0.556, 0.215, 1],
[0.403, 0.237, 1],
[0.481, 0.149, 1],
[0.437, 0.211, 1],
[0.666, 0.091, 0],
[0.243, 0.267, 0],
[0.245, 0.057, 0],
[0.343, 0.099, 0],
[0.639, 0.161, 0],
[0.657, 0.198, 0],
[0.360, 0.370, 0],
[0.593, 0.042, 0],
[0.719, 0.103, 0]])
# in[82]:
x = data[:, 0:2]
y = data[:, 2]
# in[83]:
train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.25, random_state=30)
# in[84]:
train_x = train_x.t
test_x = test_x.t
train_y = train_y.reshape(1, -1)
test_y = test_y.reshape(1, -1)
# in[85]:
# 初始化引數
def initialize_parameters():
w = np.zeros((1, 2))
b = 0
return w, b
# in[86]:
def sigmoid(z):
a = 1 / (1 + np.exp(-z))
return a
# in[87]:
# 前向傳播
def forward_propagation(w, b, x):
z = np.dot(w, x) + b
a = sigmoid(z)
return a
# in[88]:
# 損失函式
def compute_cost(a, y):
m = a.shape[1]
cost = -1 / m * np.sum(y * np.log(a) + (1 - y) * np.log(1- a))
return cost
# in[89]:
# 後向傳播
def backward_propagation(x, a, y):
m = a.shape[1]
dw = 1 / m * np.dot(a - y, x.t)
db = 1 / m * np.sum(a - y, axis=1, keepdims=true)
return dw, db
# in[90]:
# 更新引數
def update_parameters(dw, db, w, b, learning_rate=0.001):
w = w - learning_rate * dw
b = b - learning_rate * db
return w, b
# in[128]:
def model(train_x, train_y, learning_rate=0.05, num_iterations=3000, print_cost=false):
costs =
w, b = initialize_parameters()
for i in range(num_iterations):
a = forward_propagation(w, b, train_x)
cost = compute_cost(a, train_y)
dw, db = backward_propagation(train_x, a, train_y)
w, b = update_parameters(dw, db, w, b, learning_rate)
if i % 100 == 0:
if print_cost:
print("after {} iterations, the cost is {}".format(i, cost))
parameters =
plt.plot(costs)
plt.xlabel("iterations")
plt.ylabel("cost")
plt.title("learning_rate=0.05")
return parameters
# in[129]:
parameters = model(train_x, train_y, learning_rate=0.05, num_iterations=3000, print_cost=true)
# in[130]:
# 計算正確率
def score(parameters, x, y):
w = parameters["w"]
b = parameters["b"]
a = sigmoid(np.dot(w, x) + b)
y_predict = a
m = a.shape[1]
for i in range(m):
if a[0, i] <= 0.5:
y_predict[0, i] = 0
else:
y_predict[0, i] = 1
score = 1 - np.sum(abs(y_predict - y)) / m
print(score)
# in[131]:
score(parameters, train_x, train_y)
# in[132]:
score(parameters, test_x, test_y)
機器學習 周志華 第三章 線性模型
線性模型檢視學的乙個通過屬性的線性組合來進行 的函式。如公式3 1所示。外鏈轉存失敗 img znxwdron 1564968507771 3d w 1x 1 w 2x 2 w dx d b 它可以用向量形式改寫,如公式3 2所示。外鏈轉存失敗 img kiy4xxqu 1564968472267 ...
第三章 習題三
需求規格說明書在軟體開發中具有重要的作用,它也可以作為軟體可行性分析的依據.f 需求分析的主要目的是解決軟體開發的具體方案.f 需求規格說明書描述了系統每個功能的實現.f 非功能需求是從各個角度對系統的約束和限制,反映了應用對軟體系統質量和特性的額外要求.t 需求評審人員主要由開發人員組成,一般不包...
第三章 習題3 6
include using namespace std 使用結構才能儲存多類資料 struct biaoyuan biaoyuan biao 100 10 int main 標記數字 int p 1 for int i 0 i r i 採用標記模式,如果ji 1就開始輸出,ji 0停止輸出,ji遇到...