import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn import preprocessing
from sklearn import metrics
import xgboost
from sklearn import tree
from sklearn import ensemble
Pravimo XGBoost i AdaBoost klasifikatore na skupu diabetes.
data = pd.read_csv('diabetes.csv')
y = data['Outcome']
X = data.drop(columns=['Outcome'], axis=1)
X_train, X_test, y_train, y_test = model_selection.train_test_split(
X, y, test_size=0.3, stratify=y, random_state = 7)
scaler = preprocessing.StandardScaler()
scaler.fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
model_xgboost = xgboost.XGBClassifier(n_estimators=100, max_depth=5)
model_adaboost = ensemble.AdaBoostClassifier(
base_estimator=tree.DecisionTreeClassifier(max_depth=5),
n_estimators=100, random_state=7)
model_xgboost.fit(X_train, y_train);
model_adaboost.fit(X_train, y_train);
y_predicted1 = model_xgboost.predict(X_test)
metrics.accuracy_score(y_test, y_predicted1)
0.7359307359307359
metrics.f1_score(y_test, y_predicted1)
0.6013071895424836
y_predicted2 = model_adaboost.predict(X_test)
metrics.accuracy_score(y_test, y_predicted2)
0.7532467532467533
metrics.f1_score(y_test, y_predicted2)
0.6174496644295301
U ovom slučaju je AdaBoost dao bolje rezultate. Pokušajmo sa 500 stabala.
model_xgboost = xgboost.XGBClassifier(n_estimators=500, max_depth=5)
model_adaboost = ensemble.AdaBoostClassifier(
base_estimator=tree.DecisionTreeClassifier(max_depth=5), n_estimators=500, random_state=7)
model_xgboost.fit(X_train, y_train);
model_adaboost.fit(X_train, y_train);
y_predicted1 = model_xgboost.predict(X_test)
metrics.accuracy_score(y_test, y_predicted1)
0.7186147186147186
metrics.f1_score(y_test, y_predicted1)
0.5695364238410596
y_predicted2 = model_adaboost.predict(X_test)
metrics.accuracy_score(y_test, y_predicted2)
0.7445887445887446
metrics.f1_score(y_test, y_predicted2)
0.6040268456375839
Oba modela su malo lošija nego sa 100 stabala, ali je AdaBoost i dalje bolji.