generated from LufsX/Challenge-SVM
This commit is contained in:
+3
-3
@@ -50,7 +50,7 @@ def load_data(filepath):
|
|||||||
fields = [0 if f == '?' else float(f) for f in fields]
|
fields = [0 if f == '?' else float(f) for f in fields]
|
||||||
# 标签是倒数第6列
|
# 标签是倒数第6列
|
||||||
label = int(fields[-6])
|
label = int(fields[-6])
|
||||||
# 特征为其他列(不包含倒数第6列标签)
|
# 特征为其他列(不包含倒数第6列标签)
|
||||||
features = np.array(fields[: -6] + fields[-5:], dtype=float)
|
features = np.array(fields[: -6] + fields[-5:], dtype=float)
|
||||||
# 标签映射:将 1->0, 2->1, 3->2
|
# 标签映射:将 1->0, 2->1, 3->2
|
||||||
if label == 1:
|
if label == 1:
|
||||||
@@ -81,7 +81,7 @@ def train_svm(X, y, other_params=None):
|
|||||||
- other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将无法通过自动评测,成绩为零。
|
- other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将无法通过自动评测,成绩为零。
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
- SVC: 训练好的SVM模型。
|
- SVC: 训练好的SVC模型。
|
||||||
"""
|
"""
|
||||||
model = SVC(kernel='rbf', C=1.0, gamma='scale', random_state=42)
|
model = SVC(kernel='rbf', C=1.0, gamma='scale', random_state=42)
|
||||||
model.fit(X, y)
|
model.fit(X, y)
|
||||||
@@ -147,6 +147,6 @@ if __name__ == "__main__":
|
|||||||
print(f"测试集准确率: {accuracy * 100:.2f}%")
|
print(f"测试集准确率: {accuracy * 100:.2f}%")
|
||||||
|
|
||||||
# 测试单个数据
|
# 测试单个数据
|
||||||
test_sample = X_final_test[11] # 选择第12个样本(从0开始计数)
|
test_sample = X_final_test[11] # 选择第12个样本(从0开始计数)
|
||||||
y_pred_single = predict_svm(test_sample, best_model)
|
y_pred_single = predict_svm(test_sample, best_model)
|
||||||
print(f"\n单个样本预测结果: {y_pred_single}, 实际结果: {y_final_test[11]}")
|
print(f"\n单个样本预测结果: {y_pred_single}, 实际结果: {y_final_test[11]}")
|
||||||
Reference in New Issue
Block a user