diff --git a/function.py b/function.py index 9cc2ab4..f77b360 100644 --- a/function.py +++ b/function.py @@ -34,16 +34,27 @@ def load_data(filepath): - np.array: 特征数据。 - np.array: 标签数据。 """ - # Load the data, treating missing values as NaN try: + # Load data with genfromtxt, filling missing values with NaN data = np.genfromtxt(filepath, delimiter=',', missing_values='?', filling_values=np.nan) + # Check if the data has been loaded correctly with at least two dimensions + if data.ndim < 2: + print(f"Error: File '{filepath}' may be empty or improperly formatted.") + return None, None + # Remove rows with NaN values data = data[~np.isnan(data).any(axis=1)] - # Split into features and labels + # Ensure there's still data left after removing rows with NaN + if data.size == 0: + print(f"Error: No valid data after removing NaN rows in file '{filepath}'.") + return None, None + + # Split into features (X) and labels (y) X = data[:, :-1] # Features y = data[:, -1] # Labels + return X, y except Exception as e: print(f"Error loading data: {e}") @@ -100,4 +111,3 @@ 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) -