<jupyter_start><jupyter_text>XG Boost<jupyter_code>import numpy as np
import pandas as pd<jupyter_output><empty_output><jupyter_text>Part 1 - Data Preprocessing  Data Loading<jupyter_code>PATH = "../../../Model_Selection_Boosting/XGBoost/Python/Churn_Modelling.csv"
dataset = pd.read_csv(PATH)
dataset.head()
X = dataset.iloc[:, 3:-1].values
y = dataset.iloc[:, -1].values<jupyter_output><empty_output><jupyter_text>Encoding the Categorical Variables<jupyter_code>from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
X[:, 2] = le.fit_transform(X[:, 2])
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder

ct = ColumnTransformer(transformers=[('encoder', 
                                      OneHotEncoder(), [1])], 
                       remainder='passthrough')
X = np.array(ct.fit_transform(X))<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, 
                                                    test_size=0.20, 
                                                    random_state=42)<jupyter_output><empty_output><jupyter_text>Part 2 - Model<jupyter_code>from xgboost import XGBClassifier

classifier = XGBClassifier()
classifier.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Part 3 - Model Evaluation  Prediction<jupyter_code>sample = [[600, 'France', 'Male', 40, 3, 60000, 2, 1, 1, 50000]]

sample = np.array(sample)
sample[:, 2] = le.transform(sample[:, 2])
sample = np.array(ct.transform(sample))
print(classifier.predict(sample))
y_pred = classifier.predict(X_test)
print(np.concatenate((y_test.reshape(len(y_pred),1), y_pred.reshape(len(y_pred),1)), axis=1))<jupyter_output>[[0 0]
 [0 0]
 [0 0]
 ...
 [1 1]
 [1 0]
 [1 0]]<jupyter_text>K-Fold Cross Validation<jupyter_code>from sklearn.model_selection import cross_val_score

accuracies = cross_val_score(estimator=classifier, X=X_train, 
                             y=y_train, cv=10, n_jobs=-1)
print("Mean \t\t\t%.2f" % accuracies.mean())
print("Standard Deviation \t%.2f" % accuracies.std())<jupyter_output>Mean 			0.85
Standard Deviation 	0.01<jupyter_text>Confusion Matrix<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sn
from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_test, y_pred)
df_cm = pd.DataFrame(cm, range(len(cm[0])), range(len(cm[0])))
sn.set(font_scale=1.4)
sn.heatmap(df_cm, annot=True, annot_kws={"size": 16})
plt.show()<jupyter_output><empty_output><jupyter_text>Accuracy Score<jupyter_code>from sklearn.metrics import accuracy_score

accuracy_score(y_test, y_pred)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup