generated from LufsX/Experiment-LogReg
This commit is contained in:
+2
-30
@@ -65,7 +65,7 @@ def load_data(filepath):
|
||||
labels = np.array(labels)
|
||||
|
||||
num_features = data.shape[1]
|
||||
indices_to_remove = [2] # 去掉特征3(索引2)
|
||||
indices_to_remove = [2] # 去掉特征3(索引2)
|
||||
|
||||
if num_features > 23:
|
||||
indices_to_remove.extend(range(23, min(28, num_features)))
|
||||
@@ -103,32 +103,4 @@ def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, ba
|
||||
if __name__ == "__main__":
|
||||
# 加载训练集
|
||||
try:
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
except FileNotFoundError as e:
|
||||
print(e)
|
||||
exit()
|
||||
|
||||
# 数据归一化处理
|
||||
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)
|
||||
print(f"K折交叉验证的平均准确率: {avg_accuracy:.4f}")
|
||||
|
||||
# 加载测试集
|
||||
try:
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
except FileNotFoundError as e:
|
||||
print(e)
|
||||
exit()
|
||||
|
||||
# 测试集也需要进行相同的归一化
|
||||
X_test = scaler.transform(X_test) # 使用相同的Scaler进行测试集的标准化
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||
X_train, y_train = load_data("./data/hor
|
||||
|
||||
Reference in New Issue
Block a user