更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 28s

This commit is contained in:
2024-11-02 12:43:54 +08:00
parent f9818b1d36
commit cee14802ae
+18 -2
View File
@@ -55,6 +55,17 @@ def predict_logreg(inX, weights):
prediction_prob = sigmoid(z)
return 1 if prediction_prob >= 0.5 else 0
def handle_outliers(df, threshold=3):
"""
使用 Z 分数法检测并处理异常值(噪音)。
任何 Z 分数大于指定阈值的数值会被替换为列的均值。
"""
z_scores = np.abs((df - df.mean()) / df.std())
df[z_scores > threshold] = np.nan # 标记异常值为 NaN
df.fillna(df.mean(), inplace=True) # 均值填充
return df
def load_data(filepath):
data = []
labels = []
@@ -82,9 +93,14 @@ def load_data(filepath):
data = np.array(data)
labels = np.array(labels)
# 使用均值填充处理缺失值
# 转换成 DataFrame 以便进一步处理
df = pd.DataFrame(data)
df.fillna(df.mean(), inplace=True) # 均值填充
# 使用均值填充缺失值
df.fillna(df.mean(), inplace=True) # 初步均值填充
# 处理噪音数据
df = handle_outliers(df) # 检测并处理异常值
return df.to_numpy(), labels