From 522c83e06b9474f4d23d237decc11706712c33e1 Mon Sep 17 00:00:00 2001 From: DREMN2112 Date: Thu, 7 Nov 2024 17:00:29 +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 | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/function.py b/function.py index 0b2a294..4c21829 100644 --- a/function.py +++ b/function.py @@ -56,12 +56,9 @@ def predict_logreg(inX, weights): # 将概率转为标签 return 1 if prob >= 0.5 else 0 -import numpy as np -from sklearn.preprocessing import StandardScaler - def load_data(filepath): """ - 从文件中加载数据,处理缺失数据,并将 '?' 替换为列的均值或中位数。 + 从文件中加载数据,处理缺失数据,并将 '?' 替换为列的中位数。 """ data = [] labels = [] @@ -92,9 +89,10 @@ def load_data(filepath): for i in range(data.shape[1]): data[:, i] = np.where(np.isnan(data[:, i]), col_medians[i], data[:, i]) - # 对数据进行标准化 - scaler = StandardScaler() - data = scaler.fit_transform(data) + # 对数据进行标准化 (均值为 0,标准差为 1) + means = np.mean(data, axis=0) + stds = np.std(data, axis=0) + data = (data - means) / stds return data, labels