Numpy personal tips

numpy is one of the essential tools for data analysis and numerical computation. It is a library that is always needed when implementing machine learning, etc. I’ll leave a memo as a personal reminder. For details, please refer to the following official page.

Contents

github

  • The file in jupyter notebook format on github is here.

Author’s environment

The author’s environment and import method are as follows.

!
ProductName: Mac OS X
ProductVersion: 10.14.6
BuildVersion: 18G2022
Python -V
Python 3.7.3
%matplotlib inline
%config InlineBackend.figure_format = 'svg'

import numpy as np

np.__version__
'1.16.2'

exponential and logarithmic

np.exp(x)

Computes $\exp{x}$.

print(np.exp(0))
print(np.exp(1))
print(np.exp(2))
1.0
2.718281828459045
7.38905609893065

np.expm1(x)

Compute $\exp{x}-1$.

print(np.expm1(0))
print(np.expm1(1))
print(np.expm1(2))
0.0
1.7182818284590453
6.38905609893065

np.exp2(x)

Compute $2^{x}$.

print(np.exp2(0))
print(np.exp2(1))
print(np.exp2(2))
1.0
2.0
4.0

np.log(x)

Compute $\log{x}$. The bottom will be the natural logarithm.

print(np.log(1))
print(np.log(2))
print(np.log(np.e))
0.0
0.0 0.6931471805599453
1.0

np.log10(x)

Compute $\log_{10}{x}$.

print(np.log10(1))
print(np.log10(2))
print(np.log10(10))
0.0
0.3010299956639812
1.0

np.log2(x)

Compute $\log_{2}{x}$.

print(np.log2(1))
print(np.log2(2))
print(np.log2(10))
0.0
1.0
3.321928094887362

np.log1p(x)

Computes $\log{(x + 1)}$. The bottom will be the natural logarithm. This is often used in data analysis. When the original number is zero, it often occurs to add 1 to take the logarithm and apply it to the classifier. This is when calculating the so-called logloss.

print(np.log1p(0))
print(np.log1p(1))
print(np.log1p(-1 + np.e))
0.0
0.0 0.6931471805599453
1.0