generated from LufsX/Experiment-LogReg
This commit is contained in:
+17
-4
@@ -88,7 +88,7 @@ def predict_logreg(inX, weights):
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据,填充缺失值,并进行特征处理。
|
||||
从文件中加载数据,填充缺失值为均值,并进行特征处理。
|
||||
|
||||
Args:
|
||||
- filepath (str): 数据文件路径。
|
||||
@@ -104,14 +104,26 @@ def load_data(filepath):
|
||||
for line in lines:
|
||||
line = line.strip().split()
|
||||
if len(line) >= 1 and line[-1].isdigit():
|
||||
# 将缺失值 '?' 填充为 0,并转换为浮点数
|
||||
line_data = [1] + [0 if x == '?' else float(x) for x in line[:-1]]
|
||||
# 初始化数据,将 '?' 保持为原始字符串
|
||||
line_data = [1] + [x if x != '?' else x for x in line[:-1]]
|
||||
data.append(line_data)
|
||||
labels.append(int(line[-1]))
|
||||
|
||||
data = np.array(data)
|
||||
# 转换为 numpy 数组
|
||||
data = np.array(data, dtype=np.object)
|
||||
labels = np.array(labels)
|
||||
|
||||
# 将 '?' 替换为 np.nan,便于计算均值
|
||||
data[data == '?'] = np.nan
|
||||
data = data.astype(float)
|
||||
|
||||
# 计算每个特征的均值
|
||||
col_means = np.nanmean(data[:, 1:], axis=0) # 排除常数项(偏置项)
|
||||
|
||||
# 用每个特征的均值填充缺失值
|
||||
inds = np.where(np.isnan(data))
|
||||
data[inds] = np.take(col_means, inds[1]-1)
|
||||
|
||||
# 选择特征,合并目标变量
|
||||
target_variable = labels
|
||||
target_variable[(target_variable == 2) | (target_variable == 3)] = 0 # 合并死亡和安乐死为0
|
||||
@@ -125,6 +137,7 @@ def load_data(filepath):
|
||||
return data, target_variable
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
|
||||
Reference in New Issue
Block a user