更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 32s

This commit is contained in:
cy
2024-10-26 18:21:02 +08:00
parent 0ae2dc272a
commit d8359b4502
+39 -29
View File
@@ -30,7 +30,7 @@ name = "程悦"
def sigmoid(z):
return 1.0 / (1 + np.exp(-z))
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization='l2', lambda_=0.01):
"""
对率回归训练函数。
@@ -47,6 +47,12 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
z = np.dot(X, weights)
h = sigmoid(z)
gradient = np.dot(X.T, (h - y)) / m
# 正则化
if regularization == 'l2':
gradient += lambda_ * weights / m
elif regularization == 'l1':
gradient += lambda_ * np.sign(weights) / m
weights -= learning_rate * gradient
return weights
@@ -103,43 +109,47 @@ def load_data(filepath):
return data, target_variable
class CustomLogisticRegression(BaseEstimator, ClassifierMixin):
def __init__(self, learning_rate=0.01, iterations=1000, regularization='l2', lambda_=0.01):
self.learning_rate = learning_rate
self.iterations = iterations
self.regularization = regularization
self.lambda_ = lambda_
self.weights = None
def fit(self, X, y):
self.weights = train_logreg(X, y, self.learning_rate, self.iterations,
self.regularization, self.lambda_)
def predict(self, X):
return np.array([predict_logreg(x, self.weights) for x in X])
if __name__ == "__main__":
# 加载训练集和测试集
X_train, y_train = load_data("./data/horse-colic.data")
X_test, y_test = load_data("./data/horse-colic.test")
# 使用GridSearchCV进行超参数调优
# 定义网格搜索的参数范围
param_grid = {
'learning_rate': [0.001, 0.01, 0.1],
'iterations': [100, 500, 1000]
'iterations': [100, 500, 1000],
'regularization': ['l1', 'l2'],
'lambda_': [0.01, 0.1, 1.0]
}
# 定义自定义逻辑回归模型
class CustomLogisticRegression:
def __init__(self, learning_rate=0.01, iterations=1000):
self.learning_rate = learning_rate
self.iterations = iterations
self.weights = None
def fit(self, X, y):
self.weights = train_logreg(X, y, self.learning_rate, self.iterations)
def predict(self, X):
return np.array([predict_logreg(x, self.weights) for x in X])
# 创建逻辑回归模型
model = CustomLogisticRegression()
# 网格搜索
best_accuracy = 0
best_params = {}
for lr in param_grid['learning_rate']:
for it in param_grid['iterations']:
model = CustomLogisticRegression(learning_rate=lr, iterations=it)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = np.mean(y_pred == y_test)
print(f"学习率: {lr}, 迭代次数: {it}, 准确率: {accuracy}")
if accuracy > best_accuracy:
best_accuracy = accuracy
best_params = {'learning_rate': lr, 'iterations': it}
grid_search = GridSearchCV(model, param_grid, cv=5)
grid_search.fit(X_train, y_train)
print(f"最佳参数: {best_params}, 最佳准确率: {best_accuracy}")
# 使用最佳模型进行预测
best_model = grid_search.best_estimator_
y_pred = best_model.predict(X_test)
# 测试单个数据
single_prediction = best_model.predict([X_test[11]])
print(f"最佳参数组合: {grid_search.best_params_}")
print(f"最佳准确率: {grid_search.best_score_}")
print(f"预测结果: {single_prediction[0]}, 实际结果: {y_test[11]}")