Week 1 - Syntax
Variables
variable_name = <value> - used to define a variable
Python Modules and Packages
import <module>
<module>.<function> - calls function
<module>.<variable> - calls variable
Can do the same thing as above but using an alias
import <module> as <module alias>
<module alias>.<function>
<module alias>.<variable>
import <package> as <A> - used to import a top-level package containing submodules
<A>.<submodule>.<function> - used to use a function in a submodule in a package
import <package>.<submodule> as <B> - used to import just a submodule from a package
What is NumPy?
import numpy as np - importing the NumPy package under the alias np
arr = np.array([<list>],dtype = <dtype option>) - will create an array the list will be the numbers for the array. A 2D will have a list of lists. dtype option can be None, int64 for integers, float64 (for floats) and str (for strings), this is optional.
type(arr) - will check what data type is stored in an array
Advanced Array Creation
arr = np.arrange(<start>,<stop>,<step>, dtype = <dtype option>) - used to create a vector with equally spaced elements given the spacing of the elements (stop is not optional)
arr = np.linspace(<start>,<stop>,<num>, dtype = <dtype option>) - used to create a vector with equally spaced elements given the number of elements (start and stop are not optional), (50 is the default)
np.ones(<shape>): creates an array of the given shape, filled with ones.
np.zeros(<shape>): creates an array of the given shape, filled with zeros.
np.zeros_like(<arr>) – creates an array with the same shape and data type as the given arr, filled with zeros
np.logspace(<start>, <stop>, <num>) – creates a 1D array with equally spaced elements on a log scale from start to stop with num (default 50; optional) elements.
np.random.rand(<shape>) – creates an array with random floats from a uniform distribution over from 0 (inclusive) to 1 (exclusive).
np.random.randint(<low>, <high>, <shape>) – creates an array of the given shape, filled with random integers from low (inclusive) to high (exclusive).
Addressing and Slicing
In this arr could be exchanged for anything and is actually the name of the array you are referring to.
arr[<index>] - used to access an element by its index number in a 1D array
arr[<row>,<column>] - used to access an element by its index number in a 2D array
arr[[<index>,<index2>,<index3>]] - accessing multiple elements in a 1D array
Below are used for addressing multiple elements in a 2D array, result will be a 1D array
arr[<row>, [<column1>, <column2>, <column3>]]
arr[[<row1>, <row2>, <row3>], <column>]
arr[[<row1>, <row2>, <row3>], [<column1>, <column2>, <column3>]]
For all of the rows or columns put a colon (:) or can just leave a space.
arrayName[<start>:<end>:<step>] - used to take a slice of an array, end is not included in the slice, if nothing s put in for start and end then it will do the entire matrix.
Working with NumPy Arrays
<ndarray 2> = np.transpose(<ndarray 1>) - Used to transpose arrays (can only be used on 2D arrays)
<ndarray 2> = np.append(<ndarray 1>, <value>) - will add an element to the end of a 1D array
<ndarray 3> = np.hstack([<ndarray 1>, <ndarray 2>]) - will combine arrays into a single array. hstack will combine horizontally (must have the same number of rows) and vstack will combine vertically (must have the same number of columns). This is concatenating.
append can also be used to concatenate but it can only be for two arrays and cannot concatenate 1D arrays vertically. If the dimensions are not specified, the result of np.append() will be a flattened 1D array.
<ndarray 2> = np.reshape(<ndarray 1>, <shape>, order = <order>) - reshapes an array by changing its shape without changing its data. shape is the new shape of the array - 1 to flatten an array to a 1D array. order specifies the order in which the elements of the array should be arranged - options are C default rows then columns and F columns then rows.
type(arr1) - will return numpy.ndarray because it is the type of the whole array
type(arr1[0]) - will return numpy.int64 if the specific element is an integer
type(arr2[0]) - will return numpy.ndarray because arr2 is a 2D array and requires two locations
type(arr2[0,0]) - will return numpy.int64 if element is an integer
<ndarray2> = np.astype(<ndarray 1>, <dtype>) - used to cast an array options are int, float, str, bool
NumPy Array Arithmetic
Typical numerical conventions work by applying it to each corresponding element in pairs giving a final array.
arr1 + arr2 - addition
arr1 - arr2 - subtraction
arr1 * 2 - multiplication
arr1 / 2 - division
arr1 ** 2 - power of
arr1 % 2 - remainder
arr1 // 2 - floor division to give integer
arr1 @ arr2 - For typical matrix multiplication which gives a single number.
For all axis=0 is for each column, axis=1 is for each row
np.sum() - the summation for an entire array
np.mean() - the average of the entire array
np.max() and np.min() - max and min of an entire array
np.argmax() an np.argmin() - used to find the indexes of the maximum and minimum values
When the above is used for a 2D array it will return a flat index which can be converted into an equivalent coordinate axes using the below.
<row>,<col> = np.unravel_index(<flat_index>,<shape>)
np.log2(x) - log2(x)
np.log10(x) - log10(x)
np.log(x) - loge(x)
np.exp(x) - ex
np.cos(x) - cos where x is in radians
np.sin(x) - sin where x is in radians
np.tan(x) - tan where x is in radians
np.deg2rad(x) - convert x from degrees to radians
np.rad2deg(x) - convert x from radians to degrees
np.arccos(x) - inverse of cos where x is in radians
np.arcsin(x) - inverse of sin where x is in radians
np.arctan(x) - inverse of tan where x is in radians
np.e - euler’s number
np.pi - pi
np.round(value,decimals) - evenly rounds the value to the given number of decimals
np.ceil(value) - rounds up to the nearest integer
np.floor(value) - rounds down the value to the nearest integer
np.abs(), np.absolute() - absolute value
np.sqrt() - non-negative square root
np.sign() - indication of the sign (-1 for negative 0 is x==0 and 1 if positive)