generated from LufsX/Experiment-LogReg
This commit is contained in:
+13
-3
@@ -34,16 +34,27 @@ def load_data(filepath):
|
|||||||
- np.array: 特征数据。
|
- np.array: 特征数据。
|
||||||
- np.array: 标签数据。
|
- np.array: 标签数据。
|
||||||
"""
|
"""
|
||||||
# Load the data, treating missing values as NaN
|
|
||||||
try:
|
try:
|
||||||
|
# Load data with genfromtxt, filling missing values with NaN
|
||||||
data = np.genfromtxt(filepath, delimiter=',', missing_values='?', filling_values=np.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
|
# Remove rows with NaN values
|
||||||
data = data[~np.isnan(data).any(axis=1)]
|
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
|
X = data[:, :-1] # Features
|
||||||
y = data[:, -1] # Labels
|
y = data[:, -1] # Labels
|
||||||
|
|
||||||
return X, y
|
return X, y
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error loading data: {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
|
X = np.c_[np.ones(X.shape[0]), X] # Add intercept term
|
||||||
probabilities = sigmoid(np.dot(X, weights))
|
probabilities = sigmoid(np.dot(X, weights))
|
||||||
return (probabilities >= 0.5).astype(int)
|
return (probabilities >= 0.5).astype(int)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user