0% found this document useful (0 votes)
15 views7 pages

2 1

The document contains a series of Python code snippets demonstrating various operations using NumPy, including matrix multiplication, random number generation, array manipulation, and statistical calculations. It also includes examples of creating arrays, reshaping them, and performing arithmetic operations. Additionally, there are visualizations using Matplotlib to display histograms of uniform and normal distributions.

Uploaded by

tejasrathod114
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)
15 views7 pages

2 1

The document contains a series of Python code snippets demonstrating various operations using NumPy, including matrix multiplication, random number generation, array manipulation, and statistical calculations. It also includes examples of creating arrays, reshaping them, and performing arithmetic operations. Additionally, there are visualizations using Matplotlib to display histograms of uniform and normal distributions.

Uploaded by

tejasrathod114
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/ 7

NAME: Tejas Rathod

MOODLE ID: 21102061


YEAR/DIV/ROLL:BE/B/123
SUBJECT:MACHINE LEARNING EXPERIMENT NO:2

def multiply_loops(A,B):
C=np.zeros((A.shape[0],B.shape[1])) for i
in range(A.shape[1]):
for j in range(B.shape[0]):
C[i,j]=A[i,j]*B[j,i]
return C

import numpy as np #sample


Matrices
A=np.array([[1,2],[3,4]])
B=np.array([[5,6],[7,8]])
C=multiply_loops(A,B) print(C)

[[ 5. 14.]

[18. 32.]]

def multiply_vector(A,B):
return A@B

import numpy as np X=np.random.random((2,2))


Y=np.random.random((1000,1000)) print(X)

[[0.9512867 0.76109669]
[0.53666836 0.89837683]]

%timeit multiply_loops(A,B)
4.44 µs ± 112 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

print(np.random.randint(5)) print(np.random.rand(5))
print(np.random.randn(5))

4
[0.37631939 0.23752442 0.89203489 0.85175693 0.91339398]
[ 1.53085868 -0.25168637 1.71983828 -0.40483133 -0.94126992]

import numpy as np import matplotlib.pyplot as plt


import matplotlib.pyplot plt sample_size = 100000
uniform=np.random.rand(sample_size)
normal=np.random.randn(sample_size)
pdf,bins,patches=plt.hist(uniform,bins=20,ran
ge=(0,1),density=True)
plt.title('rand:uniform') plt.show()
pdf,bins,patches=plt.hist(normal,bins=20,rang e=(-
4,4),density=True) plt.title('rand:normal')

import numpy as np arr=np.array([42])


print(arr)
[42]

import numpy as np list_array1=np.array([1,2,3]) print(list_array1)

sample_list=[1,2,3] list_array2=np.array(sample_list)
print(list_array2)
[1 2 3]
[1 2 3]

import numpy as np list_array1=np.array([1,2,3]) print(list_array1)


list_array1.tolist()

[1 2 3]
[1, 2, 3]

import numpy as np tup=(1,2,3)


my_tup_array=np.array(tup)
print(my_tup_array)
print(type(my_tup_array))
[1 2 3]
<class 'numpy.ndarray'>

