generated from LufsX/Challenge-SVM
This commit is contained in:
+48
-37
@@ -28,7 +28,7 @@ name = "邓龙树"
|
||||
|
||||
|
||||
# 这里写自己的代码实现
|
||||
def train_svm(X, y, learning_rate=0.001, iterations=1000):
|
||||
def train_svm(X, y, learning_rate=0.01, iterations=1000 ):
|
||||
"""
|
||||
支持向量机训练函数。
|
||||
|
||||
@@ -40,19 +40,16 @@ def train_svm(X, y, learning_rate=0.001, iterations=1000):
|
||||
Returns:
|
||||
- numpy array: 训练得到的权重。
|
||||
"""
|
||||
n_samples, n_features = X.shape
|
||||
# 初始化权重向量
|
||||
weights = np.zeros(n_features)
|
||||
eta = 0.001 # 学习率
|
||||
epochs = 1000 # 迭代次数
|
||||
|
||||
for _ in range(epochs):
|
||||
for idx, x_i in enumerate(X):
|
||||
condition = y[idx] * (np.dot(x_i, weights))
|
||||
if condition < 1:
|
||||
weights = weights + eta * ( (y[idx] * x_i) - (2 / (n_samples * epochs)) * weights)
|
||||
else:
|
||||
weights = weights + eta * (- (2 / (n_samples * epochs)) * weights)
|
||||
# 这里采用简单的梯度下降法示例来训练线性SVM(实际应用中可使用更高效的SMO等算法优化)
|
||||
num_samples, num_features = X.shape
|
||||
learning_rate = 0.01 # 学习率,可作为参数调整
|
||||
iterations = 1000 # 迭代次数,可作为参数调整
|
||||
weights = np.zeros(num_features)
|
||||
# 迭代更新权重
|
||||
for _ in range(iterations):
|
||||
for i in range(num_samples):
|
||||
if y[i] * (np.dot(X[i], weights)) < 1:
|
||||
weights += learning_rate * (y[i] * X[i])
|
||||
return weights
|
||||
|
||||
|
||||
@@ -67,8 +64,14 @@ def predict_svm(inX, weights):
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
prediction = np.sign(np.dot(inX, weights))
|
||||
return prediction if prediction!= 0 else -1
|
||||
# 计算决策函数值(这里是线性SVM的简单情况)
|
||||
result = np.dot(inX, weights)
|
||||
# 根据结果判断类别(简单的多分类策略示例,可优化)
|
||||
if result >= 0.5:
|
||||
return 1
|
||||
elif result < -0.5:
|
||||
return 3
|
||||
return 2
|
||||
|
||||
|
||||
def load_data(filepath):
|
||||
@@ -81,38 +84,46 @@ def load_data(filepath):
|
||||
Returns:
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
|
||||
"""
|
||||
data = []
|
||||
labels = []
|
||||
with open(filepath, 'r') as file:
|
||||
lines = file.readlines()
|
||||
for line in lines:
|
||||
try:
|
||||
line = line.strip().split()
|
||||
if '?' not in line: # 只处理不包含缺失值的行
|
||||
features = [float(x) for x in line[:-1]]
|
||||
label = float(line[-1])
|
||||
if label == 0:
|
||||
label = -1 # 将类别0转换为 -1 ,符合SVM二分类的约定
|
||||
data.append(features)
|
||||
labels.append(label)
|
||||
except ValueError:
|
||||
print(f"数据解析出现问题,跳过该行数据: {line}")
|
||||
continue
|
||||
return np.array(data), np.array(labels)
|
||||
with open(filepath, 'r') as f:
|
||||
for line in f.readlines():
|
||||
parts = line.strip().split()
|
||||
features = []
|
||||
for p in parts[:-1]:
|
||||
try:
|
||||
features.append(float(p))
|
||||
except ValueError:
|
||||
features.append(0) # 将无法转换的字符替换为0,也可以用其他合适值
|
||||
label = int(parts[-1])
|
||||
data.append(features)
|
||||
labels.append(label)
|
||||
data = np.array(data)
|
||||
labels = np.array(labels)
|
||||
# 选择特征23(从1开始由左向右数)作为目标变量
|
||||
data = data[:, 22].reshape(-1, 1) # 修正这里的语法错误,将--1改为-1
|
||||
return data, labels
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 加载训练集
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
|
||||
# 加载测试集
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
|
||||
# 训练支持向量机模型
|
||||
weights = train_svm(X_train, y_train, learning_rate=0.001, iterations=1000)
|
||||
|
||||
# 测试单个数据
|
||||
weights = train_svm(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
|
||||
# 统计正确分类的样本个数
|
||||
correct_count = 0
|
||||
for i in range(len(X_test)):
|
||||
y_pred = predict_svm(X_test[i], weights)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[i]}")
|
||||
if y_pred == y_test[i]:
|
||||
correct_count += 1
|
||||
|
||||
# 计算准确率(得分)
|
||||
score = correct_count / len(X_test) * 100
|
||||
|
||||
print(f"预测准确率: {score}%")
|
||||
Reference in New Issue
Block a user