Issue
I'm trying to train a model to choose between a row of grayscaled pixel.
from sklearn.neural_network import MLPRegressor
X = [[1, 2, 3], [2, 1, 3], [2, 1, 2]]
y = [0, 1, 2]
model = MLPRegressor(hidden_layer_sizes=(10, 10), solver="lbfgs", activation="relu")
model.fit(X, y)
Where X is the list of lines I want to train the model with, and y is the expected pixel index the model should output. (example values)
img = color.rgb2gray(img)
model.predict(img[0])
where img[0] prints
[0.61176471 0.46045752 0.2627451 ]
in the console. Which is expected.
but the prediction fails:
ValueError: Expected 2D array, got 1D array instead: array=[0.61176471 0.46045752 0.2627451 ]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
and once reshaped with :
line = (np.array(img[0])).reshape(1,-1)
outputs :
[[0.61176471 0.46045752 0.2627451 ]]
and model.predict(line)
outputs
[1.79295143]
which I can't interpret since I'm looking for a array of index probability like [0.998, 0.001, 0.001]
Before that, I was working with an RGB line of pixel that should look like [[[R][G][B]],[[R][G][B]],[[R][G][B]]] Where the letters are a value between 0 and 1 and the model was outputting an array of probability. Which make me now think it was not working as expected and was most likely outputting the probability related to the colors and not the pixels.
What am I doing wrong?
Solution
As you are using regression, the model will attempt to predict a continuous value, which is how you are training it (note that the output Y is a single value):
- from X = [1,2,3] fit Y = 0
- from X = [2,1,3] fit Y = 1
- from X = [2,1,2] fit Y = 2
The output you are expecting is the one for a classifier, where each class gets a probability as output, i.e. confidence of the prediction. You should use a classification model instead, if that's what you want/need. And training it accordingly (each index in the output represents a class)
- from X = [1,2,3] fit Y = [1, 0, 0]
- from X = [2,1,3] fit Y = [0, 1, 0]
- from X = [2,1,2] fit Y = [0, 0, 1]
Answered By - gustavovelascoh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.