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.

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

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

print(np.__version__)
print(matplotlib.__version__)
1.18.1
2.2.2

Trigonometric functions

np.sin(x)

$\sin x$

print(np.sin(0))
print(np.sin(np.pi / 2))
print(np.sin(np.pi))
0.0
1.0
1.2246467991473532e-16
x = np.linspace(-2 * np.pi, 2 * np.pi, 100)
y = np.sin(x)

plt.grid()
plt.title('$y = \sin x$', fontsize=16)
plt.ylabel('$\sin x$')
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x114397588>]

np.cos(x)

$\cos x$

print(np.cos(0))
print(np.cos(np.pi / 2))
print(np.cos(np.pi))
1.0
6.123233995736766e-17
-1.0
x = np.linspace(-2 * np.pi, 2 * np.pi, 100)
y = np.cos(x)

plt.grid()
plt.title('$y = \cos x$', fontsize=16)
plt.ylabel('$\cos x$')
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x1144e8fd0>]

np.tan(x).

$\tan x$

print(np.tan(0))
print(np.tan(np.pi / 4))
print(np.tan(np.pi))
0.0
0.999999999999999
-1.2246467991473532e-16
x = np.linspace(-2 * np.pi, 2 * np.pi, 100)
y = np.tan(x)

plt.grid()
plt.title('$y = \\tan x$', fontsize=16)
plt.ylabel('$\tan x$')
plt.ylim(-5,5)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x1145def98>]

np.arcsin(x)

Inverse function of ``sin x$’'.

print(np.arcsin(0))
print(np.arcsin(1))
print(np.arcsin(-1))
0.0
1.5707963267948966
-1.5707963267948966
x = np.linspace(-1, 1, 100)
y = np.arcsin(x)

plt.grid()
plt.title('$y = \arcsin x$', fontsize=16)
plt.plot(x,y)
[<matplotlib.line.Line2D at 0x11e48cef0>]

}}.

np.arccos(x)

Inverse function of $cos x$.

print(np.arccos(0))
print(np.arccos(1))
print(np.arccos(-1))
1.5707963267948966
0.0
3.141592653589793
x = np.linspace(-1, 1, 100)
y = np.arccos(x)

plt.grid()
plt.title('$y = \arccos x$', fontsize=16)
plt.plot(x,y)
<matplotlib.line.Line2D at 0x11e4a4b00>]

}}.

np.arctan(x)

The inverse function of $tan x$.

print(np.arctan(0))
print(np.arctan(1))
print(np.arctan(-1))
0.0
0.7853981633974483
-0.7853981633974483
x = np.linspace(-np.pi, np.pi, 100)
y = np.arctan(x)

plt.grid()
plt.title('$y = \arctan x$', fontsize=16)
plt.plot(x,y)
<matplotlib.line.Line2D at 0x11e55ee10>]

}}.

np.sinh(x)

Hyperbolic sine function.

$ \displaystyle \sinh x = \frac{e^x - e^{-x}}{2} $
.
print(np.sinh(0))
print(np.sinh(-1))
print(np.sinh(1))
0.0
-1.1752011936438014
1.1752011936438014
x = np.linspace(-np.pi, np.pi, 100)
y = np.sinh(x)

plt.grid()
plt.title('$y = \sinh x$', fontsize=16)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x11e6dcf60>]

np.cosh(x)

Hyperbolic cosine function.

$ \displaystyle \cosh x = \frac{e^x + e^{-x}}{2} $
print(np.cosh(0))
print(np.cosh(-1))
print(np.cosh(1))
1.0
1.5430806348152437
1.5430806348152437
x = np.linspace(-np.pi, np.pi, 100)
y = np.cosh(x)

plt.grid()
plt.title('$y = \cosh x$', fontsize=16)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x1142c8860>]

np.tanh(x)

Hyperbolic tangent function.

$ \displaystyle \tanh x = \frac{\sinh x}{\cosh x} $

This is sometimes used for activation functions in deep learning.

print(np.tanh(0))
print(np.tanh(-1))
print(np.tanh(1))
0.0
-0.7615941559557649
0.761594155955557649
x = np.linspace(-np.pi, np.pi, 100)
y = np.tanh(x)

plt.grid()
plt.title('$y = \\tanh x$', fontsize=16)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x11e4a8a90>]

np.arcsinh(x)

Inverse function of $\sinh x$.

print(np.arcsinh(0))
print(np.arcsinh(1))
print(np.arcsinh(-1))
0.0
0.881373587019543
-0.881373587019543
x = np.linspace(-np.pi, np.pi, 100)
y = np.arcsinh(x)

plt.grid()
plt.title('$y = \\arcsinh x$', fontsize=16)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x11e7d2588>]

np.arccosh(x)

Inverse function of $\cosh x$.

print(np.arccosh(1))
0.0
x = np.linspace(1, np.pi, 100)
y = np.arccosh(x)

plt.grid()
plt.title('$y = \\arccosh x$', fontsize=16)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x11e917438>]

np.arctanh(x)

Inverse function of $\tanh x$.

print(np.arctanh(0))
print(np.arctanh(0.5))
print(np.arctanh(-0.5))
0.0
0.5493061443340549
-0.0 0.5493061443340549
x = np.linspace(-0.99, 0.99, 100)
y = np.arctanh(x)

plt.grid()
plt.title('$y = \\arctanh x$', fontsize=16)
plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x11e9e4278>]

np.deg2rad(x)

Converts from arctangent notation to radian notation.

np.deg2rad(45) # => pi / 4
0.7853981633974483

np.rad2deg(x)

Converts from arc degrees to radians notation.

np.rad2deg(np.pi / 4)
45.0