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

This commit is contained in:
2024-10-29 08:48:17 +08:00
parent 3ceac3241d
commit 9f3c495776
+10 -15
View File
@@ -51,19 +51,10 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, regularization=0.0):
return 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
# 应用sigmoid函数(如果需要的话)
# 如果需要sigmoid输出,可以添加下面这行
# sigmoid_output = 1 / (1 + np.exp(-linear_output))
# 这里可以返回线性输出或sigmoid输出,取决于您的需求
# return sigmoid_output
return linear_output
# 这里也写自己的代码实现
@@ -80,10 +71,14 @@ def load_data(filepath):
y = data_array[:, -1]
return X, y
def evaluate_model(X, y, weights, bias):
predictions = [predict_logreg(x, weights, bias) for x in X]
accuracy = np.mean(predictions == y)
return accuracy
def evaluate_model(test_data, weights, bias, user_test_func=predict_logreg):
results = []
for i in range(len(test_data)):
# 确保传递了正确的参数:test_data[i], weights, bias
result = user_test_func(test_data[i], weights, bias)
results.append(result)
# 根据需要对结果进行进一步处理,例如计算准确率等
return results
if __name__ == "__main__":
X_train, y_train = load_data("./data/horse-colic.data")