Page 222 - Ai Book - 10
P. 222
Syntax of creating an array is:
>>> import numpy
marks = numpy.array ([34,23,41,42])
Syntax of 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]]
Example 4: Program to create 2D array of 4 rows and 5 columns with all ones as value.
import numpy as np
A = np.ones((4,5))
print(a)
Output:
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
Example 5: Program to create 2D array of 4 rows and 5 columns with all zeros as value.
import numpy as np
a = np.zeros((4,5))
print(a)
96
96