How to fit perceptron scikit-learn with the svhn dataset cropped?

Viewed 124

I have to classificate the svhn dataset with the Perceptron on scikit-learn libray in python, but i don't understand why the accuracy score is very low(21%);
the dataset is the svhn dataset cropped image format and i have to pass the image in grayscale The Problem is that i have an accuracy of 21% and this is too low.
this is the code that i use:

train = sio.loadmat("train_32x32.mat")
test = sio.loadmat("test_32x32.mat")
data = train["X"]
data = np.transpose(data, [3, 0, 1, 2])
data = np.mean(data, axis=3)
X_train = np.zeros(shape=(73257, 1024))
label = train['y'].ravel()
for i in range(73257):
    X_train[i] = data[i].flatten()
clf = Perceptron()
clf.fit(X_train, label)
print(clf.score(X_train, label))
predict = clf.predict(X_train)
print(accuracy_score(label, predict))
1 Answers

The images from the dataset are a number of (32,32,3) matrices, while the Perceptron will accept a bunch of arrays.

If the data shape is (n_images,32,32,3), use:

import numpy as np

data = np.mean(data, axis=3)

to obtain an array of grey-scaled images of shape (n_images,32,32), then:

for i in range(n_images):
    X_train[i] = data[i].flatten()

so you end up with a matrix of (n_images,1024) size.

You also want to have the labels in a n_images-sized vector.

Then you set the perceptron up:

clf = Perceptron()
clf.fit(X_train, y)
Related