简体   繁体   中英

Training Sci-kit Learn Neural Network with 2D inputs

I am trying to train a neural network using sci-kit learn in Python to recognize shapes in an image. I have a list of points which lie on edges, each edge represented with an x and y coordinate, in the form [x,y] . So, I want to train the net using a list in the format [[x,y],[x,y],[x,y],[x,y],[x,y],[x,y],etc.] . I tried this with the following code, but I get the following error:

from sklearn.neural_network import MLPClassifier
X = [[[0., 0.], [0., 0.]], [[1., 1.], [1., 1.]]]
y = [0, 1]
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,
                hidden_layer_sizes=(5, 2), random_state=1)

clf.fit(X, y)

ValueError: Found array with dim 3. Estimator expected <= 2.

Does anyone have any advice on how I can approach this, if its even possible. By the definition and logic of neural networks, I'm not so sure if it's possible. Please let me know of any advice. Thanks!

Scikit-learn mostly takes a maximum of 2 dimensional input. You can try merging the lists to reduce the dimensionality. I used the below code to make it a 2-d data and it worked fine for me

from sklearn.neural_network import MLPClassifier
import numpy as np
X = [[[0., 0.], [0., 0.]], [[1., 1.], [1., 1.]]]
y = [0, 1]
clf = MLPClassifier(solver='lbfgs', alpha=1e-5,
            hidden_layer_sizes=(5, 2), random_state=1)
X_new = []
for i in X:
    temp =[] 
    for j in i:
        for k in j:
            temp.append(k)
    X_new.append(temp)
clf.fit(X_new,y)

X_new at the end of loop will look like this

[[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]]

Accuracy will depend upon the data. Try it and see whether it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM