Numpy Arrays

Sean Oughton
4 min readApr 26, 2019

--

Numpy is a great library for python for dealing with multi-dimensional arrays and matrices

Creating Arrays

  • From a list — To create an array from a list:
my_list1 = [1,2,3,4]
my_array1 = np.array(my_list1)
  • creating a matrix
my_list1 = [1,2,3,4]
my_list2 = [11,22,33,44]
my_lists = [my_list1, my_list2]
my_array2 = np.array(my_lists)
  • array shapes — To specify the shape of an array or matrix, pass in the shape inside square brackets
np.ones([5,5])
[[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.]]
  • Identity Matrix — It is a square matrix in which all the elements of the principal diagonal are ones and all other elements are zeros. The effect of multiplying a given matrix by an identity matrix is to leave the given matrix unchanged. To create an identity matrix:
np.eye(5)
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]

Scaling Arrays

  • Multiplying Arrays by themselves — It squares each element in each array.
arr1 = np.array( [[1,2,3,4],[8,9,10,11]] ) 
arr1*arr1

the result is

[[  1   4   9  16] 
[ 64 81 100 121]]
  • Subtracting Arrays- subtract each element of each array by itself
arr1 = np.array( [[1,2,3,4],[8,9,10,11]] )
arr1-arr1

the result is

[[0 0 0 0] 
[0 0 0 0]]
  • Dividing Arrays- 1/arr1 , the same as every value of the array to the power of negative 1
1/arr1

results in

[ [1.  0.5  0.33333333 0.25 ]
[0.125 0.11111111 0.1 0.0909090] ]
  • Exponential Operations
arr1** 3

results in

[[   1    8   27   64]
[ 512 729 1000 1331]]

Reshaping Arrays

Converting an array into a matrix

arr = np.arange(20)[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 1617 18 19]arr = np.arange(20).reshape(2,10)[[ 0  1  2  3  4  5  6  7  8  9]
[10 11 12 13 14 15 16 17 18 19]]

Transposing Arrays

  • This converts columns to rows. It’s kind of like turning the array(matrix) on it’s side, as the row gets transposed as the column.
  • The original matrix
arr = np.arange(50).reshape(10,5)
[
[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]
[20 21 22 23 24]
[25 26 27 28 29]
[30 31 32 33 34]
[35 36 37 38 39]
[40 41 42 43 44]
[45 46 47 48 49]
]
  • The Transposed Matrix
arr.T
[
[ 0 5 10 15 20 25 30 35 40 45 ]
[ 1 6 11 16 21 26 31 36 41 46 ]
[ 2 7 12 17 22 27 32 37 42 47 ]
[ 3 8 13 18 23 28 33 38 43 48 ]
[ 4 9 14 19 24 29 34 39 44 49 ]
]

Create a 3D Matrix

  • This will create 5 arrays, that contain 5 rows(arrays), and each of those inner arrays will have 2 columns
arr = np.arange(50).reshape(5,5,2)

Dot Product

  • An algebraic operation that takes two equal-length sequences of numbers (usually coordinate vectors) and returns a single number.
np.dot(arr.T,arr)

Universal Array Function — Basic functions that you can apply to every value in an array

  • Square Root
arr = np.arange(11)np.sqrt(arr)
  • Exponent
np.exp(arr)
  • Binary Functions (requires 2 Arrays)

Add — add every value of A to every value of B

np.add(A,B)

Max/Min — between two arrays

np.max(A,B)
np.min(A,B)

Array Processing

  • Mesh Grid — returns coordinate matrices from the vectors you give it.
points = np.arange(-5,5,.01)# griddx,dy = np.meshgrid(points,points)
[[-5. -4.99 -4.98 ... 4.97 4.98 4.99]
[-5. -4.99 -4.98 ... 4.97 4.98 4.99]
[-5. -4.99 -4.98 ... 4.97 4.98 4.99] ...
[-5. -4.99 -4.98 ... 4.97 4.98 4.99]
[-5. -4.99 -4.98 ... 4.97 4.98 4.99]
[-5. -4.99 -4.98 ... 4.97 4.98 4.99]]
  • Use matplotlib to visualize that data
z = ( np.sin(dx) + np.sin(dy) ) 
plt.imshow(z)
plt.colorbar()
plt.title('Plot for sin(x)+sin(y)')
  • numpy where — use list comprehension to create an array using boolean logic

Using List comprehension it would look like this:

A = np.array([1,2,3,4])
B = np.array([100,200,300,400])
condition = np.array( [True,True,False,False])
answer = [(A_val if cond else B_val) for A_val,B_val, cond in zip(A,B,condition)]
[1, 2, 300, 400]

Using numpy where it would look like this:

answer2 = np.where(condition,A,B)
  • Sum — sum every value in an array
arr.sum()
  • Mean
arr.mean()
  • Standard Deviation — a quantity calculated to indicate the extent of deviation for a group as a whole. Used to tell how measurements for a group are spread out from the average (mean), or expected value. A low standard deviation means that most of the numbers are very close to the average. A high standard deviation means that the numbers are spread out.
arr.std()
  • Variance — The expectation of the squared deviation of a random variable from its mean. It measures how far a set of (random) numbers are spread out from their average value. A way to measure how far a set of numbers is spread out. Describes how much a random variable differs from its expected value.
arr.var()
  • Any — returns a true or false if anything in an array is true
boolean_arr.any()
  • All — returns true if all values in the array are true
boolean_arr.all()
  • Sort — sorts the values in an array
arr.sort()
  • Unique — returns unique values from the array
np.unique(arr)

--

--