I am trying to create a lookup table using np.empty() but when I printed the array, I realized that velues in array were not zeros. When I searched for it, I found out that both np.zeros() and np.empty() assigns 0 to all values but do things different while allocating. I mean why not np.empty() returns an array full of zeros? And what is the deal with that allocating differences?
Here's my code:
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)
And here is the assignment they gave:
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.empty
documentation explicitly states that it doesn't do that. Have you read the documentation? "empty, unlike zeros, does not set the array values to zero, and may therefore be marginally faster. On the other hand, it requires the user to manually set all the values in the array, and should be used with caution."np.empty
does not fill it with zeros, it will contain data that was there at the moment when that part of memory was allocated.