0% found this document useful (0 votes)
20 views

Numpy Programs

Uploaded by

Abhinav Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Numpy Programs

Uploaded by

Abhinav Suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Write a NumPy program to create a 3x4 matrix filled with


values from 10 to 21?

This problem involves writing a NumPy program to generate a 3x4 matrix filled
with consecutive values starting from 10 and ending at 21. The task requires
utilizing NumPy's array creation functionalities to efficiently construct the matrix
with the specified range of values while maintaining the desired dimensions.

import numpy as np
m = np.arange(10, 22).reshape((3, 4))
print(m)

o/p

[[10 11 12 13]
[14 15 16 17]
[18 19 20 21]]

Write a NumPy program to create a 3x3 identity matrix, i.e.


the diagonal elements are 1, the rest are 0?
This problem involves writing a NumPy program to generate a 3x3 identity
matrix, where all diagonal elements are 1 and the rest are 0. The task requires
leveraging NumPy's identity function, which efficiently constructs such matrices
of any desired size with the specified diagonal values. The resulting identity
matrix aids in various mathematical operations, such as linear transformations
and solving systems of equations.
import numpy as np
x = np.eye(3)
print(x)

o/p
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]

An identity matrix is a square matrix with ones on its main diagonal and zeros
elsewhere.

In the above code the line ‘x = np.eye(3)’ uses the np.eye() function to create a
2-dimensional square identity matrix 'x' of size 3x3.

Finally print(x) prints the generated identity matrix to the console.

Visual Presentation:
Write a NumPy program to create a 5x5 zero matrix with
elements on the main diagonal equal to 1, 2, 3, 4, 5?
This problem involves writing a NumPy program to generate a 5x5 matrix where
all elements are initially set to zero, except for the main diagonal, which is filled
with the values 1, 2, 3, 4, and 5. The task requires utilizing NumPy's array
manipulation capabilities to efficiently construct and modify the matrix, ensuring
the specified values are placed correctly on the diagonal. The resulting matrix
combines elements of identity matrices with custom diagonal values.

import numpy as np
x = np.diag([1, 2, 3, 4, 5])
print(x)

o/p
[[1 0 0 0 0]
[0 2 0 0 0]
[0 0 3 0 0]
[0 0 0 4 0]
[0 0 0 0 5]]

Explanation:

The above exercise creates a 5x5 square matrix with the main diagonal elements
[1, 2, 3, 4, 5] and zeros elsewhere, and prints the resulting matrix.

In the above code np.diag([1, 2, 3, 4, 5]) creates a 2D square matrix with the
specified diagonal elements [1, 2, 3, 4, 5]. The rest of the elements in the matrix
are filled with zeros.
Finally print(x) prints the generated matrix to the console.

Visual Presentation:

Write a NumPy program to compute the sum of all elements,


the sum of each column and the sum of each row in a given
array?
This problem involves writing a NumPy program to compute the sum of all
elements, as well as the sum of each column and each row in a given array. The
task requires leveraging NumPy's array manipulation functions to efficiently
calculate these sums. By utilizing NumPy's built-in functions such as np.sum
along appropriate axes, the program extracts the sums of rows, columns, and the
entire array, providing comprehensive insights into the array's data distribution.
import numpy as np

x = np.array([[0, 1], [2, 3]])

print("Original array:")
print(x)

print("Sum of all elements:")


print(np.sum(x))

print("Sum of each column:")


print(np.sum(x, axis=0))

print("Sum of each row:")


print(np.sum(x, axis=1))

o/p
Original array:
[[0 1]
[2 3]]
Sum of all elements:
6
Sum of each column:
[2 4]
Sum of each row:
[1 5]

Explanation:

In the above code -

‘x = np.array([[0, 1], [2, 3]])’ creates a 2D array 'x' with the shape (2, 2) and
elements [[0, 1], [2, 3]].

In the ‘print(np.sum(x))’ statement, np.sum() function is used to calculate the sum


of all elements in the array 'x' and prints the result. In this case, the sum is
0+1+2+3 = 6.

In the ‘print(np.sum(x, axis=0))’ statement, np.sum() function is used to calculate


the sum of the elements in the array 'x' along the first axis (axis 0, corresponding
to columns). The result is a 1D array with the sum of each column: [0+2, 1+3] =
[2, 4]. The output is then printed.

Finally in ‘print(np.sum(x, axis=1))’ statement np.sum() function calculates the


sum of the elements in the array 'x' along the second axis (axis 1, corresponding
to rows). The result is a 1D array with the sum of each row: [0+1, 2+3] = [1, 5].
The output is then printed.

Visual Presentation:

You might also like