generated from LufsX/Experiment-LogReg
This commit is contained in:
+42
-18
@@ -2,6 +2,7 @@
|
||||
import numpy as np
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from itertools import product
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
|
||||
"""
|
||||
实验说明:
|
||||
@@ -42,9 +43,10 @@ def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64, regul
|
||||
- numpy array: 训练得到的权重。
|
||||
"""
|
||||
m, n = X.shape
|
||||
weights = np.random.randn(n) * 0.01 # 随机初始化权重
|
||||
weights = np.random.randn(n) * 0.01
|
||||
|
||||
for i in range(iterations):
|
||||
indices = np.random.permutation(m) # 随机打乱数据
|
||||
indices = np.random.permutation(m)
|
||||
X_shuffled = X[indices]
|
||||
y_shuffled = y[indices]
|
||||
|
||||
@@ -63,6 +65,10 @@ def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64, regul
|
||||
|
||||
weights -= learning_rate * gradient
|
||||
|
||||
# 学习率衰减
|
||||
if i % 100 == 0:
|
||||
learning_rate *= 0.95
|
||||
|
||||
return weights
|
||||
|
||||
|
||||
@@ -93,13 +99,12 @@ def load_data(filepath):
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
data = []
|
||||
labels = []
|
||||
data, labels = [], []
|
||||
|
||||
with open(filepath, 'r') as f:
|
||||
for line in f.readlines():
|
||||
for line in f:
|
||||
line = line.strip().split()
|
||||
if len(line) >= 1 and line[-1].isdigit():
|
||||
# 将缺失值 '?' 填充为 0,并转换为浮点数
|
||||
if line and line[-1].isdigit():
|
||||
line_data = [1] + [0 if x == '?' else float(x) for x in line[:-1]]
|
||||
data.append(line_data)
|
||||
labels.append(int(line[-1]))
|
||||
@@ -107,25 +112,44 @@ def load_data(filepath):
|
||||
data = np.array(data)
|
||||
labels = np.array(labels)
|
||||
|
||||
# 选择特征,合并目标变量
|
||||
target_variable = labels
|
||||
target_variable[(target_variable == 2) | (target_variable == 3)] = 0 # 合并死亡和安乐死为0
|
||||
# 映射标签
|
||||
labels[np.isin(labels, [2, 3])] = 0
|
||||
|
||||
# 去掉特征3和特征24~28
|
||||
data = np.delete(data, [2] + list(range(23, 28)), axis=1) # 特征3的索引是2,特征24-28的索引是23-27
|
||||
# 删除不需要的列
|
||||
data = np.delete(data, [2] + list(range(23, 28)), axis=1)
|
||||
|
||||
# 数据标准化
|
||||
scaler = StandardScaler()
|
||||
data = scaler.fit_transform(data)
|
||||
|
||||
return data, target_variable
|
||||
return data, labels
|
||||
|
||||
def accuracy(X, y, weights):
|
||||
"""计算模型准确率"""
|
||||
predictions = predict_logreg(X, weights)
|
||||
return np.mean(predictions == y)
|
||||
|
||||
if __name__ == "__main__":
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
# 加载数据
|
||||
X_train, y_train = load_data("./horse-colic.data")
|
||||
X_test, y_test = load_data("./horse-colic.test")
|
||||
|
||||
final_weights = train_logreg(X_train, y_train, learning_rate=0.05, iterations=1000, batch_size=16, regularization='L1', lambda_reg=0.05)
|
||||
# 使用随机森林进行初步预测
|
||||
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
|
||||
rf_model.fit(X_train, y_train)
|
||||
|
||||
# 计算并输出最终测试集的准确率
|
||||
final_accuracy = accuracy(X_test, y_test, final_weights)
|
||||
rf_predictions_train = rf_model.predict(X_train)
|
||||
rf_predictions_test = rf_model.predict(X_test)
|
||||
|
||||
# 将随机森林预测的结果作为新的特征
|
||||
X_train_combined = np.column_stack((X_train, rf_predictions_train))
|
||||
X_test_combined = np.column_stack((X_test, rf_predictions_test))
|
||||
|
||||
# 使用逻辑回归进行训练
|
||||
final_weights = train_logreg(X_train_combined, y_train, learning_rate=0.01,
|
||||
iterations=5000, batch_size=32,
|
||||
regularization='L1', lambda_reg=0.01)
|
||||
|
||||
# 计算准确率
|
||||
final_accuracy = accuracy(X_test_combined, y_test, final_weights)
|
||||
print(f"最终测试集的准确率: {final_accuracy:.4f}")
|
||||
Reference in New Issue
Block a user