Page 259 - AI Computer 10
P. 259
Arrays are mainly used for mathematical operations where lists are mainly used for data management.
NumPy can be imported into the Jupyter Notebook by using the given statement:
>>> import numpy # this will import the complete numpy
# package
OR
>>> import numpy as npy # this will import numpy and refer to it
# as npy
OR
>>> from numpy import array # this will import ONLY arrays module
# from whole numpy package
OR
>>> from numpy import array as ary # this will import ONLY
# arrays module and refer to it as ary
In NumPy, we can create n-dimensional arrays which are considered as an alternative to Python lists because
they allow faster access in reading and writing items effectively and efficiently.
Syntax for creating an array is:
>>> import numpy
marks = numpy.array ([34,23,41,42])
Syntax for creating a list is:
marks = [34, 23, 41, 42]
Creating an Array using NumPy
We can create different types of arrays using NumPy.
Example 1: Program to create one dimensional array.
import numpy
roll_no = numpy.array([1,2,3,4,5])
print(roll_no)
Output:
[1 2 3 4 5]
Example 2: Program to create 1D array with 4 random values.
import numpy as np
a=np.random.random(4)
print(a)
Output:
[0.49374929 0.86811999 0.48420151 0.44405495]
Example 3: Program to create 3 × 4 array with random integer values less than 10.
import numpy as np
a=np.random.randint(10, size = (3,4))
print(a)
Output:
[[4 8 3 6]
[7 9 0 8]
[5 1 2 2]]
125
125