generated from LufsX/Experiment-LogReg
This commit is contained in:
+14
-10
@@ -1,6 +1,5 @@
|
||||
import numpy as np
|
||||
import os
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
# 学号和姓名
|
||||
id = "202210423130"
|
||||
@@ -68,12 +67,18 @@ def load_data(filepath, remove_indices=None):
|
||||
|
||||
if remove_indices is not None:
|
||||
data = np.delete(data, remove_indices, axis=1)
|
||||
|
||||
|
||||
target_variable = np.where((labels == 2) | (labels == 3), 0, 1)
|
||||
|
||||
print(f"数据加载成功,数据形状: {data.shape}, 标签形状: {target_variable.shape}")
|
||||
return data, target_variable
|
||||
|
||||
def normalize_data(X):
|
||||
"""归一化数据。"""
|
||||
min_vals = X.min(axis=0)
|
||||
max_vals = X.max(axis=0)
|
||||
return (X - min_vals) / (max_vals - min_vals)
|
||||
|
||||
def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1):
|
||||
"""K折交叉验证。"""
|
||||
m = X.shape[0]
|
||||
@@ -99,9 +104,9 @@ def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, ba
|
||||
return np.mean(accuracies)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 定义要剔除的特征索引
|
||||
indices_to_remove = [2] + list(range(23, 28)) # 去掉特征3和24到28的特征
|
||||
|
||||
# 剔除特征索引
|
||||
indices_to_remove = [2] # 去掉特征3(索引2)
|
||||
|
||||
# 加载训练集
|
||||
try:
|
||||
X_train, y_train = load_data("./data/horse-colic.data", remove_indices=indices_to_remove)
|
||||
@@ -109,9 +114,8 @@ if __name__ == "__main__":
|
||||
print(e)
|
||||
exit()
|
||||
|
||||
# 数据归一化处理
|
||||
scaler = StandardScaler()
|
||||
X_train = scaler.fit_transform(X_train) # 使用训练集进行标准化
|
||||
# 归一化训练集
|
||||
X_train = normalize_data(X_train)
|
||||
|
||||
# k折交叉验证
|
||||
avg_accuracy = k_fold_cross_validation(X_train, y_train, k=5, learning_rate=0.01, iterations=1000)
|
||||
@@ -124,8 +128,8 @@ if __name__ == "__main__":
|
||||
print(e)
|
||||
exit()
|
||||
|
||||
# 测试集也需要进行相同的归一化
|
||||
X_test = scaler.transform(X_test) # 使用相同的Scaler进行测试集的标准化
|
||||
# 归一化测试集
|
||||
X_test = normalize_data(X_test)
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
|
||||
Reference in New Issue
Block a user