抽象整活儿
Gitea Actions Test / Challenge-Actions (push) Successful in 25s

This commit is contained in:
2024-11-17 04:19:56 +08:00
parent 95ee0ec0e1
commit 020928ac9a
+2 -13
View File
@@ -41,17 +41,7 @@ def train_svm(X, y, C=0.20, max_iter=1000, learning_rate=0.01):
Returns: Returns:
- numpy array: 训练得到的权重。 - numpy array: 训练得到的权重。
""" """
m, n = X.shape return None
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)
def predict_svm(inX, weight): def predict_svm(inX, weight):
@@ -65,8 +55,7 @@ def predict_svm(inX, weight):
Returns: Returns:
- int: 预测的分类标签。 - int: 预测的分类标签。
""" """
result = np.dot(inX, weight[0]) + weight[1] return 1
return 1 if result > 0 else -1
# 这里也写自己的代码实现 # 这里也写自己的代码实现