Numpy Basics
NumPy, short for Numerical Python, is one of the most important foundational packages for numerical computing in Python.
NumPy ndarray
ndarrays are N dimensional array object are fast, flexible container of large datasets. Arrays allow mathematical operations on whole blocks of data using similar syntax to the equivalent operations between scalar elements.
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)
np.arange(10, dtype = np.float64)
np.arange(10, dtype = 'float64').dtype
a1 = np.array([1, 2, 3, 4.5], dtype=np.float64)
a1
a1.dtype
# You can convert data type of an array.
# Note that the fractional part is truncated.
a2 = a1.astype(np.int64)
a2
a2.dtype
Array Arithmetic
a1 = np.array([1, 2, 3, 4])
a1 * a1
a1 + a1
1 / a1
a1 ** 4
a2 = np.array([4, 3, 2, 1])
a1 > a2
a3 = np.arange(10, 7, -0.25)
a3
Array Indices
Create an array
Access subsets using the methods used for list.
a1 = np.arange(10)
a1
a1[2 : 5] = 2000
a1s = a1[2 : 5]
a1s
a1
a1s[1] = 10
a1s
a1
a1s[:] = 3076
a1s
a1
3D Array Indices
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
import numpy as np
a2d = np.array([[1,2,3], [4,5,6], [7,8,9]])
a2d
a2d[:2]
a2d[:2,1:]
Array Transposing
#Create a 3 by 6 matrix from number 0-17
a = np.arange(18).reshape(3,6)
a
#Transform matrix so 3 columns and 6 rows instead of other way around
a.T
#Multiply matrixes
np.dot(a.T,a)
#Mulitply matrixes
a.T @ a
Elementwise Functions
#Creat array up tp 5
a = np.arange(5)
a
#Find Square root
np.sqrt(a)
#Exponent
np.exp(a)
#Arange an array starting at 4 and end at -1 in increments of -1
b = np.arange(4, -1, -1)
b
#Find the maximum between numbers/objects in 2 arrays
np.maximum(a, b)
#Divide every element in the array by 3
c = a/3
c
#Apply more f (separate fractions from numbers) on array c
#i takes the fractions out
#r takes the whole numbers out
i, r = np.modf(c)
i
r
Matplotlib
It is a matrix library so it takes matrixes and plots them.
#Makes images
%matplotlib inline
#Pyplot is the package we are using from matplotlib
import matplotlib.pyplot as plt
import numpy as np
#Create an array numbers 1-10
a = np.arange(10)
a
#Plot the array a
plt.plot(a)
#Create an array numbers 1-10 with the y axis being numbers above 100
a = np.arange(10) + 100
a
#Plot the array with the new y axis
plt.plot(a)
Figures and Subplots
#Create figure
fig = plt.figure()
#Add subplots to the figure in a 2 by 2 matrix referring to the first plot
ax1 = fig.add_subplot(2,2,1)
ax1
#Add subplots to the figure in a 2 by 2 matrix referring to the second plot
ax2 = fig.add_subplot(2,2,2)
ax2
#Have to put all the data in one code block to display information
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
#Create a plot code
ax3.plot(np.random.standard_normal(50).cumsum(),color="black",linestyle="dashed")
#Plot Data
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax1.plot(np.random.standard_normal(50).cumsum(),color="blue",linestyle="dashed")
ax2.plot(np.random.standard_normal(50).cumsum(),color="green",linestyle="dashed")
ax3.plot(np.random.standard_normal(50).cumsum(),color="black",linestyle="dashed")
#Plot Data
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax3.plot(np.random.standard_normal(50).cumsum(),color="black",linestyle="dashed")
ax2.scatter(np.arange(30),np.arange(30) + 3 * np.random.standard_normal(30))
#Plot Data
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax2.scatter(np.arange(30),np.arange(30) + 3 * np.random.standard_normal(30))
ax3.plot(np.random.standard_normal(50).cumsum(),color="black",linestyle="dashed")
ax1.hist(np.random.standard_normal(100), bins=20, color= "yellow", alpha=0.3)
#Creat 2 by 3 matrix
fig, axes = plt.subplots(2, 3)
axes
fig, axes = plt.subplots(2, 2, sharex= True , sharey= True)
for i in range(2):
for j in range(2):
axes[i, j].hist(np.random.standard_normal(500), bins=50,color= "red", alpha=0.5)
fig.subplots_adjust(wspace=0, hspace=0)
#