diff --git a/function.py b/function.py index 4211bad..a00a9fa 100644 --- a/function.py +++ b/function.py @@ -9,7 +9,7 @@ def load_data(filepath): data = [] with open(filepath, 'r') as file: for line in file: - line_data = line.strip().split(',') + line_data = line.strip().split() if line_data: try: data.append([float(x) if x != '?' else np.nan for x in line_data]) @@ -31,7 +31,7 @@ def preprocess_data(X): def train_logreg(X, y): X_processed = preprocess_data(X) # 预处理训练数据 - model = RandomForestClassifier(n_estimators=113, max_depth=10, random_state=42) + model = RandomForestClassifier(n_estimators=200, max_depth=20, random_state=42) model.fit(X_processed, y) return model # 返回训练好的模型 @@ -43,19 +43,4 @@ def predict_logreg(X, model): return model.predict(X_processed)[0] # 确保 model 是训练好的模型 if __name__ == "__main__": - # 加载训练集 - X_train, y_train = load_data("./data/horse-colic.data") - - # 加载测试集 - X_test, y_test = load_data("./data/horse-colic.test") - - # 训练对率回归模型 - model = train_logreg(X_train, y_train) - - # 确保测试集中有数据 - if X_test.shape[0] > 0: - # 预测测试集的第一个样本 - y_pred = predict_logreg(X_test[0], model) - print(f"预测结果: {y_pred}, 实际结果: {y_test[0]}") - else: - print("测试集没有数据,无法进行预测。") + X, y = load_data("./data/horse-colic.data")