generated from LufsX/Experiment-LogReg
This commit is contained in:
+37
@@ -26,6 +26,16 @@ name = "刘义天"
|
||||
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
Load data from a specified file path and handle missing values.
|
||||
|
||||
Args:
|
||||
- filepath (str): The path to the data file.
|
||||
|
||||
Returns:
|
||||
- np.array: Feature data.
|
||||
- np.array: Label data.
|
||||
"""
|
||||
data = []
|
||||
with open(filepath, 'r') as file:
|
||||
for line in file:
|
||||
@@ -44,6 +54,20 @@ def load_data(filepath):
|
||||
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.1, n_estimators=200, max_depth=5):
|
||||
"""
|
||||
Train a Gradient Boosting model.
|
||||
|
||||
Args:
|
||||
- X (numpy array): Feature data.
|
||||
- y (numpy array): Label data.
|
||||
- learning_rate (float): Learning rate for gradient boosting.
|
||||
- n_estimators (int): Number of trees.
|
||||
- max_depth (int): Maximum depth of each tree.
|
||||
|
||||
Returns:
|
||||
- GradientBoostingClassifier: Trained Gradient Boosting model as weights.
|
||||
- StandardScaler: Scaler fitted on training data for consistent scaling.
|
||||
"""
|
||||
scaler = StandardScaler()
|
||||
X = scaler.fit_transform(X)
|
||||
|
||||
@@ -54,10 +78,23 @@ def train_logreg(X, y, learning_rate=0.1, n_estimators=200, max_depth=5):
|
||||
random_state=42
|
||||
)
|
||||
gb_model.fit(X, y)
|
||||
|
||||
# Return model and scaler separately
|
||||
return gb_model, scaler
|
||||
|
||||
|
||||
def predict_logreg(inX, model, scaler):
|
||||
"""
|
||||
Predict the class label for a given sample using Gradient Boosting.
|
||||
|
||||
Args:
|
||||
- inX (numpy array): Input sample for classification.
|
||||
- model (GradientBoostingClassifier): Trained model.
|
||||
- scaler (StandardScaler): Scaler for feature normalization.
|
||||
|
||||
Returns:
|
||||
- int: Predicted class label (0 or 1).
|
||||
"""
|
||||
inX = scaler.transform([inX])
|
||||
prediction = model.predict(inX)
|
||||
return int(prediction[0])
|
||||
|
||||
Reference in New Issue
Block a user