generated from LufsX/Experiment-LogReg
This commit is contained in:
+17
-22
@@ -1,5 +1,6 @@
|
||||
import numpy as np
|
||||
import numpy as np
|
||||
import os
|
||||
from sklearn.preprocessing import StandardScaler # 导入标准化库
|
||||
|
||||
# 学号和姓名
|
||||
id = "202210423130"
|
||||
@@ -45,7 +46,7 @@ def predict_logreg(inX, weights):
|
||||
h = sigmoid(z)
|
||||
return 1 if h >= 0.5 else 0
|
||||
|
||||
def load_data(filepath, remove_indices=None):
|
||||
def load_data(filepath):
|
||||
"""从文件中加载数据。"""
|
||||
if not os.path.exists(filepath):
|
||||
raise FileNotFoundError(f"文件 {filepath} 不存在!")
|
||||
@@ -53,10 +54,8 @@ def load_data(filepath, remove_indices=None):
|
||||
data, labels = [], []
|
||||
print(f"加载数据中: {filepath}")
|
||||
with open(filepath, 'r') as f:
|
||||
for line_idx, line in enumerate(f):
|
||||
for line in f:
|
||||
line = line.strip().split()
|
||||
if line_idx == 2: # 跳过第三行
|
||||
continue
|
||||
if len(line) >= 1 and line[-1].isdigit():
|
||||
line_data = [0 if x == '?' else float(x) for x in line[:-1]]
|
||||
data.append(line_data)
|
||||
@@ -65,20 +64,18 @@ def load_data(filepath, remove_indices=None):
|
||||
data = np.array(data)
|
||||
labels = np.array(labels)
|
||||
|
||||
if remove_indices is not None:
|
||||
data = np.delete(data, remove_indices, axis=1)
|
||||
num_features = data.shape[1]
|
||||
indices_to_remove = [2] # 去掉特征3(索引2)
|
||||
|
||||
if num_features > 23:
|
||||
indices_to_remove.extend(range(23, min(28, num_features)))
|
||||
|
||||
data = np.delete(data, indices_to_remove, 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]
|
||||
@@ -104,18 +101,16 @@ 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] # 去掉特征3(索引2)
|
||||
|
||||
# 加载训练集
|
||||
try:
|
||||
X_train, y_train = load_data("./data/horse-colic.data", remove_indices=indices_to_remove)
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
except FileNotFoundError as e:
|
||||
print(e)
|
||||
exit()
|
||||
|
||||
# 归一化训练集
|
||||
X_train = normalize_data(X_train)
|
||||
# 数据归一化处理
|
||||
scaler = StandardScaler()
|
||||
X_train = scaler.fit_transform(X_train) # 使用训练集进行标准化
|
||||
|
||||
# k折交叉验证
|
||||
avg_accuracy = k_fold_cross_validation(X_train, y_train, k=5, learning_rate=0.01, iterations=1000)
|
||||
@@ -123,13 +118,13 @@ if __name__ == "__main__":
|
||||
|
||||
# 加载测试集
|
||||
try:
|
||||
X_test, y_test = load_data("./data/horse-colic.test", remove_indices=indices_to_remove)
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
except FileNotFoundError as e:
|
||||
print(e)
|
||||
exit()
|
||||
|
||||
# 归一化测试集
|
||||
X_test = normalize_data(X_test)
|
||||
# 测试集也需要进行相同的归一化
|
||||
X_test = scaler.transform(X_test) # 使用相同的Scaler进行测试集的标准化
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
|
||||
Reference in New Issue
Block a user