Aggregations : Min, Max and Everything in Between

노정훈·2023년 7월 14일
0

Numpy

목록 보기
5/10

Summing the Values in an Array

  • Python itself can do sum of all values in an array using the built-in sum function.
# In[1]
L = np.random.random(100)
sum(L)
# Out[1]
47.23340327381991

# In[2]
np.sum(L)
# Out[2]
47.23340327381991
  • Numpy's sum function is quite similar to Python's function, and the result is the same.
  • However, Numpy's version of the operation is computed much more quickly.
# In[3]
big_array=np.random.rand(1000000)
%timeit sum(big_array)
%timeit np.sum(big_array)
# Out[3]
498 ms ± 66.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
2.06 ms ± 246 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

Minimum and Maximum

  • Python has built-in functions min and max, and Numpy's corresponding functions have similar syntax, but operate much more quickly
# In[4]
min(big_array), max(big_array)
# Out[4]
(8.237773400088244e-07, 0.999999693122087)

# In[5]
np.min(big_array), np.max(big_array)
# Out[5]
(8.237773400088244e-07, 0.999999693122087)

# In[6]
%timeit min(big_array)
%timeit np.min(big_array)
# Out[6]
310 ms ± 83.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
1.31 ms ± 195 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
  • For min, max, and several other Numpy aggregates, a shorter syntax is to use methods of the array object itself
#In[7]
print(big_array.min(),big_array.max(),big_array.sum())
# Out[7]
8.237773400088244e-07 0.999999693122087 499601.2436555182

Multidimensional Aggregates

# In[8]
M=np.random.randint(0,10,(3,4))
print(M)
# Out[8]
[[7 9 2 7]
 [1 0 5 2]
 [7 5 9 5]]
  • Aggregation functions take an additional argument specifying the axis along which the aggregate is computed.
# In[9]
M.min(axis=0)
# Out[9]
array([1, 0, 2, 2])
  • The function returns four values, corresponding to the four columns of numbers.
  • Similary, we can find the maximun value within each row
# In[10]
M.max(axis=1)
# Out[10]
array([9, 5, 9])

Other Aggregation Functions

Useful Aggregation functions available in Numpy

FunctionNaN-safe versionDescription
np.sumnp.nansumCompute sum of elements
np.prodnp.nanprodCompute product of elements
np.meannp.nanmeanCompute mean of elements
np.stdnp.nanstdCompute standard deviation
np.varnp.nanvarCompute variance
np.minnp.nanminFind minimum value
np.maxnp.nanmaxFind maximum value
np.argminnp.nanargminFind index of minimum value
np.argmaxnp.nanargmaxFind index of maximum value
np.mediannp.nanmedianCompute median of elelments
np.percentilenp.nanpercentileCompute rank-based statistics of elements
np.anyN/AEvaluate whether any elements are true
np.allN/AEvaluate whether all elements are true
profile
노정훈

0개의 댓글