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

Assignment On Numpy (2) : #Question 1

Uploaded by

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

Assignment On Numpy (2) : #Question 1

Uploaded by

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

ASSIGNMENT ON NUMPY(2)

[ ]: NAME=RAHUL RAMESH
RATHOD MIS=112110115
BATCH=F
BRANCE=MECHANICAL

[1]: #Question 1. Write a NumPy program to create 3 arrays of 5 zeros,10␣


ones, 10 elements between 0 to 50 having equal intervals in two consecutive␣

↪elements respectively.

import numpy as np
array_zeros=np.zeros(5)
array_ones=np.ones(10)
array=np.linspace(0,50,10)
print("The array of zeros:")
print(array_zeros)
print("The array of ones:")
print(array_ones)
print("An array of 10 Elements between 0 to 50:")
print(array)
#s=np.arange(0,50,50/9)
#print(s)

The array of zeros:


[0. 0. 0. 0. 0.]
The array of ones:
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
An array of 10 Elements between 0 to 50:
[ 0. 5.55555556 11.11111111 16.66666667 22.22222222 27.77777778
33.33333333 38.88888889 44.44444444 50. ]

[2]: #Question 2. Write a NumPy program to create an array with


values␣ ↪ranging from 4 to 48.
import numpy as np
range_array= np.arange(4, 49)
print("An array with values ranging between 4 to 48")
print(range_array)

An array with values ranging between 4 to 48


[4 5 6 7 8 9101112131415161718192021222324252627

1
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48]

[14]: #Question 3. Write a NumPy program to generate six random


integers␣ ↪between 10 and 30.
import numpy as np
random_integers = np.random.randint(10, 30, 6)
print("Generate six random integer between 10 to 30")
print(random_integers)

Generate six random integer between 10


to 30 [10 17 28 17 16 27]

[15]: #Question 4. Write a NumPy program to create an array having heights


and␣ ↪grades of 15 students of 10th class(Random Numbers). Find out the
maximum␣ ↪and minimum grade of class. Also arrange them in order of their
heights.
import numpy as np
heights = np.random.randint(100, 200, 20)
grades = np.random.randint(1, 11, 20)
student_data = np.column_stack((heights, grades))
max_grade = np.max(grades)
min_grade = np.min(grades)
sorted_indices = np.argsort(heights)
sorted_student_data =
student_data[sorted_indices] print("Height
(cm) Grade") print("------------ ------")
for student in sorted_student_data:
print(f"{student[0]:6} {student[1]}")

print(f"\nMaximum Grade: {max_grade}")


print(f"Minimum Grade: {min_grade}")

Height (cm) Grade


------------ ------
141 6
149 9
150 6
153 5
153 10
154 8
155 7
156 3
159 10
161 9
163 9
164 5
165 9
167 3
179 6

2
Maximum Grade: 10
Minimum Grade: 3

[20]: #Question 5. Write a NumPy program to create a 4x 4 array with


random␣ ↪values and find the minimum and maximum value.
import numpy as np
random_array = np.random.rand(4, 4)
min_value = np.min(random_array)
max_value = np.max(random_array)
print("Random Array:")
print(random_array)
print(f"\nMinimum Value: {min_value}")
print(f"Maximum Value: {max_value}")

Random Array:
[[0.6725206 0.97172572 0.54975316 0.75395661]
[0.29987882 0.06957545 0.91819967 0.55450947]
[0.8606592 0.26261473 0.27814343 0.63766782]
[0.94737535 0.06362616 0.34960625 0.70174297]]

Minimum Value: 0.0636261592862839


Maximum Value: 0.9717257159397605

[21]: #Question 6. Write a NumPy program to get the powers of an array


values␣ ↪element-wise (elements of Array 1** elements of Array2, index
wise).
import numpy as np
array1 = np.array([2, 3, 4, 5])
array2 = np.array([3, 2, 1, 0])
result = np.power(array1, array2)
print("Array 1:", array1)
print("Array 2:", array2)
print("Result (Array 1 ** Array 2):", result)

Array 1: [2 3 4 5]
Array 2: [3 2 1 0]
Result (Array 1 ** Array 2): [8 9 4 1]

[22]: #Question 7. Generate an array which actual and obtained values for
10␣ ↪performance parameters of machines. The machine is ideal if the
actual␣ ↪values are multiple of obtained values. Check if the machine is
ideal.
import numpy as np
actual_values = np.random.randint(1, 20, 10)
obtained_values = np.random.randint(1, 20, 10)
ideal_machine = np.all(actual_values % obtained_values == 0)
print("Actual Values:", actual_values)
print("Obtained Values:", obtained_values)
if ideal_machine:
print("\nThe machine is ideal.")
else:
print("\nThe machine is not ideal.")

Actual Values: [ 2 6 5 13 10 14 4 9 8 14]


Obtained Values: [ 3 18 11 3 4 16 11 14 7 3]

The machine is not ideal.

[23]: #Question 8. Generate (5,5) 2-dimensional array and write a code to


swap␣ ↪first two rows of array
import numpy as np
array_2d = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]])
print("Original Array:")
print(array_2d)
array_2d[[0, 1]] = array_2d[[1, 0]]
print("\nArray after Swapping First Two Rows:")
print(array_2d)

Original Array:
[[1 2 3 4 5]
[ 678910]
[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]]

Array after Swapping First Two Rows:


[[6 7 8 910]
[ 12345]
[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]]

[1]: #Question 9 9. Generate 2-dimensional array of shape (10,10), print


the␣ ↪array and write a code to find the memory size of any array
import numpy as np
vector = np.linspace(0, 1, 12)[1:-1]
print('Create a vector of size 10 with values ranging from 0 to 1
(excluding 0␣ ↪and 1) is :')
print(vector)
Create a vector of size 10 with values ranging from 0 to 1 (excluding 0 and 1)
is :
[0.09090909 0.18181818 0.27272727 0.36363636 0.45454545 0.54545455
0.63636364 0.72727273 0.81818182 0.90909091]

4
[1]: #Question 10. Create a vector of size 10 with values ranging from 0 to␣
import numpy as np
1, with both end values 0 and 1 excluded. Hint: Linspace

A = np.random.rand(5, 5)
B = np.random.rand(5, 5)
result = np.dot(A, B)
diagonal_elements = np.diagonal(result)

print("Matrix A:")
print(A)
print("\nMatrix B:")
print(B)
print("\nDot Product of A and B:")
print(result)
print("\nDiagonal Elements of the Dot Product:")
print(diagonal_elements)

Matrix A:
[[0.48685434 0.45925953 0.6169875 0.73368454 0.06589155]
[0.4886029 0.91513282 0.85327392 0.49421867 0.9937429 ]
[0.46338284 0.60996615 0.14995036 0.33224589 0.42612583]
[0.06254882 0.0685735 0.97291803 0.90810684 0.34065169]
[0.18868624 0.95289157 0.15167439 0.14840683 0.86418335]]

Matrix B:
[[0.44984817 0.0970476 0.62450248 0.47225828 0.66031112]
[0.36171573 0.73416862 0.23434909 0.03672095 0.26904383]
[0.94981339 0.31521542 0.68436479 0.51810546 0.28239686]
[0.95504321 0.90357278 0.81522721 0.37653887 0.38126006]
[0.67858842 0.20425422 0.00321858 0.2679855 0.77713093]]

Dot Product of A and B:


[[1.7165686 1.25530196 1.43224499 0.86036876 0.95020258]
[2.50760866 1.63778336 1.50964384 1.15883843 1.77049682]
[1.17798436 0.92730121 0.8071769 0.55822406 0.97025763]
[2.07547572 1.25321366 1.46237269 0.96935867 0.94545579]
[1.30177815 1.07631422 0.56871164 0.49015261 1.15195879]]

Diagonal Elements of the Dot Product:


[1.7165686 1.63778336 0.8071769 0.96935867 1.15195879]

You might also like