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.]]