Preliminary Code Flashcards

1
Q

Why do we need to be concerned with data handling?

A

Before starting to train a model, we need to get the data into a form which is compatible with the training and testing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why do we train and test in batches?

A

Training all the full dataset all at once as this would take too long.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the main aim of building models (in classification or regression)?

A

To make predictions on unseen data.

This is known as generalisation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the square root function?

A

np.sqrt(num)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a useful feature of NumPy for scientific computing?

A

It can deal with sets of numbers in arrays. Arrays only contain with one type, often numbers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can we create an array?

A

array = np.array([1,2,3])

ie passing it a list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do we check the type of an object?

A

type(object)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we access elements of a 1D array?

A

Same indexing as a list
array[1] - single index
array[2:] - every element from position 2 onwards

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What kinds of arrays are usually used for image data”?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you create a 2D array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you investigate the size of an array in each dimension?

A

array.shape

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do we tell python what type of 1D vector we want (column or row vector)?

A

rowVec = array.reshape(1,-1)
rowVec .shape is (1,7)

colVec = array.reshape(-1, 1)
colVec.shape is (7, 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you see how many dimensions are in your array?

A

array.ndim

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you see the total number of elements in a multi-dimensional array?

A

array.size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you index a 2D array?

A

array[1, 2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the : by itself indicate in selection?

A

You select the whole 1D array along that dimension.

eg array2D([3, :])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are two built-in ways to quickly build arrays?

A
  • linspace()
  • arrange()

Both outpace 1D array of numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does linspace do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does arange do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are linspace and arange functions useful for?

A

Useful for building iterators.

eg x = np.arrange(0 , 5.1, 0.1)
for n in x:
print(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What should you check before joining arrays?

A

Ensure they have the same size along the dimension where you want to join them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What are functions for stacking arrays?

A

vstack - vertical stacking
hstack - horizontal stacking
dstack - stack in depth

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are different ways you can initialise arrays?

A
  • Pass a list
  • Create an array of all zeros
  • Create an array of all ones
  • Create a constant arrays
  • Create a random array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How do you create an array of zeroes?

A

np.zeros((2,2))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How do you create an array of ones?
np.ones((2,2))
26
How do you create a constant array?
np.full((2,2), 7)
27
How do you create an array of random numbers?
np.random.random((4,4))
28
What is the importance of being able to generate random numbers?
In machine learning random numbers are important. Eg to make an initial guess
29
How do you add/subtract/multiply a matrix by a scalar?
a * 2 a + 2 a - 2
30
How do you perform element-wise addition/subtraction/multiplication/division of matrices?
a+b a-b a*b a/b
31
How do you perform matrix multiplication?
np.matmul(a,b) This is what PyTorch will use. c_ij is the dot product of the ith row of a and the jth column of b.
32
How do we create a replicate of an array, rather than just pointing to the same point in memory?
b = a.copy()
33
How do we import pyplot?
import matplotlib.pyplot as plt
34
How do we plot data?
plt.plot(x, y)
35
How do we add an x or y label?
plt.xlabel("x") plt.ylabel("y")
36
What must we add to the end of each plot?
plt.plot()
37
What do we need to do when plotting two curves on one plot?
Add labels to distinguish the curves. eg plt.plot(x, y, label="sin(x)") And need to add plt.legend()
38
How do we see what curve corresponds to what function?
plt.legend()
39
How do we plot curves of functions?
Generate a set of data points using linspace or arange - set as x. Y is a function of x. Eg: x = np.linspace(0, 20, 1000) y = np.sin(x)
40
How do you customise the line colour and style of a plot?
Add argument "r--" eg plt.plot(x, y, 'r--', label = 'y = sin(x)') "r--" - red dashed line "k-" - solid black line
41
How do you plot two curves side by side?
fig, axs = plt.subplots(1, 2, figsize=(10, 4), tight_layout=True) axs[0].set_xlabel("x", fontsize = 20) axs[0].set_xlim([0, 20]) axs[0].tick_params(axis = 'x', labelsize = 20) axs[0].legend(loc = 'lower right', fontsize = 20)
42
How do we import pandas?
import pandas as pd
43
How do we create a data frame?
From an array: pd.DataFrame(array) From a dictionary: pd.DataFrame(dictionary, index=col_name)
44
How do we make a data series or a data frame from a data frame column?
df[['col_name']] [] - data series [[]] - keep as dataframe
45
How do you select a row by index label?
df.loc[["label"]]
46
How do you load in a dataset from a file?
pd.read_csv('file.csv',index_col=0)
47
How do you plot information in a dataframe?
df.plot(x="col", y="col", kind="scatter")
48
How do you change the scale of a plot?
plt.yscale('log')
49
How do you plot by a categorical variable?
cats = set(df['category'].values) Then can iterate over the categories. Subset the dataframe to have a df of this category. When plotting the categories, use list(cats)
50
How do you extract the values of a column in a dataframe?
.values
51
How do you reduce things to a 1D structure?
.flatten()
52
How do you ensure that two side by side graphs do not overlap?
Add tight_layout=False to plt.subplots()
53
How do you plot with a log scale on the y axis?
ax.semilogy
54
How do you sort values of a data frame?
df.sort_values("col", ascending=False)
55
How can you see the methods and attributes each object has?
dir(object)
56
How do we capitalise a string?
"hello".capitalize()
57
What does string1+string2
concatenates the two strings together
58
What is def __init__(self, vars)?
A special method run each time an instance is created.
59
What is the first argument of any method created within a class?
self This refers to the created instance of the object. This is used to refer to object methods.
60
How do we define a function?
def function(vars):