上传文件至 /
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 29s

This commit is contained in:
2024-11-03 16:57:52 +08:00
parent 7437d4289a
commit 817111606b
+3 -18
View File
@@ -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")