generated from LufsX/Challenge-SVM
This commit is contained in:
+49
-19
@@ -4,32 +4,62 @@ import numpy as np
|
||||
id = "202210423230"
|
||||
name = "尹家立"
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据,处理缺失值,并确保标签维持原始三分类标签。
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from sklearn.impute import SimpleImputer
|
||||
|
||||
Args:
|
||||
- filepath (str): 数据文件路径。
|
||||
def load_data(data_path, NAN_THRES=0.5):
|
||||
# 读取数据并指定列
|
||||
horse_data = pd.read_csv(data_path, header=None, sep=r"\s+")
|
||||
horse_data = horse_data.loc[:, :22] # 只取到死亡情况(前23列,索引0-22)
|
||||
horse_data.columns = data_columns # 指定列名
|
||||
horse_data = horse_data.replace("?", float('NaN')) # 替换缺失值标记
|
||||
|
||||
Returns:
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
# 读取数据,将 '?' 替换为 NaN
|
||||
data = np.genfromtxt(filepath, delimiter=' ', dtype=float, missing_values='?', filling_values=np.nan)
|
||||
# 删除无关或缺失值过多的特征
|
||||
col_del = ["Hospital Number"]
|
||||
for col in horse_data.columns:
|
||||
if horse_data[col].isna().sum() > len(horse_data) * NAN_THRES and col not in col_del:
|
||||
col_del.append(col)
|
||||
horse_data = horse_data.drop(columns=col_del)
|
||||
|
||||
# 用列均值填补缺失值
|
||||
nan_indices = np.isnan(data)
|
||||
col_means = np.nanmean(data, axis=0)
|
||||
data[nan_indices] = np.take(col_means, np.where(nan_indices)[1])
|
||||
# 删除标签缺失的样本
|
||||
horse_data = horse_data.dropna(subset=['outcome']).reset_index(drop=True)
|
||||
|
||||
# 分离特征和标签
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1]
|
||||
x_data = horse_data.drop(columns=['outcome'])
|
||||
y_data = horse_data[['outcome']]
|
||||
|
||||
# 确保标签值保持三分类标签(例如,假设标签值分别为 1:存活,2:死亡,3:安乐死)
|
||||
return X, y
|
||||
# 标签:保留三分类
|
||||
y_data['outcome'] = pd.to_numeric(y_data['outcome'], errors='coerce').astype('int32')
|
||||
|
||||
# 分离离散和连续特征
|
||||
x_discrete = x_data[list(set(columns_discrete) - set(col_del))]
|
||||
x_continue = x_data.drop(columns=x_discrete.columns)
|
||||
|
||||
# 离散特征 one-hot 编码
|
||||
for col in col_oneHot:
|
||||
if col in x_discrete.columns:
|
||||
column_onehot = pd.get_dummies(x_discrete[col], prefix=col)
|
||||
x_discrete = x_discrete.drop(columns=col)
|
||||
x_discrete = pd.concat([x_discrete, column_onehot], axis=1)
|
||||
|
||||
# 用众数填补离散特征缺失值
|
||||
imp = SimpleImputer(strategy='most_frequent')
|
||||
x_discrete = pd.DataFrame(imp.fit_transform(x_discrete), columns=x_discrete.columns)
|
||||
x_discrete = x_discrete.astype('int32')
|
||||
|
||||
# 用均值填补连续特征缺失值并归一化
|
||||
for col in x_continue.columns:
|
||||
x_continue[col] = pd.to_numeric(x_continue[col], errors='coerce').astype("float64")
|
||||
x_continue[col] = x_continue[col].fillna(x_continue[col].mean())
|
||||
x_continue = (x_continue - x_continue.mean()) / (x_continue.max() - x_continue.min())
|
||||
|
||||
# 合并特征和标签
|
||||
horse_data = pd.concat([x_discrete, x_continue, y_data], axis=1)
|
||||
horse_data = horse_data.sort_index(axis=1) # 排序列名,确保验证集和训练集一致
|
||||
|
||||
# 最终返回特征和标签
|
||||
return horse_data.drop(columns=['outcome']).values, horse_data['outcome'].values
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user