2
$\begingroup$

I am trying to train a classification Model to classify Images, but my images here are Numpy arrays of 1's and 0's as shown in the Image,

For now, I have tried,

x1_data=[]
def create_array(columns=5,rows=5,randomness=.3):

    board = np.zeros([rows,columns],dtype='int64')

    for i in range(rows):
        for j in range(columns):
            if np.random.random() <= randomness:
                board[i,j] = 1
    return board

for i in range(5):
    x1_data.append(create_array())

which gives me 5 arrays `

array([[[0, 0, 1, 0, 0],
        [0, 0, 1, 0, 0],
        [1, 0, 0, 0, 0],
        [0, 0, 0, 0, 1],
        [0, 0, 0, 0, 1]],

       [[0, 0, 1, 0, 0],
        [0, 0, 0, 1, 0],
        [0, 0, 0, 1, 0],
        [0, 0, 0, 1, 0],
        [0, 1, 0, 0, 0]],

       [[1, 0, 0, 1, 0],
        [1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 1, 0, 0]],

       [[0, 0, 0, 0, 0],
        [1, 0, 0, 1, 1],
        [0, 0, 1, 1, 1],
        [0, 0, 0, 0, 0],
        [0, 0, 1, 0, 1]],

       [[0, 0, 0, 0, 1],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0],
        [0, 1, 1, 0, 0],
        [0, 0, 0, 0, 1]],

       [[1, 0, 0, 1, 1],
        [0, 0, 1, 1, 0],
        [0, 0, 1, 0, 0],
        [1, 0, 0, 0, 0],
        [1, 1, 0, 0, 0]]]

For class A, similarly I generated 5 arrays for class B and then stacked them as a single X array.

My output y is:

y = np.random.choice([0, 1], size=(10,), p=[1./3, 2./3])
array([0, 1, 1, 1, 1, 0, 0, 0, 0, 0])

How do I preprocess this Data to train on an MLP in sklearn I am getting this error

clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
clf.fit(g, h)    
#Error
Found array with dim 3. Estimator expected <= 2.
$\endgroup$
2
$\begingroup$

If you're going to use an MLP classifier (probably not the best option for image classification: why not CNN?), you should feed it a "flat" array. Each image has 25 pixels (features), so your input must be of shape (n, 25), where n is the number of training observations (10 in your case).

Currently your input is of shape (10, 5, 5) (dimension 3, hence the error message). Try reshaping it like this: X = X.reshape(-1, 25).

|improve this answer|||||
$\endgroup$
  • $\begingroup$ I'll try that but I don't know if I am creating the Data correctly. $\endgroup$ – m2rik Dec 15 '19 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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