Professional Documents
Culture Documents
Importing NumPy
Numpy array has the various function, methods, and variables, to ease
our task of matrix computation.
Elements of an array are stored contiguously in memory. 3
1-D , 2-D or N-D Numpy Arrays
4
NumPy Arrays Vs Lists
5
Benefits of NumPy Array over
Lists
1) Numpy Arrays consumes less memory as compared to Lists.
In this example, a Python list and a Numpy array of size 1000 will be created. The
size of each element and then the whole size of both the containers will be
calculated and comparison will be done in terms of memory consumption.
import numpy as np
import sys
# declaring a list of 1000 elements
S= range(1000)
# printing size of each element of the list
print("Size of each element of list in bytes: ",sys.getsizeof(S))
print("Size of the whole list in bytes: ",sys.getsizeof(S)*len(S))
# declaring a Numpy array of 1000 elements
D= np.arange(1000)
print("Size of each element of the Numpy array in bytes: ",D.itemsize)
print("Size of the whole Numpy array in bytes: ",D.size*D.itemsize)
6
Benefits of NumPy Array over
Lists
2) Numpy Arrays are fast as compared to Python Lists.
In this example, 2 Python lists and 2 Numpy arrays are created, and each container has
1000000 elements. Multiplication of elements in both the lists and Numpy arrays
respectively is carried out and the difference in time needed for the execution for both
the containers is analyzed to determine which one takes less time to perform the
operation.
import numpy
import time
size = 1000000
list1 = range(size)
list2 = range(size)
array1 = numpy.arange(size)
array2 = numpy.arange(size)
# capturing time before the multiplication of Python lists
initialTime = time.time()
# multiplying elements of both the lists and stored in another list
resultantList = [(a * b) for a, b in zip(list1, list2)]
7
Benefits of NumPy Array over
Lists
Example Continue...
8
Benefits of NumPy Array over
Lists
3) Numpy Arrays support element-wise operations, Lists don’t.
In this example, the incapability
import numpy as np of the Python list to carry out a basic operation is
demonstrated. A Pythona list
# declaring listand a Numpy array having the same elements will be
declaredlsand=[1,
an integer
2, 3]will be added to increment each element of the container by that
integer value
arr without looping statements. The effect of this operation on the Numpy array
= np.array(ls)
and Python list will be analyzed.
try:
# adding 4 to each element of list
ls = ls + 4
except(TypeError):
print("Lists don't support list + int")
# now on array
try:
# adding 4 to each element of Numpy array
arr = arr + 4
print("Modified Numpy array: ",arr)
except(TypeError):
print("Numpy arrays don't support list + int") 9
NumPy Array Creation Routines
Numpy arrays can be created in different ways:
1. By using numpy.empty() function : The empty routine is used to
create an uninitialized array of specified shape and data type.
Syntax:
numpy.empty(shape, dtype = float, order = 'C')
• Shape: The desired shape of the specified array.
• dtype: The data type of the array items. The default is the float.
• Order: The default order is the c-style row-major order. It can be set to
F for FORTRAN-style column-major order.
10
NumPy Array Creation Routines
2. By using numpy.zeros() function : This routine is used to create the
numpy array with the specified shape where each numpy array item is
initialized to 0.
Syntax:
numpy.zeros(shape, dtype = float, order = 'C')
11
NumPy Array Creation Routines
3. By using numpy.ones() function : This routine is used to create the
numpy array with the specified shape where each numpy array item is
initialized to 1.
Syntax:
numpy.ones(shape, dtype = float, order = 'C')
12
NumPy Array Creation Routines
4. By using numpy.full() function : This routine is used to create the
numpy array with the specified shape where each numpy array item is
initialized to 1.
Syntax:
numpy.full(shape, fill_value, dtype = None, order = ‘C’)
13
NumPy Array Creation Routines
5. By using numpy.arange() function : This routine is used to create one
dimensional numpy array with specified start, end and step values.
Syntax:
numpy.arrange([start,] stop[, step,][, dtype])
1. numpy.asarray() Function :
This routine is used to create an array by using the existing data in the
form of lists, or tuples.
This routine is useful in the scenario where we need to convert a python
sequence into the numpy array object.
Syntax:
numpy.asarray(sequence, dtype = None, order = None)
sequence: Input data in any form such as list, list of tuples, tuples, tuple of
tuples or tuple of lists
dtype: By default, the data type of input data is applied to the resultant
ndarray
order: (row major) or F (column major). C is default
15
Arrays from Existing Data
numpy.asarray() Examples :
1 2 3
16
Arrays from Existing Data
Difference in numpy.array() and numpy.as array():
17
Arrays from Existing Data
Example : import numpy as np
lst = [1, 7, 0, 6, 2, 5, 6]
# converting list to array
arr = np.asarray(lst)