Professional Documents
Culture Documents
Introductory To Numpy Jan 2023
Introductory To Numpy Jan 2023
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[1 2 3]
# create an array
a = np.array([1, 2, 3, 4, 5])
# indexing
print(a[2]) # prints 3
# slicing
print(a[1:3]) # prints [2, 3]
# reshaping
#print(a.reshape((2, 2))) # prints [[1, 2], [3, 4], [5, 6]]
# concatenation
b = np.array([6, 7, 8])
c = np.concatenate((a, b))
print(c) # prints [1, 2, 3, 4, 5, 6, 7, 8]
3
[2 3]
[1 2 3 4 5 6 7 8]
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b
print(c) # prints [5, 7, 9]
Subtraction:
In [ ]: import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a - b
print(c) # prints [-3, -3, -3]
Multiplication:
In [ ]: import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a * b
print(c) # prints [4, 10, 18]
Division:
In [ ]: import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a / b
print(c) # prints [0.25, 0.4, 0.5]
Matrix Multiplication:
In [49]: import numpy as np
[[19 22]
[43 50]]
Matrix Inversion:
In [ ]: import numpy as np
a = np.array([1, 2, 3, 4, 5])
mean = np.mean(a)
print(mean) # prints 3.0
Median:
In [ ]: import numpy as np
a = np.array([1, 2, 3, 4, 5])
median = np.median(a)
print(median) # prints 3.0
Standard deviation:
In [ ]: import numpy as np
a = np.array([1, 2, 3, 4, 5])
std = np.std(a)
print(std) # prints 1.5811388300841898
Correlation:
In [ ]: import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.array([5, 4, 3, 2, 1])
correlation = np.corrcoef(a, b)
print(correlation) # prints [[ 1., -1.], [-1., 1.]]
a = np.array([1, 2, 3, 4, 5])
percentile = np.percentile(a, 50)
print(percentile) # prints 3.0
# Generate an array of 10 random numbers from a normal distribution with mean 0 and st
random_numbers = np.random.randn(10)
print(random_numbers)
In [ ]: