You are on page 1of 8

EXPERIMENT NO: 7

Date of Performance :

Date of Submission :

AIM: Exploring basics of NumPy Methods.

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.

There are several ways to create a NumPy array.


To create a one-dimensional NumPy array,
import numpy as np
x = [2, 3, 4, 5, 6]
nums = np.array([2, 3, 4, 5, 6])
type(nums)

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]])

The arange Method


Another commonly used method for creating a NumPy array is the arange method. This method takes
the start index of the array, the end index, and the step size (which is optional).

nums = np.arange(2, 7)
array([2, 3, 4, 5, 6])
nums = np.arange(2, 7, 2)
array([2, 4, 6])

The zeros Method


Apart from generating custom arrays with your pre-filled data, you can also create NumPy arrays with a
simpler set of data. For instance, you can use the zeros method to create an array of all zeros as shown
below:

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.]])

The ones Method


create one-dimensional and two-dimensional arrays of all ones using the ones method as follows:
ones = np.ones(5)
array([1., 1., 1., 1., 1.])

And again, for the two-dimensional array, try out the following code:

ones = np.ones((5, 4))


Now if you print the ones array on the screen, you should see the following two-dimensional array:

[[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)

The output will return integers from 1 to 10:


array([1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
Now let's try to create an array with 20 linearly-spaced elements
between 1 and 10. Execute thefollowing script:
lin = np.linspace(1, 10, 20)

This will result in the following array:


array([ 1. , 1.47368421, 1.94736842, 2.42105263, 2.89473684,
3.36842105, 3.84210526, 4.31578947, 4.78947368, 5.26315789,
5.73684211, 6.21052632, 6.68421053, 7.15789474, 7.63157895,
8.10526316, 8.57894737, 9.05263158, 9.52631579, 10. ])

The random Method


create arrays with random numbers. You can use the rand function of NumPy's
random module to do so.Here is a simple example of the rand function:
random = np.random.rand(2, 3)
The above script returns a matrix of 2 rows and 3 columns. The matrix
contains uniform distribution ofnumbers between 0 and 1:
array([[0.26818562, 0.65506793, 0.50035001],
[0.527117 , 0.445688 , 0.99661 ]])
Similarly, to create a matrix of random numbers with the Gaussian
distribution (or "normal"distribution), you can instead use the
randn method as shown below:
random = np.random.randn(2, 3)
The randint method takes the lower bound, upper bound, and the number
of integers to return. For instance, if you want to create an array of 5
random integers between 50 and 100, you can use thismethod as follows:

random = np.random.randint(50, 100, 5)


array([54, 59, 84, 62, 74])
Program and Output:
1. Import Numpy
2. Create an array of 10 zeroes
3. Create an array of 10 ones
4. Create an array of 10 fives
Source code:
import numpy as np
array = np.zeros(10)
print("An array of 10 zeros:")
print(array)

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 :

5. Create an array of the integers from 10 to 50


Source code:
import numpy as np
array = np.arange(10,50)
print("Array of all the even integers from 10 to 50")
print(array)
Output:

6. Create an array of all the even integers from 10 to 50


Source code:
import numpy as np
array = np.arange(10,50,2)
print("Array of all the even integers from 10 to 50")
print(array)
Ouput:
7. Create a 3x3 matrix with values ranging from 0 to 8
Source code:
import numpy as np
x = np.arange(0,9).reshape(3,3)
print(x)
Ouput:

8. Create a 3x3 identity matrix.


Source code:
import numpy as np
array_2D = np.identity(3)
print("3x3 matrix:")
print(array_2D)
Output:

9. Use Numpy to generate a random number between 0 to 1.


Source code:
import numpy as np
rand_num = np.random.normal(0,1,1)
print("Random number between 0 and 1:")
print(rand_num)
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:

12. Create an array of 20 linearly spaced points between 0 and 1.


Source code:
import numpy as np
num_line = np.linspace(0,1,20)
print(num_line)
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:

15.Write code to extract only first two rows.


Source code:
array =[[20,19,18,17,],[16,15,14,13],[17,25,22,21]]
print(array[0])
print(array[1])
Output:

16. Write code to extract first column.


Source code:
array =[[20,19,18,17,],[16,15,14,13],[17,25,22,21]]
print(array[0][0])
print(array[1][0])
print(array[2][0])
Output:

17. Write code to Extract second column and third column.


Source code:
array =[[20,19,18,17,],[16,15,14,13],[17,25,22,21]]
print(array[0][1])
print(array[1][1])
print(array[2][1])
print(array[0][2])
print(array[1][2])
print(array[2][2])
Output:
18.Write code to perform basic numpy operation on arr(1,25)
Source code:
import numpy as np
array=np.arange(1,25)
print(array)
print(array[3])
Output:

CONCLUSION: Program executed successfully

R1 R2 R3 R4 Total (10 Signature


(3 Marks) (3 Marks) (3 Marks) (1 Mark) Marks)

You might also like