Preliminary Code Flashcards
Why do we need to be concerned with data handling?
Before starting to train a model, we need to get the data into a form which is compatible with the training and testing
Why do we train and test in batches?
Training all the full dataset all at once as this would take too long.
What is the main aim of building models (in classification or regression)?
To make predictions on unseen data.
This is known as generalisation.
What is the square root function?
np.sqrt(num)
What is a useful feature of NumPy for scientific computing?
It can deal with sets of numbers in arrays. Arrays only contain with one type, often numbers.
How can we create an array?
array = np.array([1,2,3])
ie passing it a list
How do we check the type of an object?
type(object)
How do we access elements of a 1D array?
Same indexing as a list
array[1] - single index
array[2:] - every element from position 2 onwards
What kinds of arrays are usually used for image data”?
A 2D array is often used for an image, where each element of the array is the value of a pixel in the image.
How do you create a 2D array?
Combining multiple 1D arrays with the same size. Enclosing each in a curved bracket and separate with a comma.
array = np.array([(1,2), (3,4)])
How can you investigate the size of an array in each dimension?
array.shape
How do we tell python what type of 1D vector we want (column or row vector)?
rowVec = array.reshape(1,-1)
rowVec .shape is (1,7)
colVec = array.reshape(-1, 1)
colVec.shape is (7, 1)
How can you see how many dimensions are in your array?
array.ndim
How can you see the total number of elements in a multi-dimensional array?
array.size
How do you index a 2D array?
array[1, 2]
What does the : by itself indicate in selection?
You select the whole 1D array along that dimension.
eg array2D([3, :])
What are two built-in ways to quickly build arrays?
- linspace()
- arrange()
Both outpace 1D array of numbers
What does linspace do?
Outputs evenly spaced numbers between the “start” and “stop” values
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
The default number of elements is 50.
Endpoint set to true, last number included by default.
What does arange do?
Allows us to define spacing and the length of the array.
arange(start, stop, step , dtype=None)
Default start is 0 and default step is 1.
What are linspace and arange functions useful for?
Useful for building iterators.
eg x = np.arrange(0 , 5.1, 0.1)
for n in x:
print(x)
What should you check before joining arrays?
Ensure they have the same size along the dimension where you want to join them.
What are functions for stacking arrays?
vstack - vertical stacking
hstack - horizontal stacking
dstack - stack in depth
What are different ways you can initialise arrays?
- Pass a list
- Create an array of all zeros
- Create an array of all ones
- Create a constant arrays
- Create a random array
How do you create an array of zeroes?
np.zeros((2,2))