generated from LufsX/Experiment-LogReg
This commit is contained in:
+6
-6
@@ -21,16 +21,17 @@ def load_data(filepath):
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
data = np.genfromtxt(filepath, delimiter=' ', dtype=float, missing_values='?', filling_values=np.nan)
|
||||
nan_columns = np.isnan(data)
|
||||
# 填充缺失值为列均值
|
||||
col_means = np.nanmean(data, axis=0)
|
||||
data[nan_columns] = np.take(col_means, np.where(nan_columns)[1])
|
||||
inds = np.where(np.isnan(data))
|
||||
data[inds] = np.take(col_means, inds[1])
|
||||
|
||||
X = data[:, :-1]
|
||||
y = data[:, -1]
|
||||
return X, y
|
||||
|
||||
# 对率回归训练函数
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=500):
|
||||
"""
|
||||
对率回归训练函数。
|
||||
|
||||
@@ -47,6 +48,7 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
|
||||
X = np.c_[X, np.ones(n_samples)] # 添加偏置项
|
||||
weights = np.zeros(n_features + 1)
|
||||
|
||||
# 向量化梯度下降
|
||||
for _ in range(iterations):
|
||||
y_pred = sigmoid(np.dot(X, weights))
|
||||
gradient = np.dot(X.T, (y_pred - y)) / n_samples
|
||||
@@ -66,8 +68,6 @@ def predict_logreg(X, weights):
|
||||
Returns:
|
||||
- np.array: 预测的分类标签 (0 或 1)。
|
||||
"""
|
||||
if X.ndim == 1:
|
||||
X = X.reshape(1, -1)
|
||||
X = np.c_[X, np.ones(X.shape[0])]
|
||||
y_pred = sigmoid(np.dot(X, weights))
|
||||
return (y_pred >= 0.5).astype(int)
|
||||
@@ -78,7 +78,7 @@ if __name__ == "__main__":
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=500)
|
||||
|
||||
# 预测测试集并计算准确率
|
||||
predictions = predict_logreg(X_test, weights)
|
||||
|
||||
Reference in New Issue
Block a user