diff --git a/function.py b/function.py index 0c39134..57734ac 100644 --- a/function.py +++ b/function.py @@ -26,8 +26,11 @@ name = "章程鸿" # 这里写自己的代码实现 +import numpy as np + def sigmoid(z): return 1 / (1 + np.exp(-z)) + def train_logreg(X, y, learning_rate=0.01, iterations=1000): """ 对率回归训练函数。 @@ -79,11 +82,16 @@ def load_data(filepath): - np.array: 特征数据。 - np.array: 标签数据。 """ - data = np.loadtxt(filepath, delimiter=',') + data = [] + with open(filepath, 'r') as file: + for line in file: + # 清理无效数据 + cleaned_line = line.strip().replace('?', '0') # 将问号替换为0 + data.append([float(x) for x in cleaned_line.split()]) # 转换为浮点数 + data = np.array(data) X = data[:, :-1] y = data[:, -1] - return X, y - + return X, y if __name__ == "__main__": # 以下代码可用于测试,可自行随意修改 # 加载训练集