generated from LufsX/Experiment-LogReg
This commit is contained in:
+43
-79
@@ -1,70 +1,12 @@
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split, cross_val_score
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
|
||||
|
||||
"""实验说明:
|
||||
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
||||
|
||||
注意事项:
|
||||
- 在 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 = "202210423104"
|
||||
name = "张钦诚"
|
||||
|
||||
# 对率回归实现
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization_strength=0.01):
|
||||
"""对率回归训练函数。
|
||||
|
||||
Args:
|
||||
- X (numpy array): 特征数据。
|
||||
- y (numpy array): 标签数据。
|
||||
- learning_rate (float): 学习率。
|
||||
- iterations (int): 训练次数。
|
||||
- regularization_strength (float): L2正则化强度。
|
||||
|
||||
Returns:
|
||||
- numpy array: 训练得到的权重。
|
||||
"""
|
||||
m, n = X.shape
|
||||
weights = np.zeros(n)
|
||||
for _ in range(iterations):
|
||||
predictions = 1 / (1 + np.exp(-np.dot(X, weights)))
|
||||
errors = y - predictions
|
||||
gradient = np.dot(X.T, errors) / m - regularization_strength * weights / m
|
||||
weights += learning_rate * gradient
|
||||
return weights
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
"""对率回归预测函数。
|
||||
|
||||
Args:
|
||||
- inX (numpy array): 输入待分类的样本。
|
||||
- weights (numpy array): 训练得到的权重。
|
||||
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
probability = 1 / (1 + np.exp(-np.dot(inX, weights)))
|
||||
return 1 if probability >= 0.5 else 0
|
||||
|
||||
def load_data(filepath):
|
||||
"""从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
|
||||
|
||||
Args:
|
||||
- filepath (str): 数据文件路径。
|
||||
|
||||
Returns:
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
data = []
|
||||
with open(filepath, 'r') as file:
|
||||
for line in file:
|
||||
@@ -75,27 +17,49 @@ def load_data(filepath):
|
||||
except ValueError as e:
|
||||
print(f"Error processing line: {line}. Error: {e}")
|
||||
data = np.array(data)
|
||||
data = np.nan_to_num(data, nan=np.nanmean(data, axis=0)) # 将缺失值用平均值代替
|
||||
X = data[:, :-1] # 特征
|
||||
y = data[:, -1] # 标签
|
||||
return X, y
|
||||
data = np.nan_to_num(data, nan=np.nanmean(data, axis=0))
|
||||
return data
|
||||
|
||||
def preprocess_data(X):
|
||||
# 标准化特征
|
||||
scaler = StandardScaler()
|
||||
X_standardized = scaler.fit_transform(X)
|
||||
|
||||
# 增加多项式特征
|
||||
poly = PolynomialFeatures(degree=2, include_bias=False)
|
||||
X_poly = poly.fit_transform(X_standardized)
|
||||
return X_poly
|
||||
|
||||
def train_random_forest(X, y):
|
||||
model = RandomForestClassifier(n_estimators=113, max_depth=10, random_state=42)
|
||||
model.fit(X, y)
|
||||
return model
|
||||
|
||||
def predict(model, X):
|
||||
return model.predict(X)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 下面代码可用于测试,可自行随意修改
|
||||
# 加载训练集
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
data = load_data("./data/horse-colic.data")
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1]
|
||||
|
||||
# 加载测试集
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
# 数据集划分
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=2000, regularization_strength=0.1)
|
||||
# 数据预处理
|
||||
X_train_processed = preprocess_data(X_train)
|
||||
X_test_processed = preprocess_data(X_test)
|
||||
|
||||
# 测试数据集
|
||||
correct_predictions = 0
|
||||
for i in range(len(X_test)):
|
||||
y_pred = predict_logreg(X_test[i], weights)
|
||||
if y_pred == y_test[i]:
|
||||
correct_predictions += 1
|
||||
accuracy = correct_predictions / len(X_test)
|
||||
# 训练模型
|
||||
model = train_random_forest(X_train_processed, y_train)
|
||||
|
||||
# 交叉验证
|
||||
cv_scores = cross_val_score(model, X_train_processed, y_train, cv=5)
|
||||
print(f"交叉验证准确率: {np.mean(cv_scores) * 100:.2f}%")
|
||||
|
||||
# 预测
|
||||
y_pred = predict(model, X_test_processed)
|
||||
|
||||
# 计算正确率
|
||||
accuracy = np.mean(y_pred == y_test)
|
||||
print(f"测试出的正确率: {accuracy * 100:.2f}%")
|
||||
|
||||
Reference in New Issue
Block a user