XGboost is a boosting algorithm which uses gradient boosting and is a robust technique. Let’s learn to build XGboost classifier.
# First install xgboost from below link
# https://xgboost.readthedocs.io/en/latest/build.html
# Imports
from sklearn.datasets import load_iris
from xgboost import XGBClassifier
import pandas as pd
import numpy as np
# Load Data
iris = load_iris()
# Create a dataframe
df = pd.DataFrame(iris.data, columns = iris.feature_names)
df['target'] = iris.target
# Let's see a sample of created df
df.sample(frac=0.01)
sepal length (cm) | sepal width (cm) | petal length (cm) | petal width (cm) | target | |
---|---|---|---|---|---|
57 | 4.9 | 2.4 | 3.3 | 1.0 | 1 |
85 | 6.0 | 3.4 | 4.5 | 1.6 | 1 |
# Let's see target names
targets = iris.target_names
print(targets)
['setosa' 'versicolor' 'virginica']
# Prepare training data for building the model
X_train = df.drop(['target'], axis=1).values
y_train = df['target']
# Instantiate the model
cls = XGBClassifier()
# Train/Fit the model
cls.fit(X_train, y_train)
# Make prediction using the model
X_pred = [5.1, 3.2, 1.5, 0.5]
y_pred = cls.predict([X_pred])
print("Prediction is: {}".format(targets[y_pred]))
Prediction is: ['setosa']
That's how we Build XGboost classifier
That’s all for this mini tutorial. To sum it up, we learned how to Build XGboost classifier.
Hope it was easy, cool and simple to follow. Now it’s on you.
Related Resources:
- Build Random Forest classification model in Python Build Random Forest classifier Random forest is an ensemble technique which combines weak learners to build a strong classifier. #...
- Build Decision Tree classification model in Python Build Decision Tree classifier Build Decision tree model. It is a machine learning algorithm which creates a tree on the...
- Build Logistic Regression classifier model in Python Build Logistic Regression classifier Logistic regression is a linear classifier. Despite the name it is actually a classification algorithm. #...
- Build SVM Support Vector Machine model in Python Build SVM | support vector machine classifier SVM (Support Vector Machine) algorithm finds the hyperplane which is at max distance...
- Building Adaboost classifier model in Python Building Adaboost classifier model Adaboost is a boosting algorithm which combines weak learners into a strong classifier. Let’s learn building...
- Build K Nearest Neighbors classifier model in Python Build K Nearest Neighbors classifier K Nearest Neighbors also known as KNN takes max vote of nearest neighbors and predicts...
- Spam Classifier | Text Classification ML model Spam Classifier using Naive Bayes Spam classifier machine learning model is need of the hour as everyday we get thousands...
- Iris Dataset – A Detailed Tutorial Iris Dataset Iris Dataset is a part of sklearn library. Sklearn comes loaded with datasets to practice machine learning techniques...
- Save Machine Learning model to a file | Pickle Save model to file Save machine learning model so that it can be used again and again without having to...
- Bag of words model | NLP | scikit learn tokenizer Bag of words model Bag of words (bow) model is a way to preprocess text data for building machine learning...