generated from LufsX/Challenge-SVM
This commit is contained in:
+7
-4
@@ -10,8 +10,10 @@ def load_data(filepath):
|
|||||||
for line in f:
|
for line in f:
|
||||||
line = line.strip().split()
|
line = line.strip().split()
|
||||||
if len(line) >= 1 and line[-1].isdigit():
|
if len(line) >= 1 and line[-1].isdigit():
|
||||||
|
# 将特征值中的缺失值(“?”)替换为0,并将其余项转换为浮点数
|
||||||
line_data = [0 if x == '?' else float(x) for x in line[:-1]]
|
line_data = [0 if x == '?' else float(x) for x in line[:-1]]
|
||||||
data.append(line_data)
|
data.append(line_data)
|
||||||
|
# 保留标签原始的三分类标签
|
||||||
labels.append(int(line[-1]))
|
labels.append(int(line[-1]))
|
||||||
|
|
||||||
data = np.array(data)
|
data = np.array(data)
|
||||||
@@ -20,16 +22,16 @@ def load_data(filepath):
|
|||||||
# 检查特征数量
|
# 检查特征数量
|
||||||
num_features = data.shape[1]
|
num_features = data.shape[1]
|
||||||
|
|
||||||
# 特征选择 去掉特征3(索引2)和特征24~28(索引23~26) 确保索引有效
|
# 特征选择:去掉特征3(索引2)和特征24~28(索引23~26),确保索引有效
|
||||||
indices_to_delete = [2] # 特征3
|
indices_to_delete = [2] # 特征3
|
||||||
if num_features > 24: # 确保特征数量足够
|
if num_features > 24: # 确保特征数量足够
|
||||||
indices_to_delete += list(range(24, 27)) # 特征24到27(索引23到26)
|
indices_to_delete += list(range(24, 27)) # 特征24到27(索引23到26)
|
||||||
|
|
||||||
# 删除特征
|
# 删除指定特征列
|
||||||
data = np.delete(data, indices_to_delete, axis=1)
|
data = np.delete(data, indices_to_delete, axis=1)
|
||||||
|
|
||||||
# 将标签二分类化 特征23(索引22)取值2和3合并为0 其余为1
|
# 保留三分类标签
|
||||||
target_variable = np.where((labels == 2) | (labels == 3), 0, 1)
|
target_variable = labels # 不再合并任何标签,保持三分类
|
||||||
|
|
||||||
return data, target_variable
|
return data, target_variable
|
||||||
|
|
||||||
@@ -37,6 +39,7 @@ def load_data(filepath):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def train_svm(X, y, learning_rate=0.01, iterations=1000):
|
def train_svm(X, y, learning_rate=0.01, iterations=1000):
|
||||||
"""
|
"""
|
||||||
支持向量机训练函数。
|
支持向量机训练函数。
|
||||||
|
|||||||
Reference in New Issue
Block a user