Although it may sound like the name a child would give to a beloved toy stuffed animal, NumPy refers to Numerical Python. It is an open-source library in Python that aids in mathematical and numerical calculations and computations; and, scientific, engineering, and data science programming. NumPy is an essential library used to perform mathematical and statistical operations. It is especially suited for multi-dimensional arrays and matrix multiplications. This NumPy tutorial focuses on the essential things you need to know to master NumPy.
In this NumPy tutorial article, we'll be covering the following topics:
Learn data operations in Python, strings, conditional statements, and more with the Python Training course. Enroll now!
In this Numpy tutorial, we will be using Jupyter Notebook, which is an open-source web application that comes with built-in packages and enables you to run code in real-time.
Let’s get started by importing our NumPy module and writing basic code.
NumPy is usually imported under the np alias.
Fig: Basic NumPy example
You can also make more than a one-dimensional array.
Fig: 2-D NumPy array
The array object in NumPy is called ndarray, which means an N-dimensional array.
To create ndarray in NumPy, we use the array() function.
Fig: NumPy array
The ndim() attribute can be used to find the dimensions of the array.
Fig: ndim function
The itemsize() is used to calculate the byte size of each element.
Fig: itemsize()
Each item occupies four bytes in the above array.
The dtype attribute is used to understand the data type of the given element.
Fig: dtype
This array attribute returns a tuple consisting of array dimensions.
Fig: shape
This means the array has two dimensions, and each dimension contains two elements.
The reshape() function is used to reshape the array.
Fig: reshape()
Now the array has three dimensions, with two elements in each dimension.
Slicing is used to extract a range of elements from the array.
Fig: Slicing
Zero represents the index of the array, and one indicates the element of the mentioned array.
The random module’s rand() method returns a random float between zero and one.
Example:
Fig: random()
We have generated a three-dimensional array with two elements in each dimension.
The randint() method takes a size parameter where you can specify the shape of the array.
Example:
Fig: random.randint()
The code above will generate a 2D array with three rows, and each row will contain four random integers between zero and 10.
The mean() function is used to compute the arithmetic mean of the given data along the specified axis.
Example:
Fig: mean()
The median() function is used to compute the arithmetic median of the given data along the specified axis.
Example:
Fig: median()
The std() function is used to compute the standard deviation along the specified axis.
Example:
Fig: std()
The append() function is used to add new values to an existing array.
Example:
Fig: append()
The insert() function inserts the value in the input array along the mentioned axis.
Syntax: numpy.insert(arr, obj, values, axis)
Example:
Fig: insert()
The concatenate() function is used for joining two or more arrays of the same shape along the specified axis.
Example:
Fig: concatenate()
In NumPy, basic mathematical functions operate elementwise on an array. Let’s look at some examples to understand this more clearly.
Fig: Mathematical operations
Fig: Output
Broadcasting is NumPy’s ability to perform mathematical operations on arrays with different shapes.
The following example demonstrates how to multiply two arrays:
Example:
In the preceding example, the array was the same shape, and therefore multiplication was simple. However, if we consider arrays of different sizes, we will receive an error message.
Example:
NumPy can perform such operations using the concept of broadcasting.
In order to broadcast two arrays together, they must meet the following criteria:
Example:
Fig: NumPy broadcasting
In this NumPy tutorial article, we discussed the basic concepts of NumPy. We looked at how to create a NumPy array, and its different functions. We went through several mathematical operations on NumPy using broadcasting.
If you have any questions, please share them in the comments section, and we'll have our experts answer them for you.
Web developers with the latest skills in modern tools such as NumPy are in high demand. If you are looking to upskill and take your career to the next level, check out our Full Stack Web Developer - MEAN Stack Master’s program today. You can learn from anywhere and at your own pace with our award-winning, online applied learning model. What are you waiting for?
Aryan is a tech enthusiast who likes to stay updated about trending technologies of today. He is passionate about all things technology, a keen researcher, and writes to inspire. Aside from technology, he is an active football player and a keen enthusiast of the game.