为什么 np.empty() 和 np.zeros() returns 不同的值?
Why np.empty() and np.zeros() returns different values?
我正在尝试使用 np.empty() 创建查找 table,但是当我打印数组时,我意识到数组中的值不是零。当我搜索它时,我发现 np.zeros() 和 np.empty() 都将 0 分配给所有值,但在分配时做的事情不同。我的意思是为什么不 np.empty() returns 一个全是零的数组?分配差异是怎么回事?
这是我的代码:
import numpy as np
lookUpTable = np.empty((1,256), np.uint8)
print(lookUpTable[0,2])
print(lookUpTable)
gamma=0.4
for i in range(256):
lookUpTable[0,i] = np.clip(pow(i / 255.0, gamma) * 255.0, 0, 255)
print(lookUpTable)
这是他们布置的作业:
np.zeros()
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]]
np.empty()
[[ 92 0 63 0 63 0 92 0 67 0 58 0 92 0 85 0 115 0
101 0 114 0 115 0 92 0 102 0 97 0 122 0 105 0 108 0
92 0 65 0 112 0 112 0 68 0 97 0 116 0 97 0 92 0
76 0 111 0 99 0 97 0 108 0 92 0 80 0 114 0 111 0
103 0 114 0 97 0 109 0 115 0 92 0 80 0 121 0 116 0
104 0 111 0 110 0 92 0 80 0 121 0 116 0 104 0 111 0
110 0 51 0 56 0 45 0 51 0 50 0 92 0 108 0 105 0
109 0 112 0 121 0 92 0 114 0 97 0 110 0 100 0 111 0
109 0 92 0 95 0 103 0 101 0 110 0 101 0 114 0 97 0
116 0 111 0 114 0 46 0 99 0 112 0 51 0 56 0 45 0
119 0 105 0 110 0 51 0 50 0 46 0 112 0 121 0 100 0
46 0 50 0 46 0 67 0 111 0 110 0 102 0 105 0 103 0
0 0 0 0]]
np.zeros
保证您得到一个用零填充的数组。 np.empty
为您提供一个数组,其中包含内存中发生的所有内容;没有保证它会将数组清零。
仅当您计划用您自己的值完全填充数组时才使用后者。
我对 numpy 的内部实现一无所知,但我怀疑它在分配一个数组来保存结果时使用 np.empty
或一些等效的东西。它将显式填充结果数组的每个元素,因此不需要浪费时间确保数组清零。
来自 np.zeros
documentation:
Return a new array of given shape and type, filled with zeros.
来自 np.empty
documentation:
Return a new array of given shape and type, without initializing entries.
文档对此非常清楚并包含答案:numpy.empty()
returns 所选大小的 未初始化 数组。这意味着当您创建数组时,数组的内容将完全未定义,并且您需要确保在第一次使用它时正确地初始化它。
相反,numpy.zeros()
负责通过用零填充来初始化新数组。
我正在尝试使用 np.empty() 创建查找 table,但是当我打印数组时,我意识到数组中的值不是零。当我搜索它时,我发现 np.zeros() 和 np.empty() 都将 0 分配给所有值,但在分配时做的事情不同。我的意思是为什么不 np.empty() returns 一个全是零的数组?分配差异是怎么回事?
这是我的代码:
import numpy as np
lookUpTable = np.empty((1,256), np.uint8)
print(lookUpTable[0,2])
print(lookUpTable)
gamma=0.4
for i in range(256):
lookUpTable[0,i] = np.clip(pow(i / 255.0, gamma) * 255.0, 0, 255)
print(lookUpTable)
这是他们布置的作业:
np.zeros()
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]]
np.empty()
[[ 92 0 63 0 63 0 92 0 67 0 58 0 92 0 85 0 115 0
101 0 114 0 115 0 92 0 102 0 97 0 122 0 105 0 108 0
92 0 65 0 112 0 112 0 68 0 97 0 116 0 97 0 92 0
76 0 111 0 99 0 97 0 108 0 92 0 80 0 114 0 111 0
103 0 114 0 97 0 109 0 115 0 92 0 80 0 121 0 116 0
104 0 111 0 110 0 92 0 80 0 121 0 116 0 104 0 111 0
110 0 51 0 56 0 45 0 51 0 50 0 92 0 108 0 105 0
109 0 112 0 121 0 92 0 114 0 97 0 110 0 100 0 111 0
109 0 92 0 95 0 103 0 101 0 110 0 101 0 114 0 97 0
116 0 111 0 114 0 46 0 99 0 112 0 51 0 56 0 45 0
119 0 105 0 110 0 51 0 50 0 46 0 112 0 121 0 100 0
46 0 50 0 46 0 67 0 111 0 110 0 102 0 105 0 103 0
0 0 0 0]]
np.zeros
保证您得到一个用零填充的数组。 np.empty
为您提供一个数组,其中包含内存中发生的所有内容;没有保证它会将数组清零。
仅当您计划用您自己的值完全填充数组时才使用后者。
我对 numpy 的内部实现一无所知,但我怀疑它在分配一个数组来保存结果时使用 np.empty
或一些等效的东西。它将显式填充结果数组的每个元素,因此不需要浪费时间确保数组清零。
来自 np.zeros
documentation:
Return a new array of given shape and type, filled with zeros.
来自 np.empty
documentation:
Return a new array of given shape and type, without initializing entries.
文档对此非常清楚并包含答案:numpy.empty()
returns 所选大小的 未初始化 数组。这意味着当您创建数组时,数组的内容将完全未定义,并且您需要确保在第一次使用它时正确地初始化它。
相反,numpy.zeros()
负责通过用零填充来初始化新数组。