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
Q

How do you create an array of ones?

A

np.ones((2,2))

26
Q

How do you create a constant array?

A

np.full((2,2), 7)

27
Q

How do you create an array of random numbers?

A

np.random.random((4,4))

28
Q

What is the importance of being able to generate random numbers?

A

In machine learning random numbers are important.

Eg to make an initial guess

29
Q

How do you add/subtract/multiply a matrix by a scalar?

A

a * 2
a + 2
a - 2

30
Q

How do you perform element-wise addition/subtraction/multiplication/division of matrices?

A

a+b
a-b
a*b
a/b

31
Q

How do you perform matrix multiplication?

A

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
Q

How do we create a replicate of an array, rather than just pointing to the same point in memory?

A

b = a.copy()

33
Q

How do we import pyplot?

A

import matplotlib.pyplot as plt

34
Q

How do we plot data?

A

plt.plot(x, y)

35
Q

How do we add an x or y label?

A

plt.xlabel(“x”)
plt.ylabel(“y”)

36
Q

What must we add to the end of each plot?

A

plt.plot()

37
Q

What do we need to do when plotting two curves on one plot?

A

Add labels to distinguish the curves.

eg plt.plot(x, y, label=”sin(x)”)

And need to add
plt.legend()

38
Q

How do we see what curve corresponds to what function?

A

plt.legend()

39
Q

How do we plot curves of functions?

A

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
Q

How do you customise the line colour and style of a plot?

A

Add argument “r–”

eg plt.plot(x, y, ‘r–’, label = ‘y = sin(x)’)

“r–” - red dashed line
“k-“ - solid black line

41
Q

How do you plot two curves side by side?

A

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
Q

How do we import pandas?

A

import pandas as pd

43
Q

How do we create a data frame?

A

From an array:
pd.DataFrame(array)

From a dictionary:
pd.DataFrame(dictionary, index=col_name)

44
Q

How do we make a data series or a data frame from a data frame column?

A

df[[‘col_name’]]

[] - data series
[[]] - keep as dataframe

45
Q

How do you select a row by index label?

A

df.loc[[“label”]]

46
Q

How do you load in a dataset from a file?

A

pd.read_csv(‘file.csv’,index_col=0)

47
Q

How do you plot information in a dataframe?

A

df.plot(x=”col”, y=”col”, kind=”scatter”)

48
Q

How do you change the scale of a plot?

A

plt.yscale(‘log’)

49
Q

How do you plot by a categorical variable?

A

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
Q

How do you extract the values of a column in a dataframe?

51
Q

How do you reduce things to a 1D structure?

A

.flatten()

52
Q

How do you ensure that two side by side graphs do not overlap?

A

Add tight_layout=False to plt.subplots()

53
Q

How do you plot with a log scale on the y axis?

A

ax.semilogy

54
Q

How do you sort values of a data frame?

A

df.sort_values(“col”, ascending=False)

55
Q

How can you see the methods and attributes each object has?

A

dir(object)

56
Q

How do we capitalise a string?

A

“hello”.capitalize()

57
Q

What does string1+string2

A

concatenates the two strings together

58
Q

What is def __init__(self, vars)?

A

A special method run each time an instance is created.

59
Q

What is the first argument of any method created within a class?

A

self

This refers to the created instance of the object. This is used to refer to object methods.

60
Q

How do we define a function?

A

def function(vars):