python - 带有 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.
有什么问题?我该如何解决?
解决方案
部分拟合对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])
推荐阅读
- centos7 - cherrypy 2.3.0 与 Python 2.7.5 兼容吗?
- elixir - Elixir:fs 的编译突然因 rebar_abort 而失败
- php - 找不到本地主机子域 nginx wordpress 的文件
- django - Django-oscar:按照教程输入错误
- react-native - 无法在我的本机应用程序上创建 iOS 文件夹
- javascript - React / Redux - 如何渲染/更新深度嵌套的状态
- apache-zeppelin - 在 Zeppelin 中添加 jar 依赖时出错
- docker - 当 docker restart 因大量工人和短任务而变慢时,限制因素是什么?
- angular - 错误:未捕获(承诺):错误:无法匹配任何路由。URL 段:'edit/2'
- javascript - 如何计算 MongoDB 中的实例