Ex. No.
9 Name: MOHANAPRSATH G
Date : Roll No : 24MCA095
______________________________________________________________________________
USING PYTHON NUMPY
AIM:
To use Numpy package, to perform the following operations
Matrix 2-D array operations:
Create two matrices
Transpose the given matrix
Addition of two matrices
Subtraction of two matrices
Multiplication of two matrices
Generate identity matrices of size 7
Concatenate two 2-D arrays along row axis
Concatenate two 2-D arrays along column axis
Print shape and dimension of an 2D array
Print the elements of an array
([[7, “Java”, 33, 4, 55,66], [86, 87, 98, 39, 20,”Python”]])
o print(arr[1, 1:4])
o print(arr[0:2, 2])
o print(arr[0:2, 1:4])
1-D array operations:
Search an element in 1D array
Sort the elements of 1D array
Print the elements of an array ([“MCA”,10,20,40,”Lab”,60,90,”Mepco”,35,25])
o print(arr[2:5])
o print(arr[3:])
o print(arr[:3])
o print(arr[-4:-2])
o print(arr[2:7:2])
Random number generation
Generate 1-D array with 10 random numbers between 0 and 200
Generate a 2-D array with 4 rows, each row containing 3 random integers from 0
to 50
Generate a random normal distribution of size 4x5
Using Matplotlib and Numpy
Generate a graph using plot function for the given data
x=[Link]([1,2,3,4,5,6,7,8,9,10])
y=[Link]([10,20,30,40,50,60,70,80,90,100])
Generate sin wave and cos wave in different sub plots for the using
x=[Link](0,10,0.1)
Generate bar graph for the given data
x1 = [1,2,3]
x2=[4,5,6]
y1= [20,16,6]
y2 = [6,15,7]
Software used:
Operating System: Windows OS
Compiler: python 3.10.5
SOURCE CODE:
import numpy as np
import [Link]
from numpy import random
import [Link] as plt
print("\tMatrix 2-D array Operations")
arr1 = [Link]([[5, 7, 9], [6, 8, 10]])
arr2 = [Link]([[2, 5, 7], [2, 3, 7]])
print("Array 1:\n", arr1)
print("Array 2:\n", arr2)
t = [Link](arr1)
print("Transpose of Array 1:\n", t)
add = [Link](arr1, arr2)
print("Addition of two matrices:\n", add)
sub = [Link](arr1, arr2)
print("Subtraction of two matrices:\n", sub)
a1 = [Link]([[5, 7, 9], [6, 8, 10]])
a2 = [Link]([[1, 2], [3, 4], [5, 6]])
mul = [Link](a1, a2)
print("Multiplication of two matrices:\n", mul)
imatrix = [Link](7) # dtype=float
print("Identity matrix of Size 7:\n", imatrix)
joinrow = [Link]((arr1, arr2), axis=1)
print("Concatenate arrays along rows:\n", joinrow)
joincol = [Link]((arr1, arr2), axis=0)
print("Concatenate arrays along column:\n", joincol)
print("Shape of Array 1:\n", [Link])
print("Dimension of Array 2:\n", [Link])
a = [Link]([[7, "Java", 33, 4, 55, 66], [86, 87, 98, 39, 20, "Python"]])
print("Array:\n", a)
print("Array slicing:\n")
print("a[1,1:4]:\n", a[1, 1:4])
print("a[0:2,2]:\n", a[0:2, 2])
print("a[0:2,1:4]:\n", a[0:2, 1:4])
print("\tMatrix 1-D array Operations")
ar = [Link]([75, 10, 20, 80, 60, 90, 35, 25, 20])
print("Array:\n", ar)
x = [Link](ar == 20)
print("The element 20 found at index\n", x)
print("Sort an array:\n", [Link](ar))
ar1 = [Link](["MCA", 10, 20, 40, "Lab", 60, 90, "Mepco", 35, 25])
print("Array for slicing:\n", ar1)
print("\tArray slicing")
print("ar1[2:5]:", ar1[2:5])
print("ar1[3:]:", ar1[3:])
print("ar1[:3]:", ar1[:3])
print("ar1[-4:-2]:", ar1[-4:-2])
print("ar1[2:7:2]:", ar1[2:7:2])
print("\tRandom number generation")
x = [Link](200, size=10)
print("10 Random number generation between 0 and 200\n", x)
y = [Link](50, size=(4, 3))
print("Random numbers between 0 and 50 with 4 rows & 3 random numbers\
n", y)
z = [Link](size=(4, 5))
print("Random normal distribution of size 4x5\n", z)
x = [Link]([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = [Link]([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
[Link]("GRAPH-PLOT")
[Link]("x-axis")
[Link]("y-axis")
[Link](x, y)
[Link]()
[Link](2, 1, 1)
X = [Link](0, 10, 0.1)
Y = [Link](X)
[Link]("Sine Wave")
[Link](X, Y)
[Link](2, 1, 2)
Y = [Link](X)
[Link]("Cosine Wave")
[Link](X, Y)
[Link]()
x1 = [1, 2, 3]
x2 = [4, 5, 6]
y1 = [20, 16, 6]
y2 = [6, 15, 7]
[Link](x1, y1)
[Link](x2, y2)
[Link]("BAR-GRAPH")
[Link]()
OUTPUT:
Result:
This python code use Numpy package to implement the Matrix 2-D array operations, 1-
D array operations, Random number generation, Using Matplotlib and Numpy are run
successfully.