Numpy Functions To Get Started With Matrices In Python

September 16, 2019

After completing the first iteration of a neural network to process handwritten digits from the book “Make Your Own Neural Network” (a great intro to machine learning book you can read about here), I thought I’d write a bit about the numpy functions that greatly simplified the work that needs to be done to use matrices in the program.

Create a Numpy array

import numpy as n

# Create a list
list = [[1,2,3],[1,2,3]]

# Convert to a Numpy array
array = n.array(list, ndmin=2)

This function is how you use numpy to create a basic array of N dimensions with a list argument and some optional arguments such as ndmin, which is the minimum number of dimensions the returned array should have.

Transpose a Numpy array

import numpy as n

# Declare a list
list = [[1,2,3],[1,2,3]]

# Convert to transposed Numpy array
array = n.array(list, ndmin=2).T

This is a way to easily transpose any valid numpy array. If you aren’t familiar with the term, it simply means to reverse the dimensions of a matrix. If you call .T on a 2X3 array, the result will now be a 3X2 with values in the top right now being bottom left and bottom left now being top right, etc.  Remember that arrays may only be multiplied if their dimensions align properly. A 2×3 array can be multiplied with a 3×2 array because the number of columns of the first array match the number of rows of the second array.

Multiply two Numpy arrays

import numpy as n

# Declare 2 lists
list1 = [[1,2,3],[1,2,3]]
list2 = [[1,2,3],[1,2,3]]

# Convert to Numpy array
array1 = n.array(list1, ndmin=2)

# Transpose second array to allow multiplication
array2 = n.array(list2, ndmin=2).T

# Multiply arrays
result = n.dot(array1, array2)

This has a few functions depending on what you pass into it. You’ll probably use it for the most part on 2 dimensional arrays and in that case, it will handle matrix multiplications. Again, if your dimensions don’t align, try to transpose with the previous property (.T).

Cast array data types

import numpy as n

# Declare a list
list = [[1,2,3],[1,2,3]]

# Convert to Numpy arrays
array = n.array(list, ndmin=2)

# Cast resulting array to float types
float_array = n.asfarray(array)

This one is a little more situational but still handy to know. It can be read as “as float array” and casts all values inside the argument array to a float type. This was useful in formatting a new matrix of inputs for a neural network for python to evaluate as an array of floating point numbers instead of default integers.

Format .csv files for Numpy

import numpy as n

# Load a .csv file into python
csv_file = open("example.csv", 'r')

# Get the first line as a list item
csv_list = csv_file.readline()

# Close the file
csv_file.close()

# Split values by the occurrence of commas
list_for_numpy = csv_list.split(',')

# Convert to a Numpy array
array = n.array(list_for_numpy, ndmin=2)

This isn’t in numpy but is still extremely useful for using .csv files to create a numpy array. It accepts an argument of a character to parse and split the data into a Python list with. For example, if you call the function like the example above with csv_list being a variable holding the contents of a .csv file, split() will return the data in csv_list in a python array format that you can then use with numpy!

Leave a Reply