Getting Started with Linear Algebra in Python

Hello World!

So here I am after trying for a long time to not learn Python learning Python.  It just seems like I might get a hit or two more on my blog with some Python content.  Well whats the first thing I need to figure out aside from getting it up and running in my environment and installing some libraries… Thats right, find a numerical computing library and see how it ticks.

Lets just start with my environment, because I painstakingly chose one.

The IDE

I ended up with Visual Studio Community with the Python Tools plugin as my IDE.  Alright alright, before I hear all about how I work for Microsoft, hear me out.  First, I don’t get commission on Visual Studio or any of that, I get to use whatever I want and I have no ties to anything selling well.  I tried atom and I tried sublime and even the notebooks.  I was honestly hoping to find something like R Studio. If you know of it, please comment with a link!  The core differentiator for Visual Studio is multi-Project support with the full tool chain in each language so I can actually deliver something, the great version control tie ins, debugging and performance profiling capabilities just to name a few.  You can go read all about it on the Visual Studio website.

The Environment

So I got the pleasure of learning about Python environments.  Those are pretty groovy, I have to say that is a nice feature.  I wouldn’t say it is a convert me to a python dev feature as most of my environments I think will end up being the same thing, so in brand new projects it probably saves me 10 minutes realistically.  Anyways, I don’t know too much, so I just grabbed Anaconda.  Seemed like a great place to start.  Had a variety of machine learning and scientific computing libraries that are widely adopted.  For future, I will be adding the NVidia deep learning sdk as well as the plotly packages.

Getting Set Up

So getting set up is really quite easy.  I’m not going to document it, its already well documented.

  1. Just watch this video tutorial

SHOW ME LINEAR ALGEBRA ALREADY

Was that enough of a tangent?  Alright alright, here you go.  Its really simple.

import numpy as np

#define a vector size 4
a = np.array([1,2,3,4])

#define a 2x4 matrix
b = np.array([[ 1, 2, 3, 9], 
              [ 4, 5, 6, 9]])
#define a 2x4 matrix
c = np.matrix([[ 1, 2, 3, 9], 
              [ 4, 5, 6, 9]])

d = np.matrix([1,2,3,4])
#numpy adheres to traditional math notation.
#does not work - 1x4 * 2x4 matrix
#does not classify as matrix/vector mult, its vector/matrix -> fail.
a.dot(b) 
#works as expected, identifies special case
#as we provided standard math format
b.dot(a)
#as such to square b, we need to first transpose the left side.
b.transpose().dot(b)
#element wise multiplication
b * b
#using matrix structure to get
#nice default * behavior
d * b.transpose()

One thing I want to call out is b * b.  This is element wise because it is default defined as an array.  If you redefine every thing as np.matrix everything behaves like a matrix by default and you get some nice syntax.

Summary

So there you go at this point, you are ready to re-implement all of my F# machine learning linear algebra articles into python :).  Be sure to share the code.

2 thoughts on “Getting Started with Linear Algebra in Python

    • Any performance considerations with sympy? This looks very interesting. End goal is to be running deep neural nets in python and .net on nvidia deep learning boxes.

Leave a Reply

Your email address will not be published. Required fields are marked *