generated from LufsX/Challenge-SVM
This commit is contained in:
+4
-1
@@ -87,6 +87,7 @@ def load_data(filepath):
|
|||||||
with open(filepath, 'r') as file:
|
with open(filepath, 'r') as file:
|
||||||
lines = file.readlines()
|
lines = file.readlines()
|
||||||
for line in lines:
|
for line in lines:
|
||||||
|
try:
|
||||||
line = line.strip().split()
|
line = line.strip().split()
|
||||||
if '?' not in line: # 只处理不包含缺失值的行
|
if '?' not in line: # 只处理不包含缺失值的行
|
||||||
features = [float(x) for x in line[:-1]]
|
features = [float(x) for x in line[:-1]]
|
||||||
@@ -95,6 +96,9 @@ def load_data(filepath):
|
|||||||
label = -1 # 将类别0转换为 -1 ,符合SVM二分类的约定
|
label = -1 # 将类别0转换为 -1 ,符合SVM二分类的约定
|
||||||
data.append(features)
|
data.append(features)
|
||||||
labels.append(label)
|
labels.append(label)
|
||||||
|
except ValueError:
|
||||||
|
print(f"数据解析出现问题,跳过该行数据: {line}")
|
||||||
|
continue
|
||||||
return np.array(data), np.array(labels)
|
return np.array(data), np.array(labels)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@@ -112,4 +116,3 @@ if __name__ == "__main__":
|
|||||||
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]}")
|
print(f"预测结果: {y_pred}, 实际结果: {y_test[i]}")
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user