generated from LufsX/Experiment-LogReg
This commit is contained in:
+19
-4
@@ -57,24 +57,27 @@ def preprocess_data(X, y):
|
||||
|
||||
return X, y
|
||||
|
||||
|
||||
|
||||
def load_data(filepath):
|
||||
# 使用csv模块读取数据
|
||||
data = []
|
||||
with open(filepath, 'r') as file:
|
||||
reader = csv.reader(file, delimiter=' ')
|
||||
for row in reader:
|
||||
if row and row[0] != '?': # 忽略以'?'开头的行
|
||||
row = [float(x) if x != '?' else np.nan for x in row]
|
||||
if row: # 确保行不为空
|
||||
# 将非数值和空字符串转换为np.nan
|
||||
row = [float(x) if x.replace('.', '', 1).isdigit() else np.nan for x in row]
|
||||
data.append(row)
|
||||
|
||||
# 将'?'替换为np.nan
|
||||
# 将列表转换为numpy数组
|
||||
data = np.array(data, dtype=float)
|
||||
|
||||
# 使用SimpleImputer填充缺失值
|
||||
imputer = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0)
|
||||
data = imputer.fit_transform(data)
|
||||
|
||||
# 将所有列转换为数值类型
|
||||
# 分割特征和标签
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1].astype(int)
|
||||
|
||||
@@ -83,6 +86,18 @@ def load_data(filepath):
|
||||
|
||||
return X, y
|
||||
|
||||
def preprocess_data(X, y):
|
||||
# 合并目标变量的类别
|
||||
y[y == 2] = 0
|
||||
y[y == 3] = 0
|
||||
|
||||
# 删除特征3和特征24~28
|
||||
X = np.delete(X, [3], axis=1)
|
||||
X = X[:, :23]
|
||||
|
||||
return X, y
|
||||
|
||||
# 确保其他函数(sigmoid, train_logreg, predict_logreg)保持不变
|
||||
def main():
|
||||
# 更新文件路径以匹配实际位置
|
||||
X_train, y_train = load_data("C:/Users/HP/Desktop/机器学习/Experiment-LogReg/data/horse-colic.data")
|
||||
|
||||
Reference in New Issue
Block a user