Decision Tree _ Bagging Classifier simple example
2023. 2. 21. 05:41ㆍData science/Python
반응형
# Instantiate the base model
clf_dt = DecisionTreeClassifier(max_depth=4)
# Build and train the Bagging classifier
clf_bag = BaggingClassifier(
n_estimators=21,
base_estimator=clf_dt,
random_state=500)
clf_bag.fit(X_train, y_train)
# Predict the labels of the test set
pred = clf_bag.predict(X_test)
# Show the F1-score
print('F1-Score: {:.3f}'.format(f1_score(y_test, pred)))
# Build and train the bagging classifier
clf_bag = BaggingClassifier(
base_estimator=clf_dt,
n_estimators=21,
oob_score=True,
random_state=500)
clf_bag.fit(X_train, y_train)
# Print the out-of-bag score
print('OOB-Score: {:.3f}'.format(clf_bag.oob_score_))
# Evaluate the performance on the test set to compare
pred = clf_bag.predict(X_test)
print('Accuracy: {:.3f}'.format(accuracy_score(y_test, pred)))
반응형
'Data science > Python' 카테고리의 다른 글
How to print python file (0) | 2023.02.20 |
---|---|
Python - np.empty vs. np.zeros (0) | 2023.02.15 |
Python Semester 1. (0) | 2023.01.15 |
[IBM] Data Analysis with Python - Model Development (0) | 2021.05.17 |
[IBM]Data Analysis with Python - Exploratory Data Analysis(EDA) (0) | 2021.05.15 |