generated from LufsX/Experiment-LogReg
This commit is contained in:
+5
-1
@@ -101,8 +101,12 @@ def predict_logreg(inX, weights):
|
|||||||
Returns:
|
Returns:
|
||||||
- int: 预测的分类标签。
|
- int: 预测的分类标签。
|
||||||
"""
|
"""
|
||||||
|
# 确保输入样本的特征数量与训练时一致
|
||||||
|
if inX.shape[0] != weights.shape[0] - 1:
|
||||||
|
raise ValueError("输入样本的特征数量与训练时不一致")
|
||||||
|
|
||||||
# 添加偏置项
|
# 添加偏置项
|
||||||
inX = np.c_[np.ones((1, 1)), inX]
|
inX = np.insert(inX, 0, 1, axis=1) # 在特征向量的开头插入偏置项
|
||||||
z = np.dot(inX, weights)
|
z = np.dot(inX, weights)
|
||||||
y_pred = 1 / (1 + np.exp(-z))
|
y_pred = 1 / (1 + np.exp(-z))
|
||||||
return 1 if y_pred > 0.5 else 0
|
return 1 if y_pred > 0.5 else 0
|
||||||
|
|||||||
Reference in New Issue
Block a user