Session 4 - Part 2 Flashcards
Although we are processed small amounts of data like single numbers or long lists, neuroimagning data however,
contains millions of numbers and processing these one ar a time with loops is not going to work
For larger scale data analysis (usually with neuroimagning data) we can use pre-existing modules and core tool for this in Python is
numpy
Numpy works with
large blocks of numbers called ‘matrices’ (or sometimes ‘arrays’)
Matrices are often rectangular blocks that are
stored as rows x columns
Matrices are often rectangular blocks that are stored as rows x columns.
For instance:
3 rows by 2 columns so it would have shape (3, 2).
The order of the dimensions of matrices is always
(rows, columns).
For most of the time we will be working with 2D matrices, however numpy will cope with any number of dimension from
(from 1D up to any number of dimensions).
Matrices are usually two-dimensional whereas arrays can be
have any number of dimensions
when performing addition, subtraction, multiplication, or division on matrices, they must be of
he same size, meaning they must have the same number of rows and columns.
Example of addition of matrices
Example of subtraction of matrices:
Example of multiplication and division of matrices - element by element mulplication/division|
numpy and scipy are two modules that support for much of the
scientific work that you will want to do in Python
numpy provides “low-level” routines for manipulating matrices like
linear algebra routines (e.g. inverting a matrix), Fourier transforms and random number capabilities (using a range of distributions).
scipy provides “higher-level” routines for science and engineering and uses numpy as base and then adds
additional algorithms for things like image and signal processing.
matplotlib is a
plotting library that lets you produce a range of different graphs
Numpy uses the numpy.array class to store
arrays of numbers
Numpy arrays can be any
number of dimensions and any size
Python is a 0-indexed language. This means that if we have a2x2 array
the two rows are referred to using indices 0 and 1 and the two columns are referred to using indices 0 and 1.
When indexing into numpy arrays, we use
square brackets (similar to lists).
We can also use a range separated by a colon (:) in array which is exclusive of last number (similar to range function) e.g., - (2)
data[0:100, 0:100] This gives us a 100x100 subarray of the data array
data[50:, :] This gives us the 51st row onwards and all of the columns –> In Python, indexing starts from 0, so the 51st row corresponds to index 50. T
Explain the code please - (5)
import numpy as np: This line imports the NumPy library and aliases it as np, allowing us to refer to NumPy functions and objects using the shorter name np.
z = np.zeros((2, 3)): This line creates a 2x3 matrix filled with zeros.
The numbers (2, 3) inside the parentheses represent the dimensions of the matrix, with 2 rows and 3 columns.
print(‘Here is an array of zeros’): This line prints a descriptive message to indicate that the following array contains all zeros.
print(z): This line prints the array z containing zeros to the console.
What is the output of the code?
Here is an array of zeros
[[0. 0. 0.]
[0. 0. 0.]]
What does z[0, 0] = 1.0 do? - (2)
z = np.zeros((2,3)) producign 2x3 matrix of zeros
This line sets the element at the first row and first column of the matrix z to 1.0.
In other words, it modifies the top-left element of the matrix.
What does z[0, :] = 2.0 - (2)
z = np.zeros((2,3)) producign 2x3 matrix of zeros
This line sets all elements in the first row (all columns) of the matrix z to 2.0.
It fills the entire first row with the value 2.0.
What does z[:, 1] = 3.0- (2)
z = np.zeros((2,3)) producign 2x3 matrix of zeros
This line sets all elements in the second column (all rows) of the matrix z to 3.0.
It fills the entire second column with the value 3.0.
What does z[0, :] = 2.0 - (2)
z = np.zeros((2,3)) producign 2x3 matrix of zeros
This line sets all elements in the first row (all columns) of the matrix z to 2.0.
It fills the entire first row with the value 2.0.
What does z[:, 1] = 3.0- (2)
z = np.zeros((2,3)) producign 2x3 matrix of zeros
This line sets all elements in the second column (all rows) of the matrix z to 3.0.
It fills the entire second column with the value 3.0.
What does single_num = z[0,1] do if we print it out? - (2)
z = np.zeros((2,3)) producing 2x3 matrix of zeros
Extracts a single number from the first row and first column of the matrix
The item at z[0,1] is 0.0
What does a_row = z[0,:] do if we print it out? - (3)
z = np.zeros((2,3)) producing 2x3 matrix of zeros
In the context of array indexing in NumPy, the comma separates the row index from the column index.
In this expression, z[0, :], 0 indicates the row index (specifically the first row), and : indicates that all columns in that row are selected.
Output:
[0. 0. 0.]
So with matrices we can set
certain elements to be certain values as well as extract single and multiple numbers
What does rArray=np.random.rand(200,200) do?
This line initializes a NumPy array named rArray with dimensions 200x200 filled with random numbers generated using np.random.rand().
What does print(f’Array size={rArray.size}’) display?
if rArray=np.random.rand(200,200)
This line prints the total number of elements in the rArray array which is 4000
What does rint(f’Array shape={rArray.shape}’)display?
if rArray=np.random.rand(200,200) - (2)
This line prints the shape of the rArray array, which represents its dimensions as a tuple (rows, columns)
output: Array shape=(200, 200)
What does smallerArray=rArray[0:100,0:100] do
if rArray = np.random.rand(200,200)
This line creates a subset of the rArray array, selecting rows from index 0 to 99 and columns from index 0 to 99, effectively creating a smaller 100x100 array from the original 200x200 array.
: How can the size parameter in np.zeros be represented, according to the comment # The size thing here is a tuple - it can also be a list?
The size parameter in np.zeros can be represented as either a tuple or a list, allowing flexibility in specifying the dimensions of the array to be created.
Explain the code - (4)
import numpy as np
z = np.zeros((2, 3)) # The size thing here is a tuple - it can also be a list
print(‘Here is an array of zeros’)
print(z) #array of 2x3 zeros
print(‘Now I set the bottom right element (1,2) to 9’)
z[1,2]=9 #Work out how to do this
print(z [1,:])
This code snippet begins by importing the NumPy library and aliasing it as np.
It then initializes a 2x3 NumPy array named z filled with zeros.
The line z[1, 2] = 9 modifies the bottom-right element of the array to 9.
Finally, it prints the second row of the array using print(z[1, :]), where z[1, :] selects the entire second row of the array.