generated from LufsX/Challenge-SVM
This commit is contained in:
+49
-19
@@ -1,4 +1,3 @@
|
||||
# function.py
|
||||
import numpy as np
|
||||
|
||||
"""
|
||||
@@ -23,25 +22,41 @@ import numpy as np
|
||||
姓名请直接**填写中文**
|
||||
例如: name = "张三"
|
||||
"""
|
||||
id = "202210423xxx"
|
||||
name = "test_name"
|
||||
id = "202210423117"
|
||||
name = "程悦"
|
||||
|
||||
|
||||
# 这里写自己的代码实现
|
||||
def train_svm(X, y, other_params="你自己设置一些其它参数"):
|
||||
# 支持向量机训练函数
|
||||
def train_svm(X, y, learning_rate=0.001, iterations=1000):
|
||||
"""
|
||||
支持向量机训练函数。
|
||||
|
||||
Args:
|
||||
- X (numpy array): 特征数据。
|
||||
- y (numpy array): 标签数据。
|
||||
- learning_rate (float): 学习率,用于控制每次迭代更新的步长。
|
||||
- iterations (int): 迭代次数,即训练过程循环的次数。
|
||||
- other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将**无法通过**自动评测,**成绩为零**。
|
||||
|
||||
Returns:
|
||||
- numpy array: 训练得到的权重。
|
||||
"""
|
||||
n_samples, n_features = X.shape
|
||||
weights = np.zeros(n_features)
|
||||
bias = 0
|
||||
|
||||
for _ in range(iterations):
|
||||
for i in range(n_samples):
|
||||
if y[i] * (np.dot(X[i], weights) + bias) <= 1:
|
||||
weights = weights - learning_rate * (weights - y[i] * X[i])
|
||||
bias = bias - learning_rate * y[i]
|
||||
else:
|
||||
weights = weights - learning_rate * weights
|
||||
|
||||
return weights
|
||||
|
||||
|
||||
# 支持向量机预测函数
|
||||
def predict_svm(inX, weights):
|
||||
"""
|
||||
支持向量机预测函数。
|
||||
@@ -53,33 +68,48 @@ def predict_svm(inX, weights):
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
prediction = np.sign(np.dot(inX, weights))
|
||||
return prediction
|
||||
|
||||
|
||||
# 这里也写自己的代码实现
|
||||
# 从文件中加载数据函数
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
|
||||
data, labels = [], []
|
||||
with open(filepath, 'r') as f:
|
||||
for line in f.readlines():
|
||||
line = line.strip().split()
|
||||
if len(line) >= 1 and line[-1].isdigit():
|
||||
line_data = [1] + [0 if x == '?' else float(x) for x in line[:-1]]
|
||||
data.append(line_data)
|
||||
labels.append(int(line[-1]))
|
||||
|
||||
Args:
|
||||
- directory (str): 数据文件路径。
|
||||
data = np.array(data)
|
||||
labels = np.array(labels)
|
||||
|
||||
Returns:
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
target_variable = labels
|
||||
target_variable[(target_variable == 2) | (target_variable == 3)] = 0
|
||||
|
||||
data = np.delete(data, [2] + list(range(23, 28)), axis=1)
|
||||
|
||||
|
||||
return data, target_variable
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 以下代码可用于测试,可自行随意修改
|
||||
# 加载训练集
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
X_train, y_train = load_data("./horse-colic.data")
|
||||
|
||||
# 加载测试集
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
X_test, y_test = load_data("./horse-colic.test")
|
||||
|
||||
# 训练支持向量机模型
|
||||
weights = train_svm(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_svm(X_test[11], weights)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||
# 预测测试集所有数据
|
||||
y_pred_all = [predict_svm(x, weights) for x in X_test]
|
||||
|
||||
# 计算准确率
|
||||
accuracy = np.mean(y_pred_all == y_test)
|
||||
|
||||
print(f"预测准确率: {accuracy}")
|
||||
Reference in New Issue
Block a user