Numpy Basics
NumPy ndarry
import numpy as np
data = np.array([
[1,1.5, 2],
[5, 3, 1]
])
data
data.shape
data.dtype
data.ndim
Create NP Array
np.zeros((3,6))
np.empty((2,4))
np.arange(10)
a1 = np.arange(10, dtype = 'float64')
a1
np.arange(10, dtype = 'float64').dtype
a2 = a1.astype(np.int64)
a2
a2.dtype
Array Arithmetic
a1 = np.array([5, 3, 7 ,2])
a1 * a1
a1 + a1
1/a1
a1**7
a2 = np.array([4, 8, 9, 6])
a1>a2
Array Indices
a1 = np.arange(10)
a1
a1[2 : 5] = 2000
a1s = a1[2 : 5]
a1s
a1 = a1 + a1
a1
a1s[1] = 10
a1s
a1
a1s[:] = 3076
a1s
a1
3D Array Indicies
a3d = np.array([[[1, 2, 3], [4, 5, 6]],
[[7, 8, 9], [10, 11, 12]]])
a3d
a3d[1]
a3d[0][1][2]
a3d[0,1,2]
a3d[0] = 5
a3d
Array Slicing
a2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a2d
a2d[:2, 1:]
Array Transposing
a = np.arange(18).reshape(3,6)
a
a.T
np.dot(a.T, a)
a.T @ a
Elementwise Functions
a = np.arange(5)
a
np.sqrt(a)
np.exp(a)
b = np.arange(4, -1, -1)
b
np.maximum(a, b)
c = a / 3
c
i, r = np.modf(c)
i
r
Matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(10) + 100
a
plt.plot(a)
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax1
ax2 = fig.add_subplot(2, 2, 2)
ax2
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)
ax2 = fig.add_subplot(2, 2, 2)
ax3 = fig.add_subplot(2, 2, 3)
fig, axes = plt.subplots(2, 3)
axes
fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)