my_tup_array=np.array([1,2,3,4,5,6,7,8,9,10])
my_array=np.insert(my_array,3,14) print("after insertion",my_array)
my_array=np.append(my_array,11) print("after
appending",my_array) my_array=np.delete(my_array,3) print("after
deletion",my_array,3) my_array1=np.array([12,13,14])
my_array=np.concatenate((my_array,my_array1)) print("after
concatenation",my_array)

NameError Traceback (
call last)
<ipython-input-1-5854d342d6ee> in <cell line: 1>()
----> 1 my_tup_array=np.array([1,2,3,4,5,6,7,8,9,10])
2 my_array=np.insert(my_array,3,14)
3 print("after insertion",my_array) 4 my_array=np.append(my_array,11) 5
print("after appending",my_array)

NameError: name 'np' is not defined

my_range_array=np.arange(10,20,5) print(my_range_array) [10 15]

my_spaced_array=np.linspace(0,5/3,6) print(my_spaced_array)
print(my_spaced_array.max()) print(my_spaced_array.min())
print(my_spaced_array.argmax()) print(my_spaced_array.argmin())
print(my_spaced_array.size)
[0. 0.33333333 0.66666667 1. 1.33333333 1.66666667]
1.6666666666666667
0.0 5
0
6

my_array=np.zeros(10) print(my_array)
my_array=np.ones(10) print(my_array)
my_array=np.ones(10)*5 print(my_array)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[5. 5. 5. 5. 5. 5. 5. 5. 5. 5.]

np.full((3,4),0.11) array([[0.11, 0.11, 0.11, 0.11],


[0.11, 0.11, 0.11, 0.11],
[0.11, 0.11, 0.11, 0.11]])

arr=np.array([1,2,3,4]) print(arr[0]) 1

arr=np.array([1,2,3,4]) print(arr[2]+arr[3]) 7
my_orig_arr2=np.array([2,9,17,13,1,4,20,57])
my_orig_arr2[2:5]=-6 print(my_orig_arr2)
[ 2 9 -6 -6 -6 4 20 57]

my_orig_arr=np.array([1,15,3,9,26,7,89,12])
my_rev_arr=my_orig_arr[ : :-1] print(my_rev_arr)

[12 89 7 26 9 3 15 1]

my_rand_array=np.random.rand(3,4) print(my_rand_array)

tup_dim=(3,4) my_uninitialised_array=np.empty(tup_dim)
print(my_rand_array)

[[0.25021569 0.79676416 0.7036151 0.99879454]


[0.87769902 0.91607758 0.70436741 0.52686475]
[0.64397223 0.40026129 0.39138076 0.57672351]]
[[0.25021569 0.79676416 0.7036151 0.99879454]
[0.87769902 0.91607758 0.70436741 0.52686475]
[0.64397223 0.40026129 0.39138076 0.57672351]]
my_uninitialized_array = np.empty([1,2,3],[4,5,6],[7,8,9]) print(my_uninitialized_array)
TypeError Traceback (
last)
<ipython-input-25-65f7d55f40de> in <cell line: 1>()
----> 1 my_uninitialized_array = np.empty([1,2,3],[4,5,6],[7,8,9]) 2 print(my_uninitialized_arr

TypeError: Field elements must be 2- or 3-tuples, got '4'

import numpy as np my_first_arr=np.array([1,2,3,4,5,6,7,8])


my_new_arr=my_first_arr.reshape(2,4) print(my_new_arr)
print(my_first_arr)

my_second_arr=np.array([0,1,2,3,4,5,6,7,8])
my_updated_arr=my_second_arr.reshape(-1,3)
print(my_updated_arr) print(my_second_arr)
[[1 2 3 4]
[5 6 7 8]]
[1 2 3 4 5 6 7 8]
[[0 1 2]
[3 4 5]
[6 7 8]]
[0 1 2 3 4 5 6 7 8]

a_arr = np.array([60,70,80,90]) b_arr =


np.arange(4) c_arr = a_arr + b_arr print(c_arr)
d_arr=np.array([60,70,80,90]) e_arr=np.arange(4)
f_arr=d_arr - e_arr print(f_arr)
[60 71 82 93]
[60 69 78 87]

A_arr = np.array([[5,9],[4,7]])
B_arr = np.array([[2,8],[1,6]]) M_arr = A_arr *
B_arr print(M_arr)
C_arr = np.array([[5,9],[4,7]])
D_arr = np.array([[2,8],[1,6]]) P_arr =
np.dot(C_arr,D_arr) print(P_arr)
[[10 72]
[ 4 42]] [[19 94]
[15 74]]

R_div = np.array([[25,65],[40,14]])
S_div = np.array([[2,10],[4,5]]) T_div = R_div / S_div
print(T_div)

R_int_div = np.array([[25,65],[40,70]])
S_int_div = np.array([[2,10],[8,3]]) T_int_div = R_int_div //
S_int_div print(T_int_div)

R_mod = np.array([[20,65],[40,70]])
S_mod = np.array([[2,10],[8,3]]) T_mod = R_mod
% S_mod print(T_mod)
[[12.5 6.5]
[10. 2.8]]
[[12 6]
[ 5 23]]
[[0 5]
[0 1]]
R_exp = np.array([[4,2],[3,4]])
S_exp = np.array([[2,10],[4,5]]) T_exp = R_exp ** S_exp
print(T_exp)
[[ 16 1024]
[ 81 1024]]

X_stat = np.array([[1,2,3],[4,-5,6]p])rint("mean =
",X_stat.mean()) print("Variance = ",X_stat.var()) print("Standard
Deviation = ",X_stat.std()) print("min =",X_stat.min()) print("max
=",X_stat.maxp()r)int("sum=",X_stat.sump()r)int("produc=t
",X_stat.prod())X = np.array([9,16,27p])rint(np.sqrt(X))
print(np.log(X)m) ean = 1.83333333333333V3a3riance=
11.805555555555557 Standard Deviation = 3.435921354681384
min = -5 max = 6 sum = 11 product = -720
[3. 4. 5.19615242]
[2.19722458 2.77258872 3.29583687]

Z = np.arange(18).reshape(2,3,3) print(Z)
print(Z.sum(axis=2))
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]

[[ 9 10 11]
[12 13 14]
[15 16 17]]]
[[ 3 12 21]
[30 39 48]]

N = np.arange(6).reshape(3,2) print(N)
print("Transpose = ",N.T)
[[0 1]
[2 3]
[4 5]]
Transpose = [[0 2 4]
[1 3 5]]

X_broad = np.ones((3,3)) print(X_broad) print(X_broad) Y_broad


= np.arange(3) print(Y_broad) Z_broad = X_broad + Y_broad
print(Z_broad)
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[0 1 2]
[[1. 2. 3.]
[1 2. 3.]
[1. 2. 3.]]

arr = np.array([1,2,3,4,5]) x = arr.copy() arr[0] =


42 print(arr) print(x)
[42 2 3 4 5] [1 2 3 4 5]

arr = np.array([1,2,3,4,5]) x = arr.view() arr[0] =


42 print(arr) print(x)
[42 2 3 4 5]
[42 2 3 4 5]

Start coding or generate with AI.

You might also like