更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 10s

This commit is contained in:
2024-11-03 04:10:05 +08:00
parent 84ca358069
commit 03c98cf1b4
+14 -1
View File
@@ -29,12 +29,25 @@ def load_data(filepath):
- X (numpy array): 特征数据 - X (numpy array): 特征数据
- y (numpy array): 标签数据 - y (numpy array): 标签数据
""" """
data = np.loadtxt(filepath, delimiter=",") # 读取数据,指定缺失值符号为 '?'
data = np.genfromtxt(filepath, delimiter=",", dtype=str)
# 将 '?' 替换为 np.nan 并转换为浮点数
data[data == '?'] = np.nan
data = data.astype(float)
# 对每一列的缺失值使用列的均值填充
col_means = np.nanmean(data, axis=0)
inds = np.where(np.isnan(data))
data[inds] = np.take(col_means, inds[1])
# 分离特征和标签
X = data[:, :-1] X = data[:, :-1]
y = data[:, -1] y = data[:, -1]
return X, y return X, y
# 这里实现对率回归训练函数 # 这里实现对率回归训练函数
def train_logreg(X, y, learning_rate=0.01, num_iterations=1000, regularization_strength=0.01): def train_logreg(X, y, learning_rate=0.01, num_iterations=1000, regularization_strength=0.01):
""" """