generated from LufsX/Experiment-LogReg
This commit is contained in:
+4
-4
@@ -30,12 +30,12 @@ def preprocess_data(X):
|
|||||||
X_poly = poly.fit_transform(X_standardized)
|
X_poly = poly.fit_transform(X_standardized)
|
||||||
return X_poly
|
return X_poly
|
||||||
|
|
||||||
def train_random_forest(X, y):
|
def train_logreg(X, y):
|
||||||
model = RandomForestClassifier(n_estimators=113, max_depth=10, random_state=42)
|
model = RandomForestClassifier(n_estimators=113, max_depth=10, random_state=42)
|
||||||
model.fit(X, y)
|
model.fit(X, y)
|
||||||
return model
|
return model
|
||||||
|
|
||||||
def predict(model, X):
|
def predict_logreg(model, X):
|
||||||
return model.predict(X)
|
return model.predict(X)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
@@ -51,14 +51,14 @@ if __name__ == "__main__":
|
|||||||
X_test_processed = preprocess_data(X_test)
|
X_test_processed = preprocess_data(X_test)
|
||||||
|
|
||||||
# 训练模型
|
# 训练模型
|
||||||
model = train_random_forest(X_train_processed, y_train)
|
model = train_logreg(X_train_processed, y_train)
|
||||||
|
|
||||||
# 交叉验证
|
# 交叉验证
|
||||||
cv_scores = cross_val_score(model, X_train_processed, y_train, cv=5)
|
cv_scores = cross_val_score(model, X_train_processed, y_train, cv=5)
|
||||||
print(f"交叉验证准确率: {np.mean(cv_scores) * 100:.2f}%")
|
print(f"交叉验证准确率: {np.mean(cv_scores) * 100:.2f}%")
|
||||||
|
|
||||||
# 预测
|
# 预测
|
||||||
y_pred = predict(model, X_test_processed)
|
y_pred = predict_logreg(model, X_test_processed)
|
||||||
|
|
||||||
# 计算正确率
|
# 计算正确率
|
||||||
accuracy = np.mean(y_pred == y_test)
|
accuracy = np.mean(y_pred == y_test)
|
||||||
|
|||||||
Reference in New Issue
Block a user