numpy - numpy,使用赋值语句改变数组的形状来改变形状属性的行为与使用 reshape 时不同
问题描述
我对以下两种改变 numpy 数组形状的不同行为感到困惑:
x.shape = (3,4)
x = x.reshape(3,4)
这是一个例子:
In [1]: import numpy as np
##### Case 1
In [2]: fa = np.arange(12).reshape(4,3)
In [3]: x = fa
In [4]: x is fa
Out[4]: True
In [5]: x.shape = (3,4) # RESHAPING does not affect x
In [6]: x is fa
Out[6]: True
########Case 2 ########
In [7]: fa = np.arange(12).reshape(4,3)
In [8]: x = fa
In [11]: x.reshape(3,4) is fa # RESHAPING makes x a shallow copy
Out[11]: False
########Verify x is shallow copy
In [13]: fa = np.arange(12).reshape(4,3)
In [14]: x.reshape(3,4).base is fa.base
Out[14]: True
我无法理解为什么在第一种整形情况下,x 仍然与 fa 相同,但在第二种情况下,在整形后 x 变成了 fa 的浅拷贝。
解决方案
推荐阅读
- python - 如何按照声明的顺序运行单元测试测试用例
- python-3.x - 我无法同时从我的 RBP3 的相机流式传输提要并使用 python 同时记录到文件
- python - 转换为自定义字典类型
- html - 我将如何在 freecodecamp.org 上解决这个问题?- css相关
- r - 查找并用空格替换十六进制数字
- excel - 继续返回用户表单进行更正
- c++ - C ++避免位操作中的完全移位
- python - grid() 在 tkinter 中是否有像 pack() 这样的填充属性?
- regex - 提取字符串、条件或正则表达式的“结尾”?
- r - R:如何在特定条件下替换 NA 值