NumPy Introduction with Examples

If we are studying Pandas and Pandas, we must study NumPy as Pandas also includes NumPy. In this article, I’ll explain NumPy and explain some of its fundamental functions.

(This instructional video is part the Pandas Guide. Make use of the menu on the right to navigate. )

What is NumPy? NumPy It is an application which creates arrays. It allows you to create arrays of numbers using various precision and scales as well as strings, which is why it’s especially suitable for use in scientific computing.

Python in its own right has only floats as well as integers and imaginary numbers. However, NumPy can expand the capabilities of what Python is able to do as it can handle:

• 32-bit numbers
• 15 big numbers
• Numbers that are signed
• Unsigned numbers
• Plus

But that’s not all the reason to utilize NumPy. It’s built for efficiency and scale, which makes it the perfect platform for massive machines learning (ML) library such as TensorFlow.

Let’s now look at the basic features in NumPy arrays.

Making an array of NumPy

Create an array of items using np.array( ) .

Don’t use np.array(1,2,3,4,5) because 1,2,3,4,5 does not constitute an array. NumPy will interpret the items following the commas as elements to be passed of array(). array() function.

This results in an array:Copy

Numpy imports numpy using numpy.arr = np.array([1,2,3[1,2,3) arr

Results:Copy

array([1,2,3])

Shape of the array

A shape-based array can be described as exactly like, for example an array of 2×2 or 2×1 array.

Ask the shape to be reconstructed as follows:Copy

arr.shape

You’ll need to refer to this as an vector If you wish to know more about this because it’s not 3×1 since it’s only got one dimension and a blank isn’t a dimension.Copy

(3,)

It is called 3×1 as it’s an array made up composed of three arrays with a dimension of 1×1.Copy

arr = np.array([,,]) arr.shape

Results:Copy

(3, 1)

Redefining an array of

You can transform an array with the shape of m x N into any shape that is divisors of m x the number. This shapes (6,) can be modified to form 2×3 as 2*3=6 divides 6.Copy

import numpy as np arr = np.array([1,2,3,4,5,6]).reshape(2,3) print(arr)

Results:Copy

[[1 2 3] [4 5 6]]

Arange

It is important to note that this function is in no way arrange however it is it is arange like an the array. It can be used to create an array of numbers. (There are many methods to accomplish this which we’ll discuss in a future blog post.)Copy

Numpy imports numpy using np arr= np.arange(5) arr

Results:Copy

array([0, 1, 2, 3, 4])

Slice

Slice an array can be a challenging subject that gets easier after a few hours of practicing. Here are some easy examples.

Check out this assortment.Copy

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

It’s like this:Copy

array([[1, 2, 3], [4, 5, 6]])

(While it is possible to say that this is a two-row layout with three columns to simplify understanding however, this isn’t technically accurate. When you’re dealing with greater than 2 dimensions the idea of columns and rows is gone. That’s why it’s better to use the terms dimensions and Axes.)

The slices operations begins from the second position of the first axis. It will continue until the point where you are:Copy

arr[1:]

Results:Copy

array([[4, 5, 6]])

The process begins in the beginning, and then goes through the final:Copy

arr[0:]

Results:Copy

array([[1, 2, 3], [4, 5, 6]])

Use a comma in order to indicate the column to be:Copy

arr[:,1]

Results:Copy

array([2, 5])

Choose the opposite Axis like this:Copy

arr[1,:]

Results:Copy

array([4, 5, 6])

Select a single element.Copy

arr[1,0]

Results:Copy

4

Step

Copy

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

Copy

array([2, 4, 6])

This concludes the introduction.