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