...

/

Representing Data with NumPy

Representing Data with NumPy

In this lesson you will learn how to use NumPy to represent large, multi-dimensional arrays and matrices.

Introduction to NumPy

Sometimes, we need a data structure called arrays to represent a group of the same data type together. For this purpose, in Python, we can either use a list or use NumPy arrays. NumPy is a library for the Python programming language that adds support for large, multi-dimensional arrays and matrices, along with an extensive collection of high-level mathematical functions to operate on these arrays. To use NumPy, you will need to import it, as shown in the code snippet below.

Press + to interact
import numpy as np
print(np.__version__)

NumPy arrays

You can create an uninitialized array using the empty(shape, type) function. Alternatively, you can create an array of zeros or ones by using the zeros(shape, type) or ones(shape, type) function, respectively.

%0 node_1600697860071 0 node_1600697866256 0 node_1600697850497 0 node_1600697828720 0 node_1600697837070 0 node_1 0 node_2 0
Figure 1. One-dimensional array of length 7.

For creating a one-dimensional array, the argument shape has to be equal to the length of the array.

Press + to interact
import numpy as np
#creating an uninitialised array
arr = np.empty((2), dtype = int)
print("Empty array:", arr)
#creating an array of zeros
arr1 = np.zeros((3), dtype = int)
print("Array of zeros:", arr1)
#creating an array of ones
arr2 = np.ones((7), dtype = int)
print("Array of ones:", arr2)

For creating a two-dimensional or ...

Access this course and 1400+ top-rated courses and projects.