In [ ]:
import numpy as np
x = np.random.random()
print("Random number: {}".format(x))
In [ ]:
print("Square root of {} is {}".format(x, np.sqrt(x)))
In [ ]:
print("x^sqrt(x) = {}".format(np.power(x, np.sqrt(x))))
In [ ]:
matrix = np.random.random(3)
print("Random matrix {}".format(matrix))
In [ ]:
matrix = np.append(matrix, np.random.random(2))
print("New Matrix {}".format(matrix))
In [ ]:
print("Sine of matrix -> {}".format(np.sin(matrix)))
In [ ]:
# Tour of Numpy
mvalues = [0,1,5,6,80]
M = np.array(mvalues)
print("M = {}".format(M))
In [ ]:
M
In [ ]:
mvalues
In [ ]:
print(M * 39.3701) # Conversion to inches
In [ ]:
print(M)
In [ ]:
M * 39.3701
In [ ]:
x = np.arange(20, 104.75, 3.6)
In [ ]:
x
In [ ]:
x = np.linspace(20, 104.75, 10, endpoint=False)
In [ ]:
x
In [ ]:
len(x)
In [ ]:
_, spacing = np.linspace(20, 104.75, 10, retstep=True) # returns the spacing, _ is because we don't care about the actual array now
In [ ]:
spacing
In [ ]:
x = np.array([1,2,3,4])
print("x array: {}".format(x))
print("the type of x: {}".format(type(x)))
print("the dimension of x: {}".format(np.ndim(x)))
In [ ]:
# Tour of Numpy - Part 2
x = np.array([
[[1,2], [3,4]],
[[5,6], [7,8]],
[[9,10], [11,12]]
])
print("3D array {}".format(x))
In [ ]:
print("number of dimensions: {}".format(x.ndim)) # number of dimensions
In [ ]:
print("shape of the array: {}".format(np.shape(x)))
In [ ]:
print("Sliced array: {}".format(x[1][1][0]))
In [ ]:
x = np.arange(24).reshape(4, 6)
x
In [ ]:
print("Take every third item from every second row, [::2, ::3]\n{}".format(x[::2, ::3]))
In [ ]:
# Zero array
Z = np.zeros((3, 5))
Z
In [ ]:
# Identity
I = np.identity(4)
I
In [ ]:
# k defines position of diagonal. if k = 0, we get ones on [0][0], [1][1] etc... with k = 1, we get them on [0][1], [1][2]...
# So we get elements equal to 1 on [i][i+k] where i is row index, and all the other elements are set to 0
np.eye(5, 8, k=1, dtype=int)
In [ ]:
np.eye(5, 8, k=0, dtype=int)
In [ ]:
# More work on Matrices
a = np.zeros((2,2))
a
In [ ]:
b = np.ones((1, 2))
b
In [ ]:
# Creating a constant array
# c = np.full((2, 2), 7)
c = np.full((2, 2), 7, np.double)
c
In [ ]:
# Identity matrix
d = np.eye(2)
d
In [ ]:
# Random matrix
e = np.random.random((2, 2))
e
In [ ]:
# let's do it by hand
f = np.array([[1, 2, 3, 4], [11, 22, 33, 44], [111, 222, 333, 444]])
f
In [ ]:
np.shape(f)
In [ ]:
# Standard deviation
#A = np.random.random((12,12))
A = np.arange(4).reshape(2, 2)
A
In [ ]:
print("Standard deviation per row: {}".format(np.std(A, axis=0)))
In [ ]:
print("Standard deviation per column: {}".format(np.std(A, axis=1)))
In [ ]:
# Eigen Values
from numpy import linalg
A = np.diag((1, 2, 3))
A
In [ ]:
eigenvalues, _ = linalg.eig(A)
print("For matrix\n{}\nEigen Values\n{}".format(A, eigenvalues))
In [ ]:
# Another example
B = np.array([[1, -1], [1, 1]])
B
In [ ]:
eigenvalues,_ = linalg.eig(B)
print("For matrix\n{}\nEigen Values\n{}".format(B, eigenvalues))
In [ ]:
# Covariance
A = (np.arange(4) + 1).reshape(2, 2)
print("For matrix\n{}\nCovariance\n{}".format(A, np.cov(A)))
In [ ]:
# Example of expression
def gauss(x, sigma):
return np.exp(-x**2/(2*sigma**2))/(sigma*np.sqrt(2*np.pi))
G_matrix_1 = gauss(np.linspace(-5, 5, 10), 1)
G_matrix_1
In [ ]:
G_matrix_2 = gauss(np.linspace(-5, 5, 10), 2)
G_matrix_2
In [ ]:
G_matrix = [[], []]
G_matrix[0] = G_matrix_1
G_matrix[1] = G_matrix_2
print("Covariance of two different Gauss functions:")
np.cov(G_matrix)
In [ ]:
# Project (Determinants)
A = (np.arange(4) + 1).reshape(2, 2)
A
In [ ]:
# Determinant
np.linalg.det(A)
In [ ]:
B = np.array([ [[1,2],[3,4]], [[1,2],[2,1]], [[1,3],[3,1]] ])
B
In [ ]:
# Determinant of B
np.linalg.det(B)
In [ ]:
C = np.array([[2,7],[8,4]])
C
In [ ]:
np.linalg.det(C)
In [ ]:
print("det(A*C): {}".format(np.linalg.det(np.dot(A,C))))
In [ ]:
print("det(A)*det(C): {}".format(np.linalg.det(A)*np.linalg.det(C)))
In [ ]: