generated from LufsX/Experiment-LogReg
This commit is contained in:
+10
-1
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user