Professional Documents
Culture Documents
Date of Performance :
Date of Submission :
THEORY:
The NumPy library is a popular Python library used for scientific computing applications, and is an
acronym for "Numerical Python". NumPy's operations are divided into three main categories: Fourier
Transform and Shape Manipulation, Mathematical and Logical Operations, and Linear Algebra and
Random Number Generation. To make it as fast as possible, NumPy is written in C and Python.
import numpy as np
nums = np.array([2, 3, 4, 5, 6])
nums2 = nums + 2
To install the NumPy package, you can use the pip installer. Execute the following command to install:
$ pip install numpy
Otherwise, if you are running Python via the Anaconda distribution, you can execute the following
command instead:
$ conda install numpy
NumPy arrays are the building blocks of most of the NumPy operations. The NumPy arrays can be
divided into two types: One-dimensional arrays and Two-Dimensional arrays.
In the script above we first imported the NumPy library as np, and created a list x.
To create a two-dimensional array, you can pass a list of lists to the array method as shown below:
nums = np.array([[2,4,6], [8,10,12], [14,16,18]])
The above script results in a matrix where every inner list in the outer list becomes a row. The number
of columns is equal to the number of elements in each inner list. The output matrix will look like this:
array([[ 2, 4, 6],
[ 8, 10, 12],
[14, 16, 18]])
nums = np.arange(2, 7)
array([2, 3, 4, 5, 6])
nums = np.arange(2, 7, 2)
array([2, 4, 6])
zeros = np.zeros(5)
The above script will return a one-dimensional array of 5 zeros. Print the zeros array and you should see
the following:
array([0., 0., 0., 0., 0.])
Similarly, to create a two-dimensional array, you can pass both the number of rows and columns to the
zeros method, as shown below:
zeros = np.zeros((5, 4))
The above script will return a two-dimensional array of 5 rows and 4 columns:
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
And again, for the two-dimensional array, try out the following code:
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
The linspace Method
This method takes three arguments: a start index, end index, and the number of
linearly-spaced numbersthat you want between the specified range. For
instance, if the first index is 1, the last index is 10 and you need 10 equally
spaced elements within this range, you can use the linspace method as follows:
lin = np.linspace(1, 10, 10)
array = np.ones(10)
print("An array of 10 ones:")
print(array)
array = np.ones(10)*5
print("An array of 10 fives:")
print(array)
Output :
10. Use Numpy to generate an array of 25 random numbers sampled from standard
normal distribution.
Source code:
import numpy as np
rand_num = np.random.normal(0,1,15)
print("15 random number from a standard normal distribution:")
print(rand_num)
Output:
11. Create a matrix 10x10 matrix values between 0 to 1
Source code:
import numpy as np
x = np.ones((10,10))
x[1:-1,1:-1] = 0
print(x)
Output:
13. Create an 2D array with 3 rows and 4 columns values starting from 12 to
25.
Source code:
array =[[20,19,18,17,],[16,15,14,13],[17,25,22,21]]
print(array)
Output:
14. Write code to extract value 20 from 2d array.
Source code:
array =[[20,19,18,17,],[16,15,14,13],[17,25,22,21]]
print(array[0][0])
Output: