forked from LufsX/Experiment-LogReg
This commit is contained in:
+127
-56
@@ -1,69 +1,140 @@
|
|||||||
|
# function.py
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
|
||||||
|
from sklearn.preprocessing import StandardScaler
|
||||||
|
from sklearn.metrics import accuracy_score, roc_auc_score, f1_score
|
||||||
|
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, VotingClassifier
|
||||||
|
from sklearn.linear_model import LogisticRegression
|
||||||
|
from sklearn.svm import SVC
|
||||||
|
"""
|
||||||
|
实验说明:
|
||||||
|
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
||||||
|
|
||||||
|
注意事项:
|
||||||
|
- 在 function.py 中实现对率回归算法。
|
||||||
|
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
|
||||||
|
- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 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
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
请在这里填写学号和姓名,不填写即成绩无效。
|
请在这里填写学号和姓名,不填写即成绩无效。
|
||||||
"""
|
"""
|
||||||
id = "202210423224"
|
id = "202210423221"
|
||||||
name = "吴杨洋"
|
name = "廖子豪"
|
||||||
|
def load_data(filepath):
|
||||||
|
file = open(filepath)
|
||||||
|
fr = file.readlines()
|
||||||
|
data_list = []
|
||||||
|
y = []
|
||||||
|
for data in fr:
|
||||||
|
data = data.strip()
|
||||||
|
data = data.split(' ')
|
||||||
|
data_25 = data[24]
|
||||||
|
y.append(data[22])
|
||||||
|
del data[-1]
|
||||||
|
del data[26]
|
||||||
|
del data[25]
|
||||||
|
del data[24]
|
||||||
|
del data[22]
|
||||||
|
del data[2]
|
||||||
|
for i in data_25:
|
||||||
|
data.append(i)
|
||||||
|
data = [0 if data_elem == '?' else float(data_elem) for data_elem in data]
|
||||||
|
data_list.append(data)
|
||||||
|
data_number = len(y)
|
||||||
|
y_train = np.array([1 if y[j] == '2' or y[j] == '3' else 0 for j in range(data_number)])
|
||||||
|
X = np.array(data_list)
|
||||||
|
X_train = np.insert(X, 0, 1, axis=1) # 添加偏置项
|
||||||
|
return X_train, y_train
|
||||||
|
|
||||||
|
def train_logreg(X, y, numIter=1000, learning_rate=0.06, batch_size=128, lambda_reg=0.01):
|
||||||
def sigmoid(z):
|
|
||||||
return 1 / (1 + np.exp(-z))
|
|
||||||
|
|
||||||
|
|
||||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.zeros(n)
|
weights = np.zeros(n)
|
||||||
for i in range(iterations):
|
for j in range(numIter):
|
||||||
linear_model = np.dot(X, weights)
|
dataIndex = np.random.permutation(m)
|
||||||
y_predicted = sigmoid(linear_model)
|
for i in range(0, m, batch_size):
|
||||||
gradient = np.dot(X.T, (y_predicted - y)) / m
|
end = min(i + batch_size, m)
|
||||||
weights -= learning_rate * gradient
|
batch_X = X[dataIndex[i:end]]
|
||||||
|
batch_y = y[dataIndex[i:end]]
|
||||||
|
z = np.dot(batch_X, weights)
|
||||||
|
h = 1 / (1 + np.exp(-z))
|
||||||
|
error = batch_y - h
|
||||||
|
gradient = np.dot(batch_X.T, error) / len(batch_y)
|
||||||
|
reg_term = 2 * lambda_reg * weights
|
||||||
|
gradient -= reg_term / len(batch_y)
|
||||||
|
weights += learning_rate * gradient
|
||||||
return weights
|
return weights
|
||||||
|
|
||||||
|
|
||||||
def predict_logreg(inX, weights):
|
def predict_logreg(inX, weights):
|
||||||
linear_model = np.dot(inX, weights)
|
z = np.dot(inX, weights)
|
||||||
y_predicted = sigmoid(linear_model)
|
prob = 1 / (1 + np.exp(-z))
|
||||||
return 1 if y_predicted > 0.5 else 0
|
return 1 if prob >= 0.5 else 0
|
||||||
|
|
||||||
|
|
||||||
def load_data(filepath):
|
|
||||||
data = np.genfromtxt(filepath, delimiter=' ', dtype=float)
|
|
||||||
X = data[:, :-1]
|
|
||||||
y = data[:, -1]
|
|
||||||
return X, y
|
|
||||||
|
|
||||||
|
|
||||||
def standardize(X):
|
|
||||||
mean = X.mean(axis=0)
|
|
||||||
std = X.std(axis=0)
|
|
||||||
return (X - mean) / std, mean, std
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
# 加载训练集
|
|
||||||
X_train, y_train = load_data("./data/horse-colic.data")
|
|
||||||
# 加载测试集
|
|
||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
|
||||||
|
|
||||||
# 标准化特征数据
|
|
||||||
X_train, mean, std = standardize(X_train)
|
|
||||||
X_test = (X_test - mean) / std
|
|
||||||
|
|
||||||
# 训练对率回归模型
|
|
||||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
|
||||||
|
|
||||||
# 测试单个数据
|
|
||||||
correct_predictions = 0
|
|
||||||
for i in range(X_test.shape[0]):
|
|
||||||
y_pred = predict_logreg(X_test[i], weights)
|
|
||||||
if y_pred == y_test[i]:
|
|
||||||
correct_predictions += 1
|
|
||||||
|
|
||||||
accuracy = correct_predictions / X_test.shape[0]
|
|
||||||
print(f"Accuracy: {accuracy:.2f}")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
X_train, y_train = load_data("./data/horse-colic.data")
|
||||||
|
X_test, y_test = load_data("./data/horse-colic.test")
|
||||||
|
|
||||||
|
scaler = StandardScaler()
|
||||||
|
X_train = scaler.fit_transform(X_train)
|
||||||
|
X_test = scaler.transform(X_test)
|
||||||
|
|
||||||
|
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)
|
||||||
|
|
||||||
|
best_weights = None
|
||||||
|
best_auc = 0
|
||||||
|
for iter in range(1, 1001):
|
||||||
|
weights = train_logreg(X_train, y_train, numIter=iter, learning_rate=0.01, lambda_reg=0.1)
|
||||||
|
y_pred = [predict_logreg(x, weights) for x in X_val]
|
||||||
|
auc = roc_auc_score(y_val, y_pred)
|
||||||
|
if auc > best_auc:
|
||||||
|
best_auc = auc
|
||||||
|
best_weights = weights
|
||||||
|
if iter > 10 and auc == best_auc:
|
||||||
|
break
|
||||||
|
|
||||||
|
y_pred_test = [predict_logreg(x, best_weights) for x in X_test]
|
||||||
|
accuracy_test = accuracy_score(y_test, y_pred_test)
|
||||||
|
print(f"Test Accuracy: {accuracy_test}")
|
||||||
|
|
||||||
|
# 随机森林模型
|
||||||
|
param_grid = {
|
||||||
|
'n_estimators': [100, 200, 300],
|
||||||
|
'max_depth': [5, 10, 15],
|
||||||
|
'learning_rate': [0.01, 0.05, 0.1]
|
||||||
|
}
|
||||||
|
rf = RandomForestClassifier()
|
||||||
|
grid_search = GridSearchCV(estimator=rf, param_grid=param_grid, cv=5, scoring='accuracy')
|
||||||
|
grid_search.fit(X_train, y_train)
|
||||||
|
best_rf = grid_search.best_estimator_
|
||||||
|
print("Best parameters:", grid_search.best_params_)
|
||||||
|
print("Best accuracy:", grid_search.best_score_)
|
||||||
|
|
||||||
|
# 集成学习 - 投票分类器
|
||||||
|
log_clf = LogisticRegression()
|
||||||
|
rnd_clf = RandomForestClassifier(n_estimators=100, random_state=42)
|
||||||
|
voting_clf = VotingClassifier(estimators=[('lr', log_clf), ('rf', rnd_clf)], voting='soft')
|
||||||
|
voting_clf.fit(X_train, y_train)
|
||||||
|
y_pred = voting_clf.predict(X_test)
|
||||||
|
print("Voting classifier accuracy:", accuracy_score(y_test, y_pred))
|
||||||
|
|
||||||
|
# 梯度提升机模型
|
||||||
|
gb_clf = GradientBoostingClassifier()
|
||||||
|
gb_clf.fit(X_train, y_train)
|
||||||
|
y_pred = gb_clf.predict(X_test)
|
||||||
|
print("Gradient Boosting classifier accuracy:", accuracy_score(y_test, y_pred))
|
||||||
|
|
||||||
|
# 使用K折交叉验证评估模型性能
|
||||||
|
scores = cross_val_score(gb_clf, X_train, y_train, cv=5, scoring='accuracy')
|
||||||
|
print("Cross-validation scores:", scores)
|
||||||
|
print("Average cross-validation score:", np.mean(scores))
|
||||||
|
|
||||||
|
# 使用F1分数评估模型性能
|
||||||
|
f1 = f1_score(y_test, y_pred)
|
||||||
|
print("F1 Score:", f1)
|
||||||
Reference in New Issue
Block a user