generated from LufsX/Experiment-LogReg
This commit is contained in:
+38
-1
@@ -26,6 +26,16 @@ name = "刘义天"
|
|||||||
|
|
||||||
|
|
||||||
def load_data(filepath):
|
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 = []
|
data = []
|
||||||
with open(filepath, 'r') as file:
|
with open(filepath, 'r') as file:
|
||||||
for line in 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):
|
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()
|
scaler = StandardScaler()
|
||||||
X = scaler.fit_transform(X)
|
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
|
random_state=42
|
||||||
)
|
)
|
||||||
gb_model.fit(X, y)
|
gb_model.fit(X, y)
|
||||||
|
|
||||||
|
# Return model and scaler separately
|
||||||
return gb_model, scaler
|
return gb_model, scaler
|
||||||
|
|
||||||
|
|
||||||
def predict_logreg(inX, 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])
|
inX = scaler.transform([inX])
|
||||||
prediction = model.predict(inX)
|
prediction = model.predict(inX)
|
||||||
return int(prediction[0])
|
return int(prediction[0])
|
||||||
@@ -73,4 +110,4 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
# 测试单个数据
|
# 测试单个数据
|
||||||
y_pred = predict_logreg(X_test[11], model, scaler)
|
y_pred = predict_logreg(X_test[11], model, scaler)
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {int(y_test[11])}")
|
print(f"预测结果: {y_pred}, 实际结果: {int(y_test[11])}")
|
||||||
|
|||||||
Reference in New Issue
Block a user