KRIGOLSON TEACHING
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops

Matrices

Python has a well-supported scientific computing ecosystem called SciPy. When we installed Anaconda, we also installed all the packages we need to take advantage of SciPy. In this tutorial, we will be learning how to work with matrices using NumPy.

NumPy is a library which makes working with arrays a similar experience to using MATLAB. First, let's bring NumPy into our workspace:

import numpy as np

Now we can work with any part of the NumPy library by typing "np."

Let's declare a matrix:

mat = np.ones((5,6), int)
print(mat)


You should see an output like this:
[[1 1 1 1 1 1]
 [1 1 1 1 1 1]
 [1 1 1 1 1 1]
 [1 1 1 1 1 1]
 [1 1 1 1 1 1]]


A 5x6 matrix consisting entirely of 1s. The int parameter tells NumPy to make all the entries of the array integers. We can omit this, as it is an optional parameter. If we did, we would have a matrix of floats, which are decimal numbers. 

The first parameter is a tuple, which is a grouping of two pieces of data. In this case, the (5,6) defines the shape of our matrix: 5 rows and 6 columns. 

NumPy has all sorts of handy ways to create an array:
https://docs.scipy.org/doc/numpy/reference/routines.array-creation.html


We can use most of the usual MATLAB commands for working with arrays:

Adding a constant
mat = mat + 1

Division
mat = mat / 2

We can also access specific parts of our matrix:

This pulls out the entire second column:
mat[:,2] 

This pulls out the entire second row:
mat[2,:]

We can operate on just this part of the matrix too:
mat[2,:] = mat[2,:] * 10

NumPy allows us to get pretty clever with indexing:
mat[:3,:]

This commands pulls out rows 0, 1, and 2 for mat. We can also pull out from row 3 onwards:
mat[3:,:]

This is called "slicing," and you can do in in any dimension of your arrays. You can even combine them:
mat[:3,:2]

This will pull out rows 0, 1, and 2, as well as columns 0 and 1. So if our matrix looked liked this:
[[0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4]]


Then mat[:3,:2] looks like this:
[[0 1]
 [0 1]
 [0 1]]


A few other useful commands for NumPy arrays are:

Transpose array x:
np.transpose(x)

Size and shape:

mat.size
Returns the size of the matrix (number of rows * number of columns)

mat.shape
Returns the shape of the array as a tuple (eg., (5,6)).

This is of course just the tip of the iceberg. NumPy is a very capable library, with options for loading arrays straight from files, and manipulating arrays in sophisticated ways. The NumPy User Guide is helpful to get started with NumPy, and the reference documentation fills in the details when you need it:

User Guide
https://docs.scipy.org/doc/numpy/user/index.html#user

Reference Documentation
https://docs.scipy.org/doc/numpy/reference/index.html#reference
  • NEUROSCIENCE
    • NEUROSCIENCE 100 >
      • NEURO 100 INTRODUCTION
      • NEURO 101 ADVANCED
      • NEURO 102 AGING
      • NEURO 103 MEMORY
      • NEURO 104 DECISION MAKING
      • NEURO 105 LEARNING
      • Research Statistics
    • NRSC 500B / MEDS 470
  • Kinesiology
    • EPHE 245
    • EPHE 357
  • STATISTICS
    • BIOMEDICAL STATISTICS
    • MULTIVARIATE STATISTICS >
      • MULTIPLE REGRESSION
    • RESOURCES
    • R TIPS
  • MATLAB
    • THE BASICS >
      • Hello World
      • BASIC MATHEMATICS
      • VARIABLES
      • Matrices
      • Writing Scripts
      • PATHS AND DIRECTORIES
      • USER INPUT
      • FOR LOOPS
      • WHILE LOOPS
      • IF STATEMENTS
      • RANDOM NUMBERS
    • STATISTICS >
      • LOADING DATA
      • DESCRIPTIVE STATISTICS
      • MAKING FUNCTIONS
      • BAR GRAPHS
      • LINE GRAPHS
      • TTESTS
    • EXPERIMENTS: THE BASICS >
      • DRAWING A CIRCLE
      • DRAWING MULTIPLE OBJECTS
      • DRAWING TEXT
      • DRAWING AN IMAGE
      • PLAYING A TONE
      • KEYBOARD INPUT
      • BUILDING A TRIAL
      • BUILDING TRIALS
      • NESTED LOOPS
      • RIGHT OR WRONG
      • SAVING DATA
    • EXPERIMENTS: ADVANCED >
      • STROOP
      • N BACK
      • Oddball
      • Animation
      • VIDEO
    • EEG and ERP Analysis >
      • ERP Analysis
      • FFT Analysis
      • Wavelet Analysis
  • Directed Studies
    • Advanced Topics in Motor Control A
    • Advanced Topics in Motor Control B
    • An Introduction to EEG
    • Advanced EEG and ERP Methods
    • Neural Correlates of Human Reward Processing
    • Independent Research Project
  • RESOURCES
    • EXCEL
    • HOW TO READ A RESEARCH PAPER
    • HOW TO WRITE A RESEARCH PAPER
  • Workshops
    • Iowa State EEG Workshop 2018
  • Python
    • The Basics >
      • Setting Up Python
      • Hello, world!
      • Basic Math & Using Import
      • Variables
      • Matrices
      • Scripts
      • User Input
      • For Loops