diff --git a/function.py b/function.py index 0d6661c..fd9082c 100644 --- a/function.py +++ b/function.py @@ -1,57 +1,79 @@ -import numpy as np - -id = "202210423105" -name = "魏明帅" - -def train_logreg(X, y, learning_rate=0.01, iterations=1000): - m, n = X.shape - # 添加偏置项 - X = np.c_[np.ones((m, 1)), X] - weights = np.zeros(n + 1) - - for _ in range(iterations): - for i in range(m): - x_i = X[i] - y_i = y[i] - z = np.dot(x_i, weights) - h = 1 / (1 + np.exp(-z)) - error = y_i - h - weights += learning_rate * error * x_i - - return weights - -def predict_logreg(inX, weights): - z = np.dot(inX, weights) - h = 1 / (1 + np.exp(-z)) - return 1 if h >= 0.5 else 0 - -def load_data(filepath): - X = [] - y = [] - with open(filepath, 'r') as f: - for line in f: - values = line.strip().split() - # 跳过包含 '?' 的行 - if '?' in values: - continue - # 将标签和特征转换为浮点数 - y.append(float(values[-1])) - X.append([float(x) for x in values[:-1]]) - - X_array = np.array(X) - y_array = np.array(y).reshape(-1, 1) - - # 特征缩放,处理标准差为零的特征 - means = np.mean(X_array, axis=0) - stds = np.std(X_array, axis=0) - X_scaled = (X_array - means) / np.where(stds == 0, 1, stds) - - return X_scaled, y_array - -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, learning_rate=0.01, iterations=1000) - y_pred = predict_logreg(X_test[11], weights) +import numpy as np + +# 填写学号和姓名 +id = "202210423105" +name = "魏明帅" + +def sigmoid(z): + return 1 / (1 + np.exp(-z)) + +def train_logreg(X, y, learning_rate=0.01, iterations=1000): + """ + 对率回归训练函数。 + + Args: + - X (numpy array): 特征数据。 + - y (numpy array): 标签数据。 + - learning_rate (float): 学习率。 + - iterations (int): 迭代次数。 + + Returns: + - numpy array: 训练得到的权重。 + """ + m, n = X.shape + weights = np.zeros(n) + bias = 0 + + for i in range(iterations): + h = sigmoid(np.dot(X, weights) + bias) + error = h - y + weights -= learning_rate * (1 / m) * np.dot(X.T, error) + bias -= learning_rate * (1 / m) * np.sum(error) + + return weights, bias + +def predict_logreg(inX, weights, bias): + """ + 对率回归预测函数。 + + Args: + - inX (numpy array): 输入待分类的样本。 + - weights (numpy array): 训练得到的权重。 + - bias (float): 训练得到的偏置项。 + + Returns: + - int: 预测的分类标签。 + """ + prob = sigmoid(np.dot(inX, weights) + bias) + return 1 if prob > 0.5 else 0 + +def load_data(filepath): + """ + 从文件中加载数据,若不自行正确实现,则无法通过评测,不记录成绩。 + + Args: + - filepath (str): 数据文件路径。 + + Returns: + - np.array: 特征数据。 + - np.array: 标签数据。 + """ + data = np.loadtxt(filepath, delimiter=',') + X = data[:, :-1] + y = data[:, -1] + y = (y > 0).astype(int) # 将标签转换为1和0 + return X, 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, bias = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000) + + # 测试单个数据 + y_pred = predict_logreg(X_test[11], weights, bias) print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") \ No newline at end of file