generated from LufsX/Experiment-LogReg
This commit is contained in:
+39
-29
@@ -30,7 +30,7 @@ name = "程悦"
|
|||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
return 1.0 / (1 + np.exp(-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)
|
z = np.dot(X, weights)
|
||||||
h = sigmoid(z)
|
h = sigmoid(z)
|
||||||
gradient = np.dot(X.T, (h - y)) / m
|
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
|
weights -= learning_rate * gradient
|
||||||
return weights
|
return weights
|
||||||
|
|
||||||
@@ -103,43 +109,47 @@ def load_data(filepath):
|
|||||||
|
|
||||||
return data, target_variable
|
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__":
|
if __name__ == "__main__":
|
||||||
# 加载训练集和测试集
|
# 加载训练集和测试集
|
||||||
X_train, y_train = load_data("./data/horse-colic.data")
|
X_train, y_train = load_data("./data/horse-colic.data")
|
||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
X_test, y_test = load_data("./data/horse-colic.test")
|
||||||
|
|
||||||
# 使用GridSearchCV进行超参数调优
|
# 定义网格搜索的参数范围
|
||||||
param_grid = {
|
param_grid = {
|
||||||
'learning_rate': [0.001, 0.01, 0.1],
|
'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:
|
model = 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])
|
|
||||||
|
|
||||||
# 网格搜索
|
# 网格搜索
|
||||||
best_accuracy = 0
|
grid_search = GridSearchCV(model, param_grid, cv=5)
|
||||||
best_params = {}
|
grid_search.fit(X_train, y_train)
|
||||||
|
|
||||||
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}
|
|
||||||
|
|
||||||
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]}")
|
||||||
Reference in New Issue
Block a user