首页 > 解决方案 > 带有 partial_fit() 的 MLPRegressor

问题描述

我正在使用MLPRegressor()with partial_fit()。我的数据集有 2 个输出和 2 个输入。我正在使用顺序模型:

# 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

但它给了我这个错误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.

有什么问题?我该如何解决?

标签: pythonpython-3.xscikit-learn

解决方案


部分拟合对X和的子集进行操作y。现在您正在使用单行,但那些没有预期的形状。如果您想一次传递一个数据点,您只需要某种方式让 scikit-learn 知道您有一个数据点。这里有几个选项。

# 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))

或者,您可以使用相同的方法进行大批量操作(或应用遮罩,或执行其他任何操作——它非常灵活)。

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

推荐阅读