From 17d85e7a9d371022dc2d4f3df7553806841b6684 Mon Sep 17 00:00:00 2001 From: dong <2938690875@qq.com> Date: Thu, 31 Oct 2024 16:58:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/function.py b/function.py index 19caac4..0bfeecc 100644 --- a/function.py +++ b/function.py @@ -1,6 +1,8 @@ # function.py import numpy as np - +import pandas as pd +from sklearn.preprocessing import StandardScaler +from sklearn.model_selection import train_test_split """ @@ -88,6 +90,11 @@ def load_data(filepath): return np.array(data), np.array(labels) +def preprocess_data(X): + # 特征标准化 + scaler = StandardScaler() + return scaler.fit_transform(X) + if __name__ == "__main__": # 以下代码可用于测试,可自行随意修改 @@ -99,6 +106,7 @@ if __name__ == "__main__": # 训练对率回归模型 weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000,lambda_reg=0.01) + X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42) # 测试单个数据 y_pred = predict_logreg(X_test[11], weights)