From 49d7b0c7e82a813348742a884a2b949fcf8184d7 Mon Sep 17 00:00:00 2001 From: 43606070 <43606070@qq.com> Date: Sat, 2 Nov 2024 15:57:27 +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 | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/function.py b/function.py index 04cfd7d..5c6af88 100644 --- a/function.py +++ b/function.py @@ -38,7 +38,7 @@ def load_data(filepath): data = np.genfromtxt(filepath, delimiter=delimiter, missing_values='?', filling_values=np.nan) # Check if the data has been loaded correctly with at least two dimensions - if data.ndim < 2 or data.size == 0: + if data.ndim < 2 or data.shape[1] < 2: # 至少需要两列:特征和标签 continue # 尝试下一个分隔符 # Remove rows with NaN values @@ -52,6 +52,14 @@ def load_data(filepath): # Split into features (X) and labels (y) X = data[:, :-1] # Features y = data[:, -1] # Labels + + # Check if shapes of X and y match + if X.shape[0] != y.shape[0]: + print(f"Error: Feature and label count mismatch in file '{filepath}'") + return None, None + + # 输出数据形状供检查 + print(f"Loaded data from '{filepath}': {X.shape[0]} samples, {X.shape[1]} features.") return X, y except Exception as e: @@ -111,3 +119,4 @@ def predict_logreg(X, weights): X = np.c_[np.ones(X.shape[0]), X] # Add intercept term probabilities = sigmoid(np.dot(X, weights)) return (probabilities >= 0.5).astype(int) +