Level up your programming with data science
By: Randy Taylor
Tags: Javascript Development, Python Programming, Numpy, React, AI

JavaScript was created in 1995 and did not get any traction for 10 years. Then in 2005 AJAX revolutionized how we use browsers and the web. You can read the actual paper that did it here. If you have a minute to go down a rabbit hole I would recommend reading it. To summarize before this you would have to wait and refresh the whole browser before you got new data. With this new method you could simply update parts of your browser through JavaScript. Not surprisingly this is why React has been so successful and taken over the current Front-End web dev landscape. React only updates the parts of the page that need to and leaves the remaining parts alone. Since this paper JavaScript has taken over software and become part of the modern revolution of hi-tech human interaction.

It is no coincidence enabling people to communicate, sell, buy, share, connect, friend, offer up, lift, and “like” has become one of the must important things to know and in demand skills to have. This new mode of communication and the businesses behind them make money. Money drives the world and if you know this tech you can get a job helping the company rake in that money. We can all take a minute to thank Capitalism and Darwin’s theory of evolution for the JavaScript revolution.

However something is rotten in the state of Denmark. JavaScript has it’s limitations. JavaScript is stuck in the browser for example. This is effectively true. There are other places to use JavaScript if you do the necessary prep work. JS is also really bad at big math. You can read more about it in my previous post about AI in JavaScript.

In that AI/JS mashup you will see it integrating python with some middleware. So lets look at Python which has had it’s JS 2005 moment of blastoff in software. I’ll argue it happened around 2015 but I am sure there are others who disagree with that.

Python is a dynamically typed language. This means the interpreter is figuring out what the types are at runtime. This can cause errors and a good linter program or extension will catch things but you can get TypeErrors sneaking into your code. Python is also a high-level programming language. This means you’re not worrying about how you’re managing your memory. Because of this you can spend your time thinking about the code you want to write not how to manager your memory/computer/waiting for it to compile.

Python itself is opensource also has many opensource libraries. These will help you do things like tensorflow, Django, or numpy. Numpy is a math library that lets you do all the really cool stuff you need in Data Science and Machine Learning.

If you are a programmer you will have a slight interest in Artificial Intelligence and Machine learning. What you may not know is part of those fields is Data Science. Data Science is the study of Data. I remember in my University of Arizona physiology 201 class PhD Gene Settle said in the future there will be a data overload problem. He said something like “We will have too much data and not know what to do with it”. Data Science attempts to solve this problem by taking data and making sense out of it. Our standard programming languages(python) can usually do this but we can make processing this data faster and more optimal. That’s what numpy does.

Numpy is a python library written is C and C++ and is uber quick and at getting your data processed. The Array data structure comes with numpy and has many methods for you to work with.

You can turn a python list into an Array with numpy.array(my_python_list).

If you have a CSV file can use the import method with numpy.genfromtxt(csvfile, delimiter). Here you can import CSV files (comma separated value) which are a file type ubiquitous is business. With this method you must specify the “delimiter” or thing that separates the values, a comma being the most common.

genfromtxt('file.csv', delimiter=",")

You can use multiple arrays to create matrix. Here you will be working with the axes x and y. However they are called 0 and 1 respectively with numpy. Now you can use the axes to pick out the data you want so numpy.array[0,1] would give you the 0 index on the column(which x coordinate), and the 1st index on the row(which y coordinate). You can select entire rows or columns with the bracket notation

matrix = np.array([[1,2,3],[4,5,6]])

matrix[:,2] #=> returns the last column from every row [[3],[6]]

matrix[1,:] #=> returns the entire second row [[4 5 6]]

Those are the basics but soon we will start to see all the greatness of numpy. For example you can use comparison operators on the array/matrix. Now this will give you the Boolean value back.

matrix > 1 #=> [[False True True] [ True True True]]

However if you go further and put the comparison operator in bracket notation you will get the values back that pass as True.

matrix[matrix > 1] => [[2 3 4 5 6]]

Linear Algebra method that can be very useful to that Data Scientist. In linear algebra will help you work with vectors.

I learned about vectors at the UofA as speed + direction in physics. However as Computer Scientists we will think of them as a list of numbers. Think about those numbers as on the x,y cartesian coordinate system.

vector = [1,2]

So here we move 1 direction on the x axis and 2 directions on the y axis. We can create a vector in 3 dimensional space by adding the z axis.

vector = [1,2,3]

Numby will let us add, divide, multiply, or subtract a “scalar” to our arrays or matrices where every element will undergo the operation. So for example we want to add 5 to every element in our vector array we can perform

vector_plus_5 = vector + 5 #=> [6,7,8]

We have to assign a new variable as numpy does not alter the original array and will return the the new array/matrix with the scalar added to each element.

We can also add multiple arrays together if they have the same dimensions.

person_daily_pay = [100, 200, 150, 150]

person2_daily_pay = [110, 120, 160, 160]

person3_daily_pay = [110, 130, 150, 190]

total_pay = person_daily_pay + person2+daily_pay + person3_daily_pay

total_pay #is an array of the total pay for all people each day

You can access this data with indices such as

person_daily_pay[0] #=> 100

You can even use negative indices to count backward from the end of the array such as

person_daily_pay[-4] #=> 100

We can access ranges of data such as

person_daily_pay[0:2] #=> [100,200] as you see the first is included and the second is excluded and why we get the zero and first element

As a reference you can think of accessing this Numpy.array this way: You will use bracket notation where the first element is the row and the second is the column. So Numpy.array[0,1] will give you the element in the index 0 row at index 1 column. You can also grab multiple with Numpy.array[ startIndex : endIndex_not_included] e.g. Numpy.array[ 0:2, 0:2] would give you the first two rows and the first 2 columns.