From 11567bc8211910ebbaf2ef70489ed090f04878e1 Mon Sep 17 00:00:00 2001 From: 43606070 <43606070@qq.com> Date: Thu, 31 Oct 2024 11:52:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/function.py b/function.py index 9568239..340896c 100644 --- a/function.py +++ b/function.py @@ -34,19 +34,29 @@ def load_data(filepath): - np.array: 标签数据。 """ # Load the data, treating missing values as NaN - data = np.genfromtxt(filepath, delimiter=',', missing_values='?', filling_values=np.nan) + try: + data = np.genfromtxt(filepath, delimiter=',', missing_values='?', filling_values=np.nan) + + # Check if data was loaded correctly + if data.ndim == 1 and len(data) == 1 and np.isnan(data).any(): + print(f"数据加载错误: 文件 '{filepath}' 可能为空或格式不正确。") + return None, None - # Check if data was loaded correctly - if data.ndim == 1: - print(f"数据加载错误: 文件 '{filepath}' 可能为空或格式不正确。") + # Remove rows with NaN values + data = data[~np.isnan(data).any(axis=1)] + + if data.size == 0: + print(f"数据加载错误: 文件 '{filepath}' 中没有有效数据。") + return None, None + + X = data[:, :-1] # All columns except the last one + y = data[:, -1] # Last column + + return X, y + except Exception as e: + print(f"数据加载错误: {e}") return None, None - data = data[~np.isnan(data).any(axis=1)] # Remove rows with NaN values - X = data[:, :-1] # All columns except the last one - y = data[:, -1] # Last column - - return X, y - def sigmoid(z): """计算 sigmoid 函数""" return 1 / (1 + np.exp(-z)) @@ -112,4 +122,3 @@ if __name__ == "__main__": # 测试单个数据 y_pred = predict_logreg(X_test[11], weights) print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") -