From d427c23869b90500f3ffc40f9b4784b2b9261e06 Mon Sep 17 00:00:00 2001 From: wzy <675163717@qq.com> Date: Thu, 31 Oct 2024 20:26:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 51 +++++++++++++++------------------------------------ 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/function.py b/function.py index 8d51b11..4b5f68a 100644 --- a/function.py +++ b/function.py @@ -1,7 +1,7 @@ # function.py import numpy as np import random -import pandas as pd +from sklearn.preprocessing import StandardScaler """ 实验说明: @@ -28,8 +28,8 @@ name = "万子玉" # 这里写自己的代码实现 -def sigmoid(inx): - return 1 / (1 + np.exp(-inx)) +def sigmoid(z): + return 1 / (1 + np.exp(-z)) def train_logreg(X, y, learning_rate=0.001, iterations=500, batch_size=16, initial_weights=None): X = np.insert(X, 0, 1, axis=1) # 添加偏置项 @@ -56,6 +56,7 @@ def train_logreg(X, y, learning_rate=0.001, iterations=500, batch_size=16, initi weights -= learning_rate * gradient return weights + """ 对率回归训练函数。 @@ -92,16 +93,13 @@ def evaluate_model(X_val, y_val, weights): return accuracy def split_train_val(X, y, val_size=0.2): - # 确保数据是随机打乱的 shuffled_indices = list(range(X.shape[0])) random.shuffle(shuffled_indices) X_shuffled = X[shuffled_indices] y_shuffled = y[shuffled_indices] - # 计算验证集的大小 val_count = int(X.shape[0] * val_size) - # 分割数据集 X_val = X_shuffled[:val_count] y_val = y_shuffled[:val_count] X_train = X_shuffled[val_count:] @@ -110,16 +108,17 @@ def split_train_val(X, y, val_size=0.2): return X_train, X_val, y_train, y_val + # 这里也写自己的代码实现 def load_data(filepath): with open(filepath, 'r') as file: lines = file.readlines() - data = [line.strip().split() for line in lines] + data = [line.strip().split() for line in lines if line.strip().split()[-1].isdigit()] data = [list(map(lambda x: float(x) if x != '?' else np.nan, row)) for row in data] # 移除类别标签缺失的样本 - data = [row for row in data if row[-1] != np.nan] + data = [row for row in data if not np.isnan(row[-1])] # 使用“0”填充缺失值 for row in data: @@ -129,34 +128,20 @@ def load_data(filepath): # 合并特征23的值2和3为0 for row in data: - if row[22] == 2 or row == 3: # 假设特征23是第23个元素 + if row[22] == 2 or row[22] == 3: # 假设特征23是第23个元素 row[22] = 0 # 移除不需要的特征 - # 假设特征从0开始编号,那么我们需要移除特征3和特征24~28 data = [row[:2] + row[3:23] + row[28:] for row in data] # 分离特征和标签 - X = [row[:-1] for row in data] # 特征现在是从0到21,然后是23到27 + X = [row[:-1] for row in data] y = [row[-1] for row in data] - return np.array(X), np.array(y) + scaler = StandardScaler() + X = scaler.fit_transform(X) - data = pd.read_csv(filepath, header=None, delimiter=' ') - return data - -# 标准化函数 -def regularize(xMat): - inMat = xMat.copy() - inMeans = np.mean(inMat, axis=0) - inVar = np.std(inMat, axis=0) - inMat = (inMat - inMeans) / inVar - return inMat - -def classify(inx, weights): - p = sigmoid(np.dot(inx, weights)) - return 1 if p >= 0.5 else 0 - + return np.array(X), np.array(y) """ 从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。 @@ -169,16 +154,10 @@ def classify(inx, weights): """ 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) - print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") + print(f"预测结果: {y_pred}, 实际结果: {y_test[11]}") \ No newline at end of file