generated from LufsX/Experiment-LogReg
This commit is contained in:
+14
-5
@@ -83,14 +83,20 @@ def preprocess_data(X):
|
|||||||
return scaler.fit_transform(X)
|
return scaler.fit_transform(X)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# 加载数据集
|
# 加载训练集
|
||||||
X, y = load_data("./data/horse-colic.data")
|
X_train, y_train = load_data("./data/horse-colic.data")
|
||||||
|
|
||||||
# 归一化数据
|
# 归一化训练集
|
||||||
X = preprocess_data(X)
|
X_train = preprocess_data(X_train)
|
||||||
|
|
||||||
|
# 加载测试集
|
||||||
|
X_test, y_test = load_data("./data/horse-colic.test")
|
||||||
|
|
||||||
|
# 归一化测试集
|
||||||
|
X_test = preprocess_data(X_test)
|
||||||
|
|
||||||
# 使用 train_test_split 打乱并随机抽取数据
|
# 使用 train_test_split 打乱并随机抽取数据
|
||||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)
|
||||||
|
|
||||||
# K折交叉验证
|
# K折交叉验证
|
||||||
k = 5
|
k = 5
|
||||||
@@ -114,6 +120,9 @@ if __name__ == "__main__":
|
|||||||
print(f"K折交叉验证得分: {accuracies}")
|
print(f"K折交叉验证得分: {accuracies}")
|
||||||
print(f"平均准确率: {np.mean(accuracies)}")
|
print(f"平均准确率: {np.mean(accuracies)}")
|
||||||
|
|
||||||
|
# 训练逻辑回归模型
|
||||||
|
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||||
|
|
||||||
# 测试单个数据
|
# 测试单个数据
|
||||||
y_pred = predict_logreg(X_test[11], weights)
|
y_pred = predict_logreg(X_test[11], weights)
|
||||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}")
|
||||||
|
|||||||
Reference in New Issue
Block a user