1

I'm using a MLPRegressor() with partial_fit(). My dataset has 2 outputs and 2 inputs. I'm using a sequential model:

# input: X.shape(10.000, 2)
# output: y.shape(10.000, 2)
model = MLPRegressor()

for i in range(len(X)):
    model.partial_fit(X[i], y[i])
    # more code

But it gives me this error at model.partial_fit(X[i], y[i]):

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.

What's the problem? How could I solve it?

1 Answer 1

2

Partial fit operates on a subset of X and y. Right now you're using a single row, but those don't have the expected shape. If you want to pass data in one point at a time you just need some way to let scikit-learn know that you have a single data point. Here's a couple options.

# Your current strategy
# model.partial_fit(X[i], y[i])
# Doesn't work because X[i] and y[i] have shape (2,) rather than (1, 2)

# Slices
model.partial_fit(X[i:i+1], y[i:i+1])

# Reshape
model.partial_fit(X[i].reshape(1, -1), y[i].reshape(1, -1))

Alternatively, you could use the same method to operate in larger batches (or apply masks, or do any of a number of other things -- it's pretty flexible).

n = 2
for i in range(0, len(X), n):
    model.partial_fit(X[i:i+n], y[i:i+n])

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