I’ve been trying to flesh out my Python knowledge and learn more about machine learning(ML) in the process. Most of my day-to-day Python use is focused on text manipulation, API calls, and JSON parsing, so leveling up on ML is more math (specifically stat-related) than I’m used to.
Today I played around with the NumPy Python package a bit and figured out some simple things.
For example, if I wanted to multiply the numbers in two lists with vanilla Python, like this:
a = [1, 2, 3, 4] b = [4, 3, 2, 1] print(a * b)
I’d get TypeError: can’t multiply sequence by non-int of type ‘list’ . I’d have to write something to iterate through each list.
NumPy, on the other hand, can handle this like a champ. And this is probably the simplest thing you could use it for.
import numpy a = [1, 2, 3, 4] b = [4, 3, 2, 1] new_a = numpy.array(a) new_b = numpy.array(b) print(new_a * new_b) > [4 6 6 4]
NumPy really shines when you start dealing with multidimensional lists and stat work.
numpy.vstack((new_a,new_b)) > array([[1, 2, 3, 4], [4, 3, 2, 1]])
And then it’s just turtles all the way down. You can slice intersections, calculate standard deviations, and so on. It’s a handy Python package that I knew literally nothing about prior today and a nice tool to add to the toolbox.