DATE:
EXPRIMENT-6
NUMPY MODULE FOR MATRIX MODELS
AIM: To develop python program for matrix models by using numpy module.
SOURCE CODE:
import numpy as np
# Step 1: Create a 2D array (matrix) from a list
matrix_from_list = np.loadtxt('matrix_data.csv', delimiter=',')
print("Matrix from List:")
print(matrix_from_list)
# Step 2: Create a zero matrix (3x3)
zero_matrix = np.zeros((3, 3))
print("\nZero Matrix (3x3):")
print(zero_matrix)
# Step 3: Create a ones matrix (2x4)
ones_matrix = np.ones((2, 4))
print("\nOnes Matrix (2x4):")
print(ones_matrix)
# Step 4: Create an identity matrix (4x4)
identity_matrix = np.eye(4)
print("\nIdentity Matrix (4x4):")
print(identity_matrix)
# Step 5: Create a random matrix (3x3)
random_matrix = np.random.rand(3, 3)
print("\nRandom Matrix (3x3):")
print(random_matrix)
DATASET DESCRIPTION:
The matrix data represents a simple 3x3 grid of numerical values saved in a CSV file format. Each row in
the matrix corresponds to a set of values separated by commas, allowing for easy loading and
manipulation using Python’s NumPy library.
Rows: Each row represents a separate list of values.
Columns: Each column represents an individual element within the rows.
Shape: This matrix is a 3x3 square matrix with three rows and three columns.
This matrix can be used in various computational applications, such as linear algebra operations,
transformations, or for educational purposes in understanding matrix data structures.
Data give below:
1 2 3
4 5 6
7 8 9
. Save data set by ‘matrix_data.csv’.
OUTPUT:
Matrix from List:
[[1 2 3]
[4 5 6]
[7 8 9]]
Zero Matrix (3x3):
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Ones Matrix (2x4):
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
Identity Matrix (4x4):
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
Random Matrix (3x3):
[[0.83489341 0.81429439 0.89072867]
[0.19209183 0.14419781 0.68256356]
[0.12222279 0.09337582 0.27508651]]
RESULT:
Numpy module for matrix models by using number data set is successfully executed.