<jupyter_start><jupyter_text>Simple Image Classification using XGBoost This Code template is for Simple Image Classification using XGBoost.

  Required packages<jupyter_code>import warnings
warnings.filterwarnings("ignore")
import xgboost as xgb
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import os
import random
from sklearn.model_selection import train_test_split
from sklearn.metrics import plot_confusion_matrix, classification_report
from sklearn import metrics<jupyter_output><empty_output><jupyter_text>InitializationPath of the main folder containing image dataset files<jupyter_code>#load the dataset
data = ''<jupyter_output><empty_output><jupyter_text>List of target categories which are required for model training and predictions.<jupyter_code>target = []<jupyter_output><empty_output><jupyter_text>Data FetchingThe OS module in Python provides functions for interacting with the operating system. This module provides a portable way of using operating system-dependent functionality. OpenCV-Python is a library of Python bindings designed to solve computer vision problems.We will use the "**os.path**" module to interact with the file system and the "**cv2.imread()**" method to load an image from the specified file path.<jupyter_code># Loading the image dataset and plotting some images from all target categories
def plot_image(data, target):
  count = 0
  for category in target:
      path=os.path.join(data, category)
      flg = 0
      ar = []
      # storing image paths in an array
      for img in os.listdir(path):
          ar.append(os.path.join(path,img)) 
          flg+= 1
          if flg==2:
            break
      # plotting the images in dataset
      plt.figure(figsize=(5,5))
      for i in range(2):
        d1 = ar[i]
        img_array = cv2.imread(d1,cv2.IMREAD_COLOR)
        img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)
        ax = plt.subplot(1,2,i+1)
        plt.imshow(img_array)
        plt.title(category)
        plt.axis("off")
      count+= 1
      if count == len(target):
        break
plot_image(data, target)<jupyter_output><empty_output><jupyter_text>Data PreprocessingSince we are working with image data we need to preprocess our data because the machine learning model cannot work directly with images. So the images are converted into a format that our machine learning algorithm can understand. Image data is represented as a matrix, where the depth is the number of channels. An RGB image has three channels (red, green, and blue) and we create a function that firstly resizes all the images so that the model receives inputs of the same size and then stores them as an array of RGB values.The different categories of images are also read and labeled (as 0,1,2....). We finally append the images and their labelled category into a single list that acts as our final preprocessed dataset.<jupyter_code>def create_training_data(data,target):
    training_data=[]
    img_size = 50
    for category in target:
        path=os.path.join(data, category)
        class_num=target.index(category)
        for img in os.listdir(path):
            try:
                img_array=cv2.imread(os.path.join(path,img))
                new_array=cv2.resize(img_array,(img_size,img_size))
                training_data.append([new_array,class_num])
            except Exception as e:
                pass
    return training_data<jupyter_output><empty_output><jupyter_text>Calling preprocessing function on our actual image data<jupyter_code>training_data = create_training_data(data,target) 
df = pd.DataFrame(training_data, columns=['image', 'label'])
df.head()<jupyter_output><empty_output><jupyter_text>Feature SelectionsAfter preprocessing our data into a single array, we now need to provide the input and the target features for our model. Also the XGBClassifier training model accepts dimension <=2 so we need to convert the image data to 2D by converting our features into numpy arrays and then flattening our input feature X.<jupyter_code>lenofimage = len(training_data)
X, y = [], []
for categories, label in training_data:
    X.append(categories)
    y.append(label)
# Flattening our training feature
X = np.array(X).reshape(lenofimage,-1)
y = np.array(y)<jupyter_output><empty_output><jupyter_text>Data SplittingThe train-test split is a procedure for evaluating the performance of an algorithm. The procedure involves taking a dataset and dividing it into two subsets. The first subset is utilized to fit/train the model. The second subset is used for prediction. The main motive is to estimate the performance of the model on new data.<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=123)<jupyter_output><empty_output><jupyter_text>ModelXGBoost provides a wrapper class to allow models to be treated like classifiers or regressors in the scikit-learn framework.This means we can use the full scikit-learn library with XGBoost models.The XGBoost model for classification is called XGBClassifier. We can create and and fit it to our training dataset. Models are fit using the scikit-learn API and the model.fit() function. Model Tuning Parameters> - n_estimators -> Number of boosting rounds.> - max_depth -> Maximum tree depth for base learners.> - learning_rate -> Boosting learning rate (xgb’s “eta”)> - booster -> Specify which booster to use: gbtree, gblinear or dart.Read more at [XGBoost Python scikit-learn API](https://xgboost.readthedocs.io/en/latest/python/python_api.htmlmodule-xgboost.sklearn)<jupyter_code>model = xgb.XGBClassifier()
model.fit(X_train, y_train)<jupyter_output>[10:03:07] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.4.0/src/learner.cc:1095: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.<jupyter_text>Model Accuracyscore() method return the mean accuracy on the given test data and labels.In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.<jupyter_code>y_pred = model.predict(X_test)
print("Accuracy score {:.2f} %\n".format(model.score(X_test,y_test)*100))<jupyter_output>Accuracy score 62.88 %<jupyter_text>Confusion MatrixA confusion matrix is utilized to understand the performance of the classification model or algorithm in machine learning for a given test set where results are known.<jupyter_code>plot_confusion_matrix(model,X_test,y_test,cmap=plt.cm.Blues)<jupyter_output><empty_output><jupyter_text>Classification ReportA Classification report is used to measure the quality of predictions from a classification algorithm. How many predictions are True, how many are False.* **where**:    - Precision:- Accuracy of positive predictions.    - Recall:- Fraction of positives that were correctly identified.    - f1-score:-  percent of positive predictions were correct    - support:- Support is the number of actual occurrences of the class in the specified dataset.<jupyter_code>print(classification_report(y_test,model.predict(X_test)))<jupyter_output>precision    recall  f1-score   support

           0       0.63      0.64      0.63       402
           1       0.63      0.62      0.62       398

    accuracy                           0.63       800
   macro avg       0.63      0.63      0.63       800
weighted avg       0.63      0.63      0.63       800
{'fraction_non_alphanumeric': 0.07174947145877378, 'fraction_numerical': 0.015724101479915435, 'mean_word_length': 4.103843560350641, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 54, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '302649', 'license': 'Apache-2.0', 'n_tokens_mistral': 2230, 'n_tokens_neox': 2128, 'n_words': 798, 'path': 'Audio Visual/Problems/ImageClassificaitonXGBoost.ipynb', 'repo_name': 'shreepad-nade/ds-seed', 'size': '7568'}