更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 1m23s

This commit is contained in:
2024-11-03 16:15:36 +08:00
parent 3dccf5168e
commit 5da642c2ec
+20 -27
View File
@@ -1,6 +1,7 @@
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.model_selection import GridSearchCV
id = "202210423104"
name = "张钦诚"
@@ -25,39 +26,31 @@ def load_data(filepath):
def preprocess_data(X):
scaler = StandardScaler()
X_standardized = scaler.fit_transform(X)
poly = PolynomialFeatures(degree=2, include_bias=False)
poly = PolynomialFeatures(degree=3, include_bias=False) # 尝试更高的次数
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
X_processed = preprocess_data(X) # 预处理训练数据
# 使用网格搜索调参
param_grid = {
'n_estimators': [100, 200, 300],
'max_depth': [5, 10, 15],
'class_weight': ['balanced', None] # 处理不平衡数据
}
model = RandomForestClassifier(random_state=42)
grid_search = GridSearchCV(model, param_grid, cv=5)
grid_search.fit(X_processed, y)
return grid_search.best_estimator_ # 返回最佳模型
def predict_logreg(model, X):
# 确保传入的是训练好的模型
X_processed = preprocess_data(X.reshape(1, -1)) # 将单个样本重塑并预处理
return model.predict(X_processed)[0] # 预测并返回结果
# 确保输入数据是一个 numpy 数组
if len(X.shape) == 1:
X = X.reshape(1, -1) # 保持输入数据的形状为 (1, n),符合模型的输入要求
X_processed = preprocess_data(X) # 预处理测试数据
return model.predict(X_processed)[0] # 确保 model 是训练好的模型
if __name__ == "__main__":
# 加载数据
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)
# 数据预处理
X_train_processed = preprocess_data(X_train)
X_test_processed = preprocess_data(X_test)
# 训练模型
model = train_logreg(X_train_processed, y_train)
# 预测
y_pred = []
for sample in X_test:
y_pred.append(predict_logreg(model, sample))
# 计算正确率
accuracy = np.mean(y_pred == y_test)
print(f"测试出的正确率: {accuracy * 100:.2f}%")