Numpy reshape 更改数组的维度
【摘要】 所属的课程名称及链接[AI基础课程--常用框架工具]环境信息* ModelArts * Notebook - Multi-Engine 2.0 (python3) * JupyterLab - Notebook - Conda-python3 * numpy 1.19.1Numpy reshape 更改数组的维度temp = np.zeros([2,3,4])print("...
所属的课程名称及链接
环境信息
- * ModelArts
- * Notebook - Multi-Engine 2.0 (python3)
- * JupyterLab - Notebook - Conda-python3
- * numpy 1.19.1
- * JupyterLab - Notebook - Conda-python3
- * Notebook - Multi-Engine 2.0 (python3)
Numpy reshape 更改数组的维度
temp = np.zeros([2,3,4])
print("temp",temp)
temp_new = temp.reshape([4,6]) # 元素的总个数(2*3*4=24)不变的情况下进行reshape
print("temp",temp)
print("temp_new",temp_new)
print(id(temp),id(temp_new))
print("-----------------")
temp_new = temp.reshape([4,7]) # 这样做就会报错,ValueError!
temp [[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]
temp [[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]
temp_new [[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]
140319658903952 140319660464768
-----------------
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-27-784dd88cae99> in <module>()
6 print(id(temp),id(temp_new))
7 print("-----------------")
----> 8 temp_new = temp.reshape([4,7]) # 元素的总个数不变的情况下进行reshape
ValueError: cannot reshape array of size 24 into shape (4,7)
help
help(np.array([]).reshape)
Help on built-in function reshape:
reshape(...) method of numpy.ndarray instance
a.reshape(shape, order='C')
Returns an array containing the same data with a new shape.
Refer to `numpy.reshape` for full documentation.
See Also
--------
numpy.reshape : equivalent function
Notes
-----
Unlike the free function `numpy.reshape`, this method on `ndarray` allows
the elements of the shape parameter to be passed in as separate arguments.
For example, ``a.reshape(10, 11)`` is equivalent to
``a.reshape((10, 11))``.
备注
1. 感谢老师的教学与课件
2. 欢迎各位同学一起来交流学习心得^_^
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
2. 欢迎各位同学一起来交流学习心得^_^
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)