diff --git a/function.py b/function.py index 963c095..4e2794c 100644 --- a/function.py +++ b/function.py @@ -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")