text_plain_output_1.png
from pathlib import Path

from pathlib import Path

from skimage.io import imread

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import classification_report

from tensorflow.keras.preprocessing import image

import cv2 as cv

import numpy as np

image_height = 128
image_width = 128
DATA = '/kaggle/input/malimg-dataset9010/dataset_9010/dataset_9010/malimg_dataset'

TRAIN_DATA = DATA + '/train'
VALIDATION_DATA = DATA + '/validation'

from pathlib import Path
train_image_dir = Path(TRAIN_DATA)
train_folders = [directory for directory in train_image_dir.iterdir() if directory.is_dir()]
classes = [fo.name for fo in train_folders]

from pathlib import Path
val_image_dir = Path(VALIDATION_DATA)
val_folders = [directory for directory in val_image_dir.iterdir() if directory.is_dir()]

from skimage.io import imread
from tensorflow.keras.preprocessing import image
import cv2 as cv
train_img = []
y_train = []
for i, direc in enumerate(train_folders):
    for file in direc.iterdir():
        img = imread(file)
        img_pred = cv.resize(img, (50, 50), interpolation=cv.INTER_AREA)
        img_pred = image.img_to_array(img_pred)
        img_pred = img_pred / 255
        train_img.append(img_pred)
        y_train.append(i)

val_img = []
y_val = []
for i, direc in enumerate(val_folders):
    for file in direc.iterdir():
        img = imread(file)
        img_pred = cv.resize(img, (50, 50), interpolation=cv.INTER_AREA)
        img_pred = image.img_to_array(img_pred)
        img_pred = img_pred / 255
        val_img.append(img_pred)
        y_val.append(i)

import numpy as np
X_train = np.array(train_img)
X_val = np.array(val_img)

X_train = np.reshape(X_train, (X_train.shape[0], -1))
X_val = np.reshape(X_val, (X_val.shape[0], -1))

from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100)
hist = model.fit(X_train, y_train)

score = model.score(X_val, y_val)

Y_pred = model.predict(X_val)

from sklearn.metrics import classification_report
print(classification_report(y_val, Y_pred, target_names=classes))