diff --git a/function.py b/function.py index 486431b..4e765df 100644 --- a/function.py +++ b/function.py @@ -32,10 +32,9 @@ from sklearn.preprocessing import StandardScaler def sigmoid(intX): return 1.0 / (1 + np.exp(-intX)) - def train_logreg(X, y, numIter=150): m, n = X.shape - weights = np.ones(n) + weights = np.zeros(n) # 初始化权重为0而不是1 for j in range(numIter): dataIndex = list(range(m)) for i in range(m): @@ -43,11 +42,10 @@ def train_logreg(X, y, numIter=150): randIndex = int(np.random.uniform(0, len(dataIndex))) h = sigmoid(np.dot(X[randIndex], weights)) error = y[randIndex] - h - weights = weights + np.dot(alpha * error, X[randIndex]) + weights = weights + alpha * error * X[randIndex] del(dataIndex[randIndex]) return weights - def predict_logreg(X, weights): predictions = [] for x in X: @@ -55,7 +53,6 @@ def predict_logreg(X, weights): predictions.append(1 if prob > 0.5 else 0) return predictions - def load_data(filepath): data = [] labels = [] @@ -70,29 +67,19 @@ def load_data(filepath): data = np.array(data) labels = np.array(labels) target_variable = labels - target_variable[(target_variable == 2) | (target_variable == 3)] = 0 + target_variable[np.logical_or(target_variable == 2, target_variable == 3)] = 0 data = np.delete(data, [2] + list(range(23, 28)), axis=1) scaler = StandardScaler() data = scaler.fit_transform(data) return data, target_variable - def accuracy(X, y, weights): predictions = predict_logreg(X, weights) return np.mean(predictions == y) - - # 以下代码可用于测试,可自行随意修改 if __name__ == '__main__': - # 加载训练集 - X_train, y_train = load_data("./data/horse-colic.data") - - # 加载测试集 - X_test, y_test = load_data("./data/horse-colic.test") - - # 训练对率回归模型 - weights = train_logreg(X_train, y_train) - - # 测试单个数据 - y_pred = predict_logreg(X_test[11], weights) - print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") + X_train, y_train = load_data("./horse-colic.data") + X_test, y_test = load_data("./horse-colic.test") + final_weights = train_logreg(X_train, y_train) + final_accuracy = accuracy(X_test, y_test, final_weights) + print(f"最终测试集的准确率: {final_accuracy:.4f}")