I created SVC model trained on the "MNIST" data set which works well on the mnist digits. When writing my own digits on a paper and trying the model on those images it predicts a 3 on every image.
I have tried to format the handwritten image to match the format of the mnist images used during the training of the model. i ran the mnist data through this pipeline :
`preprocess_pipe = Pipeline([
('scaler', StandardScaler()),
('pca', PCA(n_components=0.95)),
])`
For the handwritten digit i used this function :
with Image.open(image_path) as img: img = ImageOps.grayscale(img) img_resized = img.resize((28, 28), Image.LANCZOS) img_inverted = ImageOps.invert(img_resized) img_array = np.array(img_inverted) img_flattened = img_array.flatten() img_reshaped = img_flattened.reshape(1, -1) img_ready = pipe.transform(img_reshaped)
The model still predicts 3 for the images i insert. I have also tried to use this function to adjust the pixels but the result is the same:
def adjust_pixel_values(image, threshold=127): max_value = 255 image_adjusted = np.where(np.abs(image - 0) < np.abs(image - max_value), 0, max_value) return image_adjusted