0% found this document useful (0 votes)
8 views5 pages

Assign8 (T&T)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Assign8 (T&T)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2/22/24, 3:17 AM Assignment 8

In [3]: Q1
import numpy as np

print('Roll: 2105555')
null_vector = np.zeros(10)

null_vector[4] = 1

print(null_vector)

Roll: 2105555
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]

In [4]: Q2
import numpy as np

print('Roll: 2105555')
vector = np.arange(10, 50)

print(vector)

Roll: 2105555
[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]

In [5]: Q3
import numpy as np

print('Roll: 2105555')
vector = np.array([1, 2, 3, 4, 5])

reversed_vector = vector[::-1]

print(reversed_vector)

Roll: 2105555
[5 4 3 2 1]

In [6]: Q4
import numpy as np
print('Roll: 2105555')

array = np.arange(9)

matrix = array.reshape(3, 3)

print(matrix)

Roll: 2105555
[[0 1 2]
[3 4 5]
[6 7 8]]

In [7]: Q5
import numpy as np
print('Roll: 2105555')
arr = np.array([1, 2, 0, 0, 4, 0])

indices = np.nonzero(arr)

file:///C:/Users/Prashant/Downloads/Assignment 8.html 1/5


2/22/24, 3:17 AM Assignment 8

print(indices)

Roll: 2105555
(array([0, 1, 4], dtype=int64),)

In [8]: Q6
import numpy as np
print('Roll: 2105555')
array = np.random.random((3, 3, 3))

print(array)

Roll: 2105555
[[[0.71814551 0.71188911 0.79163963]
[0.75469638 0.49176884 0.44994773]
[0.00938393 0.14348557 0.10868323]]

[[0.00193207 0.07663488 0.5161109 ]


[0.5238665 0.81257342 0.95602986]
[0.93384719 0.32018341 0.0803387 ]]

[[0.51622059 0.07472859 0.98968601]


[0.05909411 0.21372675 0.70219993]
[0.18517651 0.35354267 0.69245299]]]

In [9]: Q7
import numpy as np

print('Roll: 2105555')

array = np.random.random((10, 10))

min_value = array.min()
max_value = array.max()

print("Minimum value:", min_value)


print("Maximum value:", max_value)

Roll: 2105555
Minimum value: 0.008943328439719456
Maximum value: 0.9901988327952138

In [10]: Q8
import numpy as np

print('Roll: 2105555')

vector = np.random.random(30)

mean_value = np.mean(vector)

print("Mean value:", mean_value)

Roll: 2105555
Mean value: 0.41729794687513977

In [11]: Q9
import numpy as np

print('Roll: 2105555')
shape = (5, 5)

file:///C:/Users/Prashant/Downloads/Assignment 8.html 2/5


2/22/24, 3:17 AM Assignment 8

array = np.zeros(shape)

array[0, :] = 1
array[-1, :] = 1
array[:, 0] = 1
array[:, -1] = 1

print(array)

Roll: 21053343
[[1. 1. 1. 1. 1.]
[1. 0. 0. 0. 1.]
[1. 0. 0. 0. 1.]
[1. 0. 0. 0. 1.]
[1. 1. 1. 1. 1.]]

In [12]: Q10
import numpy as np
print('Roll: 2105555')
matrix = np.random.random((5, 5))

mean_value = np.mean(matrix)
std_value = np.std(matrix)

normalized_matrix = (matrix - mean_value) / std_value

print("Original matrix:")
print(matrix)
print("\nNormalized matrix:")
print(normalized_matrix)

Roll: 2105555
Original matrix:
[[0.99285886 0.03793692 0.79306009 0.47948129 0.5934302 ]
[0.66628567 0.9189867 0.39359815 0.08021751 0.72961314]
[0.01212984 0.70606849 0.68252579 0.36650103 0.38124914]
[0.3122033 0.62353585 0.639048 0.4814023 0.99005208]
[0.43605918 0.37378858 0.14473083 0.90996421 0.85070316]]

Normalized matrix:
[[ 1.57184933 -1.77081037 0.87246307 -0.22520489 0.17366793]
[ 0.42869513 1.3132633 -0.52583482 -1.62280913 0.65036999]
[-1.86114685 0.56795303 0.48554291 -0.62068702 -0.56906194]
[-0.81075371 0.27905136 0.33335094 -0.2184805 1.56202433]
[-0.37720196 -0.59517729 -1.39698328 1.28168049 1.07423995]]

In [13]: Q11
import numpy as np

print('Roll: 2105555')

matrix1 = np.random.random((5, 3))

matrix2 = np.random.random((3, 2))

result = np.dot(matrix1, matrix2)

print("Matrix 1:")
print(matrix1)
print("\nMatrix 2:")

file:///C:/Users/Prashant/Downloads/Assignment 8.html 3/5


2/22/24, 3:17 AM Assignment 8

print(matrix2)
print("\nResult of matrix multiplication:")
print(result)

Roll: 2105555
Matrix 1:
[[0.74033213 0.79450822 0.68029739]
[0.46479668 0.82185347 0.10491305]
[0.75573388 0.87111661 0.18570238]
[0.17266047 0.38274747 0.88976154]
[0.03916313 0.96609062 0.18988594]]

Matrix 2:
[[0.94008408 0.23807398]
[0.2560473 0.33393311]
[0.06924621 0.25704318]]

Result of matrix multiplication:


[[0.94651414 0.61643222]
[0.65464615 0.41206726]
[0.94635963 0.51854878]
[0.32192942 0.39762515]
[0.29733041 0.38074225]]

In [14]: Q12
import numpy as np

print('Roll: 2105555')

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

arr[(arr > 3) & (arr < 8)] *= -1

print("Modified array:")
print(arr)

Roll: 2105555
Modified array:
[ 1 2 3 -4 -5 -6 -7 8 9 10]

In [26]: Q13
import numpy as np
print('Roll: 2105555')

matrix = np.array([[1, 2, 3],


[4, 5, 6],
[7, 8, 9]])

eigenvalues, eigenvectors = np.linalg.eig(matrix)

print("Eigenvalues:")
print(eigenvalues)
print("\nEigenvectors:")
print(eigenvectors)

file:///C:/Users/Prashant/Downloads/Assignment 8.html 4/5


2/22/24, 3:17 AM Assignment 8

Roll: 2105555
Eigenvalues:
[ 1.61168440e+01 -1.11684397e+00 -1.30367773e-15]

Eigenvectors:
[[-0.23197069 -0.78583024 0.40824829]
[-0.52532209 -0.08675134 -0.81649658]
[-0.8186735 0.61232756 0.40824829]]

In [25]: Q14
import numpy as np

# Define the 2x2 non-singular matrix


matrix = np.array([[2, 3],
[1, 4]])

# Calculate the inverse of the matrix


inverse_matrix = np.linalg.inv(matrix)

print("Original Matrix:")
print(matrix)
print("\nInverse Matrix:")
print(inverse_matrix)

Original Matrix:
[[2 3]
[1 4]]

Inverse Matrix:
[[ 0.8 -0.6]
[-0.2 0.4]]

In [ ]:

file:///C:/Users/Prashant/Downloads/Assignment 8.html 5/5

You might also like