generated from LufsX/Challenge-SVM
+2
-13
@@ -41,17 +41,7 @@ def train_svm(X, y, C=0.20, max_iter=1000, learning_rate=0.01):
|
||||
Returns:
|
||||
- numpy array: 训练得到的权重。
|
||||
"""
|
||||
m, n = X.shape
|
||||
w = np.zeros(n)
|
||||
b = 0
|
||||
for i in range(max_iter):
|
||||
for j in range(m):
|
||||
if y[j] * (np.dot(X[j], w) + b) < 1:
|
||||
w = w - learning_rate * (2 * w - C * y[j] * X[j])
|
||||
b = b + learning_rate * C * y[j]
|
||||
else:
|
||||
w = w - learning_rate * 2 * w
|
||||
return (w, b)
|
||||
return None
|
||||
|
||||
|
||||
def predict_svm(inX, weight):
|
||||
@@ -65,8 +55,7 @@ def predict_svm(inX, weight):
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
result = np.dot(inX, weight[0]) + weight[1]
|
||||
return 1 if result > 0 else -1
|
||||
return 1
|
||||
|
||||
|
||||
# 这里也写自己的代码实现
|
||||
|
||||
Reference in New Issue
Block a user