Alex Shum bio photo

Alex Shum

Data Scientist at Apple.

Twitter LinkedIn Github Stackoverflow

Some examples of using numpy and scipy coming from R. Generally numpy will contain the data structures for vectors and matrices. Scipy will contain the linear algebra and other numerical algorithms. Due to compatability issues some linear algebra functions that would be better under scipy are also contained under numpy.

Basic array operations

import numpy as np

#define a vector
v1 = np.array([1,2,3,4,5])
v2 = np.array(np.arange(15))
v3 = np.arange(5)

#array array operation
v1 + v3 #vector addition
v1 + v2 #error
v1 * v3 #element wise multiplication

#scalar array operations
v1 + 1
v1 - 1
v2 / 5

#2d arrays
w3 = np.array([[1,0,0],[0,1,0],[0,0,1]])
w4 = np.array([[2,0,0],[0,2,0],[0,0,2]])

Note here that arrays are not vectors in the mathematical sense. All basic math operations will be element-wise for both array-array and array-scalar operations.

Basic matrix operations

from numpy import *

#define a matrix
m1 = matrix('1 2 3; 4 5 6; 7 8 9')
m2 = matrix(w3)
m3 = matrix([[3,0,0],[0,3,0],[0,0,3]])

#matrix matrix operation
m1 + m3
m1 * m3
m1.T #transpose
m3.I #inverse

#array matrix operation
v1 = v1[0:3] #take first 3 elements from previously defined v1
v1 + m1 #adds array v1 to each row of matrix m1
v1 * m1 #this will do a vector matrix multiplication

#elementwise operations
m3 / 3
multiply(m1, 2)

Arrays are the “base” object in numpy and more advanced types like matrices are an extension of arrays. Remember that python is object oriented so matrices and arrays are both objects.

Merging vector/matrices

from numpy import * 

#rbind / cbind
a1 = array([[1,2,3,4],[2,3,4,5],[3,4,5,6]])
append(a1, [[4,5,6,7]], axis = 0) #rbind
append(a1, [[5],[6],[7]], axis = 1) #cbind

For append note that difference between adding a column and adding a row.

Indexing arrays/matrices with conditions

from numpy import * 
a1 = array([1,2,3,4])

#return a array of booleans 
a1 > 1

#finding index positions given a condition
where(a1 > 1)

#subsetting arrays using conditions
a1[a1 > 1]
a1[where(a1 > 1)]

Arrays can be indexed with both sequences of index positions and booleans. This also allows us to subset arrays/matrices using inequalities and other conditions. Same lessons apply to matrices.