python – order =’F’的numpy.reshape()如何工作?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – order =’F’的numpy.reshape()如何工作?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1753字,纯文字阅读大概需要3分钟。
内容图文
![python – order =’F’的numpy.reshape()如何工作?](/upload/InfoBanner/zyjiaocheng/811/e2433b9deccf4697aa8c97df4599a172.jpg)
我以为我理解了Numpy中的重塑功能,直到我搞砸了它并遇到了这个例子:
a = np.arange(16).reshape((4,4))
返回:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
这对我来说很有意义,但是当我这样做时:
a.reshape((2,8), order = 'F')
它返回:
array([[0, 8, 1, 9, 2, 10, 3, 11],
[4, 12, 5, 13, 6, 14, 7, 15]])
我希望它能回归:
array([[0, 4, 8, 12, 1, 5, 9, 13],
[2, 6, 10, 14, 3, 7, 11, 15]])
有人可以解释一下这里发生了什么吗?
解决方法:
‘F’的元素
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
是[0,4,8,12,1,5,9 …]
现在用(2,8)数组重新排列它们.
我认为重塑文档谈论了对元素的粗暴,然后重新塑造它们.显然,首先完成了ravel.
尝试a.ravel(order =’F’).重塑(2,8).
哎呀,我得到了你的期望:
In [208]: a = np.arange(16).reshape(4,4)
In [209]: a
Out[209]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
In [210]: a.ravel(order='F')
Out[210]: array([ 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15])
In [211]: _.reshape(2,8)
Out[211]:
array([[ 0, 4, 8, 12, 1, 5, 9, 13],
[ 2, 6, 10, 14, 3, 7, 11, 15]])
好的,我必须在重塑期间保持’F’顺序
In [214]: a.ravel(order='F').reshape(2,8, order='F')
Out[214]:
array([[ 0, 8, 1, 9, 2, 10, 3, 11],
[ 4, 12, 5, 13, 6, 14, 7, 15]])
In [215]: a.ravel(order='F').reshape(2,8).flags
Out[215]:
C_CONTIGUOUS : True
F_CONTIGUOUS : False
...
In [216]: a.ravel(order='F').reshape(2,8, order='F').flags
Out[216]:
C_CONTIGUOUS : False
F_CONTIGUOUS : True
来自np.reshape docs
You can think of reshaping as first raveling the array (using the given
index order), then inserting the elements from the raveled array into the
new array using the same kind of index ordering as was used for the
raveling.
订单上的注释相当长,所以这个主题令人困惑也就不足为奇了.
内容总结
以上是互联网集市为您收集整理的python – order =’F’的numpy.reshape()如何工作?全部内容,希望文章能够帮你解决python – order =’F’的numpy.reshape()如何工作?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。