generated from LufsX/Challenge-SVM
This commit is contained in:
+24
-18
@@ -5,29 +5,35 @@ id = "202210423230"
|
||||
name = "尹家立"
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据,并处理缺失值。
|
||||
data, labels = [], []
|
||||
with open(filepath, 'r') as f:
|
||||
for line in f:
|
||||
line = line.strip().split()
|
||||
if len(line) >= 1 and line[-1].isdigit():
|
||||
line_data = [0 if x == '?' else float(x) for x in line[:-1]]
|
||||
data.append(line_data)
|
||||
labels.append(int(line[-1]))
|
||||
|
||||
Args:
|
||||
- filepath (str): 数据文件路径。
|
||||
data = np.array(data)
|
||||
labels = np.array(labels)
|
||||
|
||||
Returns:
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
# 读取数据,替换缺失值 '?' 为 NaN
|
||||
data = np.genfromtxt(filepath, delimiter=' ', dtype=float, missing_values='?', filling_values=np.nan)
|
||||
# 检查特征数量
|
||||
num_features = data.shape[1]
|
||||
|
||||
# 将 NaN 替换为列均值
|
||||
nan_indices = np.isnan(data)
|
||||
col_means = np.nanmean(data, axis=0)
|
||||
data[nan_indices] = np.take(col_means, np.where(nan_indices)[1])
|
||||
# 特征选择:去掉特征3(索引2)和特征24~28(索引23~26),确保索引有效
|
||||
indices_to_delete = [2] # 特征3
|
||||
if num_features > 24: # 确保特征数量足够
|
||||
indices_to_delete += list(range(24, 27)) # 特征24到27(索引23到26)
|
||||
|
||||
# 删除特征
|
||||
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)
|
||||
|
||||
return data, target_variable
|
||||
|
||||
# 将最后一列作为标签
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1]
|
||||
|
||||
return X, y
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user