generated from LufsX/Experiment-LogReg
This commit is contained in:
+12
-11
@@ -1,14 +1,16 @@
|
|||||||
# function.py
|
|
||||||
import numpy as np
|
|
||||||
from sklearn.linear_model import LogisticRegression
|
|
||||||
|
|
||||||
# 学号和姓名
|
# 学号和姓名
|
||||||
id = "202210423230"
|
id = "202210423230"
|
||||||
name = "尹家立"
|
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):
|
def train_logreg(X, y, max_iter=1000):
|
||||||
"""
|
"""
|
||||||
使用liblinear求解器实现逻辑回归训练函数 (梯度上升)。
|
使用liblinear求解器实现逻辑回归训练函数。
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
- X (numpy array): 特征数据。
|
- X (numpy array): 特征数据。
|
||||||
@@ -48,7 +50,7 @@ def sigmoid(z):
|
|||||||
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
"""
|
"""
|
||||||
从文件中加载数据。
|
从文件中加载数据并使用KNNImputer对缺失数据进行预测填充。
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
- filepath (str): 数据文件路径。
|
- filepath (str): 数据文件路径。
|
||||||
@@ -73,10 +75,9 @@ def load_data(filepath):
|
|||||||
X = data[:, :-1]
|
X = data[:, :-1]
|
||||||
y = data[:, -1]
|
y = data[:, -1]
|
||||||
|
|
||||||
# 填充缺失值
|
# 使用 KNNImputer 对缺失值进行预测填充
|
||||||
col_mean = np.nanmean(X, axis=0)
|
imputer = KNNImputer(n_neighbors=5)
|
||||||
inds = np.where(np.isnan(X))
|
X = imputer.fit_transform(X)
|
||||||
X[inds] = np.take(col_mean, inds[1])
|
|
||||||
|
|
||||||
return X, y.astype(int)
|
return X, y.astype(int)
|
||||||
|
|
||||||
@@ -86,7 +87,7 @@ if __name__ == "__main__":
|
|||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
X_test, y_test = load_data("./data/horse-colic.test")
|
||||||
|
|
||||||
# 使用liblinear训练对率回归模型
|
# 使用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)
|
y_pred = predict_logreg(X_test[11], weights)
|
||||||
|
|||||||
Reference in New Issue
Block a user