I am new one on bagging model. And I refer the example bagging code by using my data set. I prepare the array train_images, train_labels, test_images, test_labels. Then I use the prepossing.LabelEncoder() to the label.
print(train_labels)
print(test_labels)
Here is some code for preprocessing.LabelEncoder:
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(test_labels)
list(le.classes_)
test_labels_encoded = le.transform(test_labels)
le.fit(train_labels)
train_labels_encoded = le.transform(train_labels)
here is the classes result:
Then I start to do the bagging function & predict.
x_train, y_train, x_test, y_test = train_images, train_labels_encoded, test_images, test_labels_encoded
from sklearn import tree
clf=tree.DecisionTreeClassifier()
bagging=BaggingClassifier(base_estimator=clf,n_estimators=10, bootstrap=True,bootstrap_features=True,max_features=3,max_samples=0.7)
x_train = (x_train.reshape(x_train.shape[0], x_train.shape[1] * x_train.shape[2] * x_train.shape[3]))
bagging.fit(x_train,y_train)
x_test = (x_test.reshape(x_test.shape[0], x_test.shape[1] * x_test.shape[2] * x_test.shape[3]))
bagging.predict(x_test)
bagging.score(x_test,y_test)
the test predict accuracy is almost 97%. but when I use my new pics to predict
sample_images = []
for directory_path in glob.glob("/root/data_Camera/mixclasses_15/test3/*"):
#print("1")
for img_path in glob.glob(os.path.join(directory_path, "*.png")):
#print("2")
print(img_path)
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = cv2.resize(img, (SIZE, SIZE))
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
sample_images.append(img)
sample_images = np.array(sample_images)
sample_images = (sample_images.reshape(sample_images.shape[0], sample_images.shape[1] * sample_images.shape[2] * sample_images.shape[3]))
bagging.predict(sample_images)
le.inverse_transform(bagging.predict(sample_images).all())
the bagging.predict(sample_images) shows me the array, and after le.inverse_transform only shows me two item. why? I can't understand. I think it need to feedback to me 9 items. because I input 9 sample pics. and the sample arrary will be:
And according to the bagging.predict(sample_images) result, the label is :
5887B,3820B,4585B,3993B,5176B,7090B,4864B,5176B,4585B
Do my understand correct? Many thanks!

