NAME: MAKADIYA DEEP DINESHBHAI
ENROLLMENT NO: 230280152034
1. Import Numpy
import numpy as np
2. Numpy Array: Array with one dimention →
Vectors, Two dimentions →
Matrices, Three or more dimentions → Tensors
a. Create numpy array and print it. Use [Link]
arr=[Link]([1,2,3,4])
arr=[Link]([1,2,3,4])
print(arr)
[1 2 3 4]
b. Create a two-dimentional array and print it.
arr1=[Link]([[1,2],[3,4]])
print(arr1)
print(arr1)
[[1 2]
[3 4]]
c. Use the shape function to determine the shape of array.
[Link]
(2, 2)
d. Use reshape function to convert vector into matrix and
matrix into
vector.
[Link](2,2)
array([[1, 2],
[3, 4]])
[Link](-1)
array([1, 2, 3, 4])
3. Creating Arrays:
a. Create an array using numpy arange function.
arr2=[Link](0,8,2)
print(arr2)
[0 2 4 6]
b. Reshape it into a matrix form using reshape function.
[Link](2,2)
array([[0, 2],
[4, 6]])
c. Create arrays !lled with zeros and ones. Use zeros and
ones function.
arr4=[Link]((3,3))
print(arr4)
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
arr5=[Link]((3,3))
print(arr5)
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
d. Create two arrays with zeros and ones and combine
them using:
i. as columns next to each other using column_stack
np.column_stack((arr4,arr5))
array([[0., 0., 0., 1., 1., 1.],
[0., 0., 0., 1., 1., 1.],
[0., 0., 0., 1., 1., 1.]])
ii. as rows underneath each other using row_stack
np.row_stack((arr4,arr5))
/tmp/[Link]: DeprecationWarning: `row_stack` alia
np.row_stack((arr4,arr5))
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
4. Vectorized Functions:
• Numpy o"ers a plethora of vectorized functions
and operators,
called universal functions.
• Vectorized operations are easier to code, easier to
read, and result in
faster code.
a. Create a matrix of size 3X4 using arrange.
arr6=[Link](0,12).reshape(3,4)
print(arr6)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
b. Add 150 to each element.
sum=arr6+150
print(sum)
[[150 151 152 153]
[154 155 156 157]
[158 159 160 161]]
c. Rise each element to power of 2.
expo=arr6 ** 2
print(expo)
[[ 0 1 4 9]
[ 16 25 36 49]
[ 64 81 100 121]]
d. Create the array a ## ([[ 2, 4, 6, 8, 10],
[12, 14, 16, 18, 20],
[22, 24, 26, 28, 30],
[32, 34, 36, 38, 40]])
Hint: Use arrange function to create matrix of size 4X5 then
apply
addition and multiplication operators.
arr7=[Link](0,20).reshape(4,5)
print(arr7)
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
print(arr7+1)
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]]
print(arr7*2)
[[ 0 2 4 6 8]
[10 12 14 16 18]
[20 22 24 26 28]
[30 32 34 36 38]]
e. Use comparison operators (a>6, a==10)
gtr=arr7>6
print('the greater value are',arr7[gtr])
the greater value are [ 7 8 9 10 11 12 13 14 15 16 17 18 19]
eq = arr7==10
print(arr7[eq])
[10]
f. Use logical operators. ((a < 3) | (a > 8)), ((a > 4) & (a < 7)),
(~(a > 6))
print(arr7[(arr7<3) | (arr7>8)])
[ 0 1 2 9 10 11 12 13 14 15 16 17 18 19]
print(arr7[(arr7>4) & (arr7<7)])
[5 6]
print(arr7[(~(arr7>6))])
[0 1 2 3 4 5 6]
5. Array Indexing and Slicing - Position: Indexing
refer to extracting elements
based on their position or certain criteria. This is one
of the fundamental
operations with arrays.
a. Create an array with 12 elements.
arr8=[Link](0,12)
b. Make all elements from index 7 to 11 to -1.
for i in range(7,12):
arr8[i]=-1
print(arr8)
[ 0 1 2 3 4 5 6 -1 -1 -1 -1 -1]
c. Extract elements at index 2,3,6 positions in one go.
print(arr8[[2,3,6]])
[2 3 6]
print(arr8[2],arr8[3],arr8[6])
2 3 6
d. Create a matrix of size 3X4.
arr9=[Link](1,13).reshape(3,4)
print(arr9)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
e. Extract 2nd row
print(arr9[1,:])
[5 6 7 8]
f. Extract 3rd column
print(arr9[:,2])
[ 3 7 11]
g. Extract 1st, 2nd row
print(arr9[0:2,:])
[[1 2 3 4]
[5 6 7 8]]
h. Extract 2nd row, 3rd column
print(arr9[1:2,2:3])
[[7]]
i. Extract 1s, 2nd row, !rst three columns
print(arr9[0:2,0:3])
[[1 2 3]
[5 6 7]]
j. set the third row to 1,2,3,4
arr9[2]=[1,2,3,4]
print(arr9)
[[1 2 3 4]
[5 6 7 8]
[1 2 3 4]]
6. Array Indexing and Slicing – Criteria
a. Create two vectors:
i. names = [Link](["Rahul", "Simran", "Ritesh", "Simran",
"Rahul"])
names = [Link](["Rahul", "Simran", "Ritesh", "Simran","Rahul"])
ii. score = [Link]([126, 115, 130, 141, 132])
score = [Link]([126, 115, 130, 141, 132])
b. Extract all test scores that are smaller than 130
for numbers in score:
if(numbers<130):
print(numbers)
126
115
c. Extract all test scores by Simran
for i in range(5):
if(names[i]=="Simran"):
print(score[i])
115
141
d. Add 10 points to Rahul’s scores.
for i in range(5):
if(names[i]=="Rahul"):
print(score[i]+10)
136
142
7. Random Numbers:
a. Explore [Link]
import random
list1=["Rahul", "Simran", "Ritesh", "Simran","Rahul"]
choice=[Link](list1)
print(list1)
print(choice)
['Rahul', 'Simran', 'Ritesh', 'Simran', 'Rahul']
Simran
b. Explore [Link]
random=[Link]()
print(random)
-1.1208824465317349
rnadom1=[Link](loc=10, scale=2, size=5)
print(rnadom1)
[10.08217017 10.56963381 5.38987608 12.54578338 10.33420064]
c. Explore [Link]
x = [Link](n=10, p=0.5, size=10)
print(x)
[5 5 5 6 5 6 6 3 5 4]
d. Explore [Link]
print([Link](20, 60))
56.104144374427925
e. Explore and explain the use of [Link]
Random Seeds and Reproducibility. Se#ing Up Your ...A random seed
is a starting value used to initialize a pseudo-random number
generator. Se#ing a random seed allows for the generation of
predictable and reproducible sequences of random numbers, which
is useful for testing, debugging, and ensuring consistent results in
various applications, especially in machine learning and simulations
[Link](10)
print([Link]())
0.771320643266746
8. Statistical Functions: (sum, mean, median, etc...)
a. Create a matrix of size 3X4
arr10=[Link](1,13).reshape(3,4)
print(arr10)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
b. Find the sum of all elements
[Link]()
np.int64(78)
c. Add rows, preserve columns
arr11 = [Link]([[1, 2, 3], [4, 5, 6]])
arr12 = [Link]([[7, 8, 9]])
arr13 = [Link](arr11, arr12, axis=0)
print(arr13)
[[1 2 3]
[4 5 6]
[7 8 9]]
d. Add columns, preserve rows
Note: [Link] and [Link](a) are same.
arr14 = [Link]([[1, 2],
[3, 4],
[5, 6]])
colmn = [Link]([7, 8, 9]).reshape(-1, 1)
result = [Link]((arr14,colmn))
print(result)
[[1 2 7]
[3 4 8]
[5 6 9]]
Start coding or generate with AI.