generated from LufsX/Experiment-LogReg
This commit is contained in:
+39
-31
@@ -31,9 +31,17 @@ import numpy as np
|
|||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
return 1 / (1 + np.exp(-z))
|
return 1 / (1 + np.exp(-z))
|
||||||
|
|
||||||
def train_logreg(X, y, learning_rate=0.005, iterations=1000):
|
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||||
"""
|
"""
|
||||||
修改处:将学习率从 0.01 调整为 0.005。
|
对率回归训练函数。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- X (numpy array): 特征数据。
|
||||||
|
- y (numpy array): 标签数据。
|
||||||
|
- other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将**无法通过**自动评测,**成绩为零**。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- numpy array: 训练得到的权重。
|
||||||
"""
|
"""
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.zeros(n)
|
weights = np.zeros(n)
|
||||||
@@ -48,16 +56,38 @@ def train_logreg(X, y, learning_rate=0.005, iterations=1000):
|
|||||||
return weights
|
return weights
|
||||||
|
|
||||||
def predict_logreg(inX, weights):
|
def predict_logreg(inX, weights):
|
||||||
|
"""
|
||||||
|
对率回归预测函数。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- inX (numpy array): 输入待分类的样本。
|
||||||
|
- weights (numpy array): 训练得到的权重。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- int: 预测的分类标签。
|
||||||
|
"""
|
||||||
z = np.dot(inX, weights)
|
z = np.dot(inX, weights)
|
||||||
h = sigmoid(z)
|
h = sigmoid(z)
|
||||||
return 1 if h >= 0.5 else 0
|
return 1 if h >= 0.5 else 0
|
||||||
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
|
"""
|
||||||
|
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- directory (str): 数据文件路径。
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
- np.array: 特征数据。
|
||||||
|
- np.array: 标签数据。
|
||||||
|
"""
|
||||||
data = []
|
data = []
|
||||||
with open(filepath, 'r') as file:
|
with open(filepath, 'r') as file:
|
||||||
for line in file:
|
for line in file:
|
||||||
|
# 清理无效数据
|
||||||
cleaned_line = line.strip().replace('?', '0')
|
cleaned_line = line.strip().replace('?', '0')
|
||||||
values = [float(x) for x in cleaned_line.split()]
|
values = [float(x) for x in cleaned_line.split()]
|
||||||
|
# 处理目标变量
|
||||||
if values[22] in [2, 3]:
|
if values[22] in [2, 3]:
|
||||||
values[22] = 0
|
values[22] = 0
|
||||||
new_values = values[:2] + values[3:23]
|
new_values = values[:2] + values[3:23]
|
||||||
@@ -67,38 +97,16 @@ def load_data(filepath):
|
|||||||
y = data[:, -1]
|
y = data[:, -1]
|
||||||
return X, y
|
return X, y
|
||||||
|
|
||||||
def calculate_accuracy(X, y, weights):
|
|
||||||
predictions = [predict_logreg(x, weights) for x in X]
|
|
||||||
correct_predictions = sum([1 for pred, actual in zip(predictions, y) if pred == actual])
|
|
||||||
accuracy = correct_predictions / len(y)
|
|
||||||
return accuracy
|
|
||||||
|
|
||||||
def find_best_learning_rate(X_train, y_train, X_test, y_test, learning_rates, iterations=1000):
|
|
||||||
best_accuracy = 0
|
|
||||||
best_lr = None
|
|
||||||
best_weights = None
|
|
||||||
|
|
||||||
for lr in learning_rates:
|
|
||||||
weights = train_logreg(X_train, y_train, learning_rate=lr, iterations=iterations)
|
|
||||||
accuracy = calculate_accuracy(X_test, y_test, weights)
|
|
||||||
print(f"学习率: {lr}, 测试集准确率: {accuracy}")
|
|
||||||
|
|
||||||
if accuracy > best_accuracy:
|
|
||||||
best_accuracy = accuracy
|
|
||||||
best_lr = lr
|
|
||||||
best_weights = weights
|
|
||||||
|
|
||||||
return best_lr, best_accuracy, best_weights
|
|
||||||
|
|
||||||
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")
|
||||||
|
|
||||||
learning_rates = [0.0001, 0.001, 0.005, 0.01, 0.05, 0.1] # 不同的学习率
|
# 训练对率回归模型
|
||||||
best_lr, best_accuracy, best_weights = find_best_learning_rate(X_train, y_train, X_test, y_test, learning_rates)
|
weights = train_logreg(X_train, y_train, learning_rate=0.001, iterations=1000) # 修改学习率
|
||||||
|
|
||||||
print(f"最佳学习率: {best_lr}, 测试集准确率: {best_accuracy}")
|
# 测试单个数据
|
||||||
|
y_pred = predict_logreg(X_test[11], weights)
|
||||||
# 使用最佳学习率训练最终模型
|
|
||||||
y_pred = predict_logreg(X_test[11], best_weights)
|
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||||
|
|||||||
Reference in New Issue
Block a user