I am working on LULC classification of Sentinel-2 image(4-bands) using Machine Learning algorithms. I split the whole image into 64*64 patches and assigned class label (builtup, barren land, water and vegetation) to selected patches.

For each class patch spectral features (mean, variance values of each band, mean normalized difference vegetation index(NDVI) for each class patch, texture features (grey-level co-occurrence matrix (GLCM) contrast, homogeneity, dissimilarity and correlation for NIR band of each class patch were calculated. I split the features into training and testing dataset and trained a random forest classifier on the training dataset. The problem is I don't know how to classify the whole image using a trained classifier and visualize it as a classified image.

import numpy as np 
import pandas as pd 
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
from sklearn.metrics import confusion_matrix
train = pd.read_excel(r'D:\band_composite\traning_data\training.xls')#import traing dataset
test=pd.read_excel(r"D:\band_composite\test_data\test.xls")#import testing daatset
# (Xtrain,Ytrain for training dataset)
X_train = train.drop(['class'], axis=1)
y_train = pd.DataFrame(train['class'].values)
X_test = test.drop(['class'], axis=1)
y_test = test['class']
# Random forest model training
clf = RandomForestClassifier(max_depth=2, random_state=0)
model=clf.fit(X_train, y_train)

# X_train shape
    (141, 15)
# y_train.shape
    (141, 1)

#X_training dataset excel image

enter image description here

  • I would do some digging into the specific model that you are using and how to deploy it. Your step of creating tiles is more for a deep learning model and is not necessary with random forests. Then taking moments from the tiles really mucks thigs up. You should be using a point sample or segmentation approach. The premise of random forest is that the recursive partitioning model is unconstrained and allowed to grow to its maximum depth. Using max_depth=2 would notably constraint it and yield incorrect results. You really must understand the specified model. Commented Jan 3, 2023 at 23:13

1 Answer 1


I think this may help you (https://github.com/florianbeyer/RandomForest-Classification/blob/master/Classification_script.ipynb)

  • Please do not provide "link only" answers. They are only relevant for the life of the URL. Commented Jan 3, 2023 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.