NumPy Crash Course
NumPy is a robust scientific computing library for use with the Python language.
Array Basics
One of the most common uses of NumPy is manipulating computational arrays.
The primary array structures are:
1-Dimensional(1D)
2-Dimensional(2D)
3-Dimensional(3D)
Array terminology to know:
Row = i
Column = j
Where:
Array Table |
---|
V j, j, j, |
i [1, 2, 3] |
i [4, 5, 6] |
i [7, 8, 9] |
Values(which we will call ‘V’) in the array can be accessed or referenced through the i
‘th to the j
‘th position.
So for a generic value, we can represent this as ‘V(i,j)’ or in the case of ‘5’ = V(1,1) assuming a 0-based indexing.
LOADING/SAVING ARRAYS:
Text Files:
np.savetxt("arrayname.txt", array, delimiter ="")
np.loadtxt("filepath/filename.txt", array)
Disk:
np.load("filepath/filename.npy")
np.save("filepath/filename.npy", array)
CREATING ARRAYS:
Creating an array is terribly simple:
1dim_array = np.array([5, 10, 15])
2dim_array = np.array([2, 4, 6],[8,10,12])
One way to create a 3-dimensional array is to generate an empty array as follows:
3dim_array =np.empty((2,2,2))
Which will create a 2x2x2 structure.
We can now expand our “i’th to the j’th” model to include a k’th position. Alternatively, (and more intuitive IMO), is simply thinking in terms of x, y, and z positions.
STARTING VALUES:
I illustrated how to create an empty array above, but what if you’d like to populate the array with non-empty values as placeholders?
You have options!
array = np.empty()
- Empty
array = np.zeros()
- All 0’s
array = np.ones()
- All 1’s
array = np.random.random()
- All Randoms
array = np.arange()
All step-wise ranges
array = np.linspace()
Values spaced evenly
ARRAY ATTRIBUTES:
It’s always helpful to probe your array for information. I’ve compiled a list of common selectors to investigate your structures.
array.dtype
- Returns the Data Type
len(array)
- Returns the array length
array.shape
- Returns the array dims
array.size
- Returns a count of values in the array
array.astype(dtype)
- Changes array values from one data type to another
SORTING/COPYING:
There are a number of options to create copies and sort existing arrays.
Sorting:
array.sort()
- Return a sorted array
array.sort(axis = value)
- Return an array with elements in an axis sorted
Copying:
np.copy(array)
- Generates a copy of the array
arrayCopyName = array.copy()
- Generates a deep copy
NumPy Math Tool Kit
Arithmetic:
Addition |
---|
ArrayAddition = ArrayX + ArrayY |
np.add(ArrayX, ArrayY) |
Subtraction |
---|
ArraySubtration = ArrayX - ArrayY |
np.subtraction(ArrayX, ArrayY) |
Division |
---|
ArrayDivision = ArrayX/ArrayY |
np.division(ArrayX, ArrayY) |
Multiplication |
---|
ArrayMultiply = ArrayX*ArrayY |
np.multiply(ArrayX, ArrayY) |
Exponentiation |
---|
ArrayExp(ArrayX**ArrayY) |
np.exp(ArrayX, ArrayY) |
Square Root |
---|
np.sqrt(ArrayX) |
Logarithms:
Logs |
---|
np.log(ArrayX) -> Natural Log |
np.log10(ArrayX) -> Base-10 Log |
Trig:
Trigonometry |
---|
np.sin() |
np.cos() |
np.tan() |
np.arcsin() |
np.arccos() |
np.arctan() |
np.hypot() |
np.degrees() |
np.radians() |
Rounding:
Functions |
---|
np.trunc() |
np.ceil() |
np.floor() |
np.around() |
np.round_() |
np.fix() |
Misc:
Functions |
---|
array.sum() |
array.min() |
array.max() |
array.cumsum() |
array.mean() |
array.median() |
array.corrcoef() |
np.std(array) |
Array Manipulation
Addition or Removal of Elements
Functions |
---|
np.append() |
np.insert() |
np.delete() |
Logic Assertions
Equalities
Functions |
---|
ArrayX == ArrayY |
ArrayX > number |
ArraYX < number |
np.array_equal(ArrayX, ArrayY) |
Accessing Array Content
Slicing
Functions |
---|
array[start:stop:step,column] |