Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Aks Python Numpy

The document demonstrates various numpy operations on randomly generated arrays including addition, subtraction, multiplication, and division of element-wise operations as well as dot products. It also shows indexing, slicing, calculating statistics of random arrays.

Uploaded by

akshaysasi12.knr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Aks Python Numpy

The document demonstrates various numpy operations on randomly generated arrays including addition, subtraction, multiplication, and division of element-wise operations as well as dot products. It also shows indexing, slicing, calculating statistics of random arrays.

Uploaded by

akshaysasi12.knr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

import numpy as np

A = np.random.rand(3, 3)

B = np.random.rand(3, 3)

print("Matrix A:")

print(A)

print("\nMatrix B:")

print(B)

addition_result = A + B

print("\nElement-wise Addition:")

print(addition_result)

subtraction_result = A - B

print("\nElement-wise Subtraction:")

print(subtraction_result)

multiplication_result = A * B

print("\nElement-wise Multiplication:")

print(multiplication_result)

B[B == 0] = 1

division_result = A / B

print("\nElement-wise Division:")

print(division_result)

dot_product_result = np.dot(A, B.T)

print("\nDot Product of A and transpose of B:")

print(dot_product_result)

output:
Matrix A:
[[0.52320462 0.31600583 0.12417074]
[0.18081323 0.89542254 0.78584336]
[0.93104329 0.67799888 0.85155123]]

Matrix B:
[[0.2005074 0.52847325 0.10169274]
[0.91999346 0.27732516 0.39067615]
[0.9316541 0.75018569 0.43413305]]
Element-wise Addition:
[[0.72371201 0.84447908 0.22586348]
[1.10080669 1.17274771 1.1765195 ]
[1.86269739 1.42818457 1.28568428]]

Element-wise Subtraction:
[[ 3.22697221e-01 -2.12467416e-01 2.24779961e-02]
[-7.39180237e-01 6.18097383e-01 3.95167213e-01]
[-6.10812421e-04 -7.21868084e-02 4.17418189e-01]]

Element-wise Multiplication:
[[0.1049064 0.16700063 0.01262726]
[0.16634699 0.2483232 0.30701025]
[0.8674103 0.50862506 0.36968653]]

Element-wise Division:
[[2.60940308 0.59795994 1.22103836]
[0.19653751 3.2287822 2.01149563]
[0.99934438 0.90377475 1.96149831]]

Dot Product of A and transpose of B:


[[0.28453429 0.61749174 0.7784154 ]
[0.58937582 0.72168044 1.18134913]
[0.63158192 1.37726064 1.74572189]]

2. import numpy as np

array = np.arange(21)

print("Original Array:")

print(array)

even_numbers = array[::2]

print("\nEven Numbers:")

print(even_numbers)

array[array % 2 != 0] = -1

print("\nArray with Odd Numbers Replaced by -1:")

print(array)

output:
Original Array:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]

Even Numbers:
[ 0 2 4 6 8 10 12 14 16 18 20]

Array with Odd Numbers Replaced by -1:


[ 0 -1 2 -1 4 -1 6 -1 8 -1 10 -1 12 -1 14 -1 16 -1 18 -1 20]
3. import numpy as np

random_array = np.random.rand(100)

mean_value = np.mean(random_array)

median_value = np.median(random_array)

std_deviation = np.std(random_array)

print("Random Array:")

print(random_array)

print("\nMean:", mean_value)

print("Median:", median_value)

print("Standard Deviation:", std_deviation)

max_index = np.argmax(random_array)

min_index = np.argmin(random_array)

print("\nIndex of Maximum Value:", max_index)

print("Index of Minimum Value:", min_index)

output:
Random Array:
[0.07158925 0.22954581 0.93019346 0.76782331 0.30878765 0.71842636
0.42727568 0.34834966 0.61639596 0.05193189 0.28177112 0.52418425
0.05794718 0.30568083 0.45527442 0.57102646 0.10475938 0.54738644
0.26974918 0.16633987 0.61404562 0.04866694 0.29791016 0.30797741
0.41372213 0.3011158 0.90871694 0.73778884 0.91719647 0.40477112
0.96015033 0.7549503 0.58778421 0.96931821 0.97963489 0.40253644
0.82972447 0.92525553 0.56600489 0.10073911 0.39638846 0.08515834
0.98377073 0.80265811 0.11970499 0.98506532 0.71260925 0.1652933
0.40413475 0.11866743 0.840139 0.09418748 0.13427373 0.63345636
0.16019946 0.11975248 0.18700815 0.38800418 0.11362667 0.25563294
0.00539523 0.50275473 0.11490276 0.00748527 0.31764188 0.26907681
0.91598325 0.74850564 0.65229702 0.85939004 0.85571185 0.49435428
0.15550067 0.06517907 0.00371241 0.70871778 0.87369228 0.1474105
0.88522991 0.21198146 0.66444287 0.31572196 0.13500444 0.8189145
0.05835433 0.91530576 0.63421244 0.65046398 0.12793288 0.15460259
0.40615072 0.44582667 0.07173766 0.3610741 0.44309686 0.01888941
0.71167438 0.54332102 0.2228205 0.05109366]
Mean: 0.4402774269979007
Median: 0.4033355954170448
Standard Deviation: 0.3052392845722013

Index of Maximum Value: 45


Index of Minimum Value: 74

You might also like