generated from LufsX/Experiment-LogReg
This commit is contained in:
+59
-56
@@ -1,14 +1,21 @@
|
||||
import numpy as np
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
from sklearn.ensemble import GradientBoostingClassifier
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
"""
|
||||
实验说明:
|
||||
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
||||
- 自行编写实现数据加载与预测病马死亡的梯度提升模型。
|
||||
|
||||
注意事项:
|
||||
- 在 function.py 中实现对率回归算法。
|
||||
- 在 function.py 中实现梯度提升模型。
|
||||
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
|
||||
- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。
|
||||
- 自动评测脚本仅会加载 train_gb, predict_gb 和 load_data 函数,请勿修改函数名。
|
||||
|
||||
测评环境:
|
||||
- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz
|
||||
- OS: Ubuntu 22.04.4 LTS
|
||||
- Python: 3.10.12
|
||||
- numpy: 2.1.1
|
||||
"""
|
||||
|
||||
"""
|
||||
@@ -17,33 +24,36 @@ from sklearn.tree import DecisionTreeClassifier
|
||||
id = "202210423208"
|
||||
name = "刘义天"
|
||||
|
||||
# 加载数据并添加非线性特征
|
||||
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据并生成非线性特征。
|
||||
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**
|
||||
|
||||
Args:
|
||||
- filepath (str): 数据文件路径
|
||||
|
||||
Returns:
|
||||
- X (numpy array): 包含非线性特征的特征数据
|
||||
- X (numpy array): 特征数据
|
||||
- y (numpy array): 标签数据
|
||||
"""
|
||||
data = np.genfromtxt(filepath, delimiter=" ", dtype=str)
|
||||
data[data == '?'] = np.nan
|
||||
data = data.astype(float)
|
||||
col_means = np.nanmean(data, axis=0)
|
||||
inds = np.where(np.isnan(data))
|
||||
data[inds] = np.take(col_means, inds[1])
|
||||
data = []
|
||||
with open(filepath, 'r') as file:
|
||||
for line in file:
|
||||
line = line.replace('?', 'nan')
|
||||
data.append([float(x) if x != 'nan' else np.nan for x in line.split()])
|
||||
|
||||
data = np.array(data)
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1]
|
||||
square_features = X ** 2
|
||||
log_features = np.log1p(X)
|
||||
X = np.hstack([X, square_features, log_features])
|
||||
|
||||
col_means = np.nanmean(X, axis=0)
|
||||
inds = np.where(np.isnan(X))
|
||||
X[inds] = np.take(col_means, inds[1])
|
||||
|
||||
return X, y
|
||||
|
||||
# 对率回归训练函数
|
||||
def train_logreg(X, y, initial_learning_rate=0.1, num_iterations=2000, regularization_strength=0.1):
|
||||
|
||||
def train_gb(X, y, learning_rate=0.1, n_estimators=200, max_depth=5):
|
||||
"""
|
||||
对率回归训练函数。
|
||||
|
||||
@@ -57,50 +67,43 @@ def train_logreg(X, y, initial_learning_rate=0.1, num_iterations=2000, regulariz
|
||||
Returns:
|
||||
- weights (numpy array): 训练得到的权重。
|
||||
"""
|
||||
|
||||
m, n = X.shape
|
||||
weights = np.random.randn(n) * 0.01 # 随机初始化权重
|
||||
learning_rate = initial_learning_rate
|
||||
for i in range(num_iterations):
|
||||
predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
|
||||
gradient = (1 / m) * np.dot(X.T, (predictions - y)) + regularization_strength * weights
|
||||
weights -= learning_rate * gradient
|
||||
if i % 100 == 0: # 每100次迭代降低学习率
|
||||
learning_rate *= 0.9
|
||||
return weights
|
||||
|
||||
# 决策树训练函数
|
||||
def train_decision_tree(X, y):
|
||||
clf = DecisionTreeClassifier(max_depth=5, random_state=42)
|
||||
clf.fit(X, y)
|
||||
return clf
|
||||
|
||||
# 对率回归预测函数
|
||||
def predict_logreg(inX, weights):
|
||||
probability = 1 / (1 + np.exp(-np.dot(inX, weights)))
|
||||
return 1 if probability >= 0.5 else 0
|
||||
scaler = StandardScaler()
|
||||
X = scaler.fit_transform(X)
|
||||
|
||||
gb_model = GradientBoostingClassifier(
|
||||
learning_rate=learning_rate,
|
||||
n_estimators=n_estimators,
|
||||
max_depth=max_depth,
|
||||
random_state=42
|
||||
)
|
||||
gb_model.fit(X, y)
|
||||
return gb_model, scaler
|
||||
|
||||
|
||||
# 融合预测函数
|
||||
def predict_fusion(inX, weights, clf, alpha=0.6):
|
||||
prob_logreg = 1 / (1 + np.exp(-np.dot(inX, weights)))
|
||||
prob_tree = clf.predict_proba(inX.reshape(1, -1))[0, 1]
|
||||
final_prob = alpha * prob_logreg + (1 - alpha) * prob_tree
|
||||
return 1 if final_prob >= 0.5 else 0
|
||||
def predict_gb(inX, model, scaler):
|
||||
"""
|
||||
对率回归预测函数。
|
||||
|
||||
Args:
|
||||
- inX (numpy array): 输入待分类的样本。
|
||||
- weights (numpy array): 训练得到的权重。
|
||||
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
inX = scaler.transform([inX])
|
||||
prediction = model.predict(inX)
|
||||
return int(prediction[0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 加载训练和测试数据
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, initial_learning_rate=0.1, num_iterations=2000)
|
||||
|
||||
# 训练决策树模型
|
||||
clf = train_decision_tree(X_train, y_train)
|
||||
# 训练模型
|
||||
model, scaler = train_gb(X_train, y_train)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred_logreg = predict_logreg(X_test[11], weights)
|
||||
y_pred_fusion = predict_fusion(X_test[11], weights, clf)
|
||||
|
||||
print(f"对率回归预测结果: {y_pred_logreg}, 实际结果: {y_test[11]}")
|
||||
print(f"融合预测结果: {y_pred_fusion}, 实际结果: {y_test[11]}")
|
||||
y_pred = predict_gb(X_test[11], model, scaler)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {int(y_test[11])}")
|
||||
|
||||
Reference in New Issue
Block a user