Want to learn more? Take the full course at https://learn.datacamp.com/courses/object-oriented-programming-in-python at your own pace. More than a video, you’ll learn hands-on coding & quickly apply skills to your daily work.
Now that we’ve learned about the basics of OOP, let’s talk about NumPy, the library that’s behind the Python libraries we use every day, like Pandas.
NumPy is a package for scientific computing in Python that includes the ability to do linear algebra.
NumPy was created because many scientific computing operations are based on a mathematical structure known as a matrix, known in numpy as arrays. Arrays are essentially data grouped by rows and columns.
In data science, NumPy is used when data for analysis often needs to be laid out as either a matrix or a database table.
For example, we use matrices when creating input datasets for modeling, when performing an aggregated analysis, or when creating linear algebra matrices as calculations in recommendation algorithms.
NumPy fundamentals are used to build out all the Python libraries we use on a daily basis.
Pandas documentation shows that pandas Series objects, which make up DataFrames, are an ndarray. An ndarray is another name for a NumPy array.
Think of NumPy arrays as similar in structure and shape to Python lists. However, they’re much more dynamic.
Unlike lists, NumPy Arrays can be n-dimensional, so more than one dimension, called numpy-dot-array or ndarray and they only take one datatype (such as a string or int) at a time.
The visualization shows several examples of arrays, along with their dimensions. Starting on the left-hand size, you can see that the 1D array looks most like a list. Once you have a 2D array in the middle, you have multiple axes. A 3-D array becomes more complicated.
We’ll deal with only 1 and 2-D arrays in this course.
To create an array, we import NumPy as np so we can use the array method in the first line. In the second line, we create an np-dot-array of one-dimension, which includes the members 2,3, and 4, and we create a variable, our_array, as a reference to the array.
In the third line, we can print the array, and we see that we return an array object.
If we introspect, or print, our_array’s type, we can see that the type is now a numpy array.
In the last slide, we printed an example of a very simple array, but NumPy arrays can come in all shapes and sizes.
The key element is the axis, which is another name for row or column. Axis 0 refers to rows. Axis 1 refers to columns. Think of each axis as a Python list. So example one has three elements along axis 0, while example 2 has one.
Each axis has a given number of elements. So example 1 has 5 elements, while example 2 has three elements.
There are many other array features, but we’ll keep it simple to really get at the heart of what we want to use arrays for, which is to build objects.
Now let’s try some examples.