如何理解ndarray.reshape函数? [英] How to understand ndarray.reshape function?

查看:151
本文介绍了如何理解ndarray.reshape函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

reshape()的原型是reshape(shape, order="C"),并且形状类型为元组. 所以我们应该用myarray.reshape((1000, 1, 32, 32))调用此函数,但是我发现很多人使用myarray.reshape(1000, 1, 32, 32),为什么?

The prototype of reshape() is that reshape(shape, order="C"), and the type of shape is tuple. So we should call this function with myarray.reshape((1000, 1, 32, 32)), But I find that many use myarray.reshape(1000, 1, 32, 32), why?

推荐答案

reshape方法中内置了一些隐藏的灵活性.

It's a bit of hidden flexibility built into the reshape method.

此处的关键字必须明确:您不能这样做:

The keyword here needs to be explicit: you can't do for example:

myarray.reshape(1000, 1, 32, 32, "C")

您将得到一个TypeError,表示必须为整数.

You'll get a TypeError, saying that an integer is required.

(实际上,即使使用元组:

(In fact, even using a tuple:

myarray.reshape((1000, 1, 32, 32), "C")

提高TypeError.)

如果您查看源代码(例如,在 GitHub ,您会看到在解析关键字之后,将检查0或1个参数.在这种情况下,该参数将被解释为元组.如果还有更多参数,每个都将解释为整数,并组合成新形状的元组(关键字参数已被删除).

If you look at the source code (e.g., at GitHub, you'll see that, after the keywords are parsed, there is a check for 0 or 1 arguments. In that case, the argument is interpreted as a tuple. If there are more arguments, each is interpreted as an integer and combined into a tuple as the new shape (the keyword arguments have already been taken out).

关于应该使用哪一个:我想这并不是一个很好的答案.
您可以坚持使用文档,并使用元组.
但是,多整数参数约定很明显.

As to which one you should use: I guess there's not really a good answer.
You could stick with the documentation, and use tuples.
The multiple-integer-arguments convention, however, feels rather obvious.

请务必遵守您或您正在从事的项目所使用的约定.不要在同一项目中使用元组和单个整数参数.

Do stick to the convention that you, or the project you're working on, use(s). Don't use tuples and individual integer arguments in the same project.

这篇关于如何理解ndarray.reshape函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