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

This commit is contained in:
2024-11-02 11:48:28 +08:00
parent 76ae8ac01a
commit 3c0478ec60
+12 -11
View File
@@ -1,14 +1,16 @@
# function.py
import numpy as np
from sklearn.linear_model import LogisticRegression
# 学号和姓名
id = "202210423230"
name = "尹家立"
# function.py
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.impute import KNNImputer
def train_logreg(X, y, max_iter=1000):
"""
使用liblinear求解器实现逻辑回归训练函数 (梯度上升)。
使用liblinear求解器实现逻辑回归训练函数。
Args:
- X (numpy array): 特征数据。
@@ -48,7 +50,7 @@ def sigmoid(z):
def load_data(filepath):
"""
从文件中加载数据。
从文件中加载数据并使用KNNImputer对缺失数据进行预测填充。
Args:
- filepath (str): 数据文件路径。
@@ -73,10 +75,9 @@ def load_data(filepath):
X = data[:, :-1]
y = data[:, -1]
# 填充缺失值
col_mean = np.nanmean(X, axis=0)
inds = np.where(np.isnan(X))
X[inds] = np.take(col_mean, inds[1])
# 使用 KNNImputer 对缺失值进行预测填充
imputer = KNNImputer(n_neighbors=5)
X = imputer.fit_transform(X)
return X, y.astype(int)
@@ -86,7 +87,7 @@ if __name__ == "__main__":
X_test, y_test = load_data("./data/horse-colic.test")
# 使用liblinear训练对率回归模型
weights = train_logreg(X_train, y_train, max_iter=5000)
weights = train_logreg(X_train, y_train, max_iter=1000)
# 测试单个样本的预测
y_pred = predict_logreg(X_test[11], weights)