From 203a237b56e9f763c67a6806c29d14f5175ea5a4 Mon Sep 17 00:00:00 2001 From: 202210423104 <1961532386@qq.com> Date: Sun, 3 Nov 2024 15:42:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/function.py b/function.py index d62464f..430ffb1 100644 --- a/function.py +++ b/function.py @@ -6,6 +6,7 @@ from sklearn.preprocessing import PolynomialFeatures, StandardScaler id = "202210423104" name = "张钦诚" + def load_data(filepath): data = [] with open(filepath, 'r') as file: @@ -18,7 +19,12 @@ def load_data(filepath): print(f"Error processing line: {line}. Error: {e}") data = np.array(data) data = np.nan_to_num(data, nan=np.nanmean(data, axis=0)) - return data + + # 将最后一列作为标签,其他列作为特征 + X = data[:, :-1] + y = data[:, -1] + return X, y + def preprocess_data(X): # 标准化特征 @@ -30,18 +36,19 @@ def preprocess_data(X): X_poly = poly.fit_transform(X_standardized) return X_poly + def train_logreg(X, y): model = RandomForestClassifier(n_estimators=113, max_depth=10, random_state=42) model.fit(X, y) return model + def predict_logreg(model, X): return model.predict(X) + if __name__ == "__main__": - data = load_data("./data/horse-colic.data") - X = data[:, :-1] - y = data[:, -1] + X, y = load_data("./data/horse-colic.data") # 数据集划分 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)