generated from LufsX/Experiment-LogReg
This commit is contained in:
+10
-15
@@ -51,19 +51,10 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.0):
|
|||||||
return weights, bias
|
return weights, bias
|
||||||
|
|
||||||
def predict_logreg(inX, weights, bias):
|
def predict_logreg(inX, weights, bias):
|
||||||
# 确保 inX 是一维数组
|
|
||||||
if inX.ndim == 2 and inX.shape[1] == 1:
|
|
||||||
inX = inX.flatten()
|
|
||||||
elif inX.ndim > 1:
|
|
||||||
raise ValueError("Input data should be one-dimensional or a column vector.")
|
|
||||||
|
|
||||||
# 计算线性输出
|
|
||||||
linear_output = np.dot(inX, weights) + bias
|
linear_output = np.dot(inX, weights) + bias
|
||||||
|
# 如果需要sigmoid输出,可以添加下面这行
|
||||||
# 应用sigmoid函数(如果需要的话)
|
|
||||||
# sigmoid_output = 1 / (1 + np.exp(-linear_output))
|
# sigmoid_output = 1 / (1 + np.exp(-linear_output))
|
||||||
|
# return sigmoid_output
|
||||||
# 这里可以返回线性输出或sigmoid输出,取决于您的需求
|
|
||||||
return linear_output
|
return linear_output
|
||||||
|
|
||||||
# 这里也写自己的代码实现
|
# 这里也写自己的代码实现
|
||||||
@@ -80,10 +71,14 @@ def load_data(filepath):
|
|||||||
y = data_array[:, -1]
|
y = data_array[:, -1]
|
||||||
return X, y
|
return X, y
|
||||||
|
|
||||||
def evaluate_model(X, y, weights, bias):
|
def evaluate_model(test_data, weights, bias, user_test_func=predict_logreg):
|
||||||
predictions = [predict_logreg(x, weights, bias) for x in X]
|
results = []
|
||||||
accuracy = np.mean(predictions == y)
|
for i in range(len(test_data)):
|
||||||
return accuracy
|
# 确保传递了正确的参数:test_data[i], weights, bias
|
||||||
|
result = user_test_func(test_data[i], weights, bias)
|
||||||
|
results.append(result)
|
||||||
|
# 根据需要对结果进行进一步处理,例如计算准确率等
|
||||||
|
return results
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
X_train, y_train = load_data("./data/horse-colic.data")
|
X_train, y_train = load_data("./data/horse-colic.data")
|
||||||
|
|||||||
Reference in New Issue
Block a user