107 KiB
107 KiB
In [1]:
import numpy as np
n = 10
x = np.random.normal(size=n)
print(x)[-0.61405495 -0.32387761 1.12215626 -0.4519346 0.45982886 0.30950817 -0.99310295 0.38292897 1.44400587 -0.28739213]
In [2]:
import numpy as np
x = np.array([1, 2, 3])
print(x)[1 2 3]
In [3]:
import numpy as np
x = np.log(np.array([4, 7, 8]))
print(x)[1.38629436 1.94591015 2.07944154]
In [4]:
import numpy as np
from math import log
x = np.array([4, 7, 8])
for i in range(0, len(x)):
x[i] = log(x[i])
print(x)[1 1 2]
In [5]:
import numpy as np
x = np.log(np.array([4, 7, 8], dtype = np.float64))
print(x)[1.38629436 1.94591015 2.07944154]
In [6]:
import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0]))
print(x)[1.38629436 1.94591015 2.07944154]
In [7]:
import numpy as np
x = np.log(np.array([4.0, 7.0, 8.0]))
print(x.itemsize)8
In [8]:
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
print(A)[[1.38629436 1.94591015 2.07944154] [1.09861229 2.30258509 2.39789527] [1.38629436 1.60943791 1.94591015]]
In [9]:
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
# print the first column, row-major order and elements start with 0
print(A[:,0])[1.38629436 1.09861229 1.38629436]
In [10]:
import numpy as np
A = np.log(np.array([ [4.0, 7.0, 8.0], [3.0, 10.0, 11.0], [4.0, 5.0, 7.0] ]))
# print the first column, row-major order and elements start with 0
print(A[1,:])[1.09861229 2.30258509 2.39789527]
In [11]:
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to zero
A = np.zeros( (n, n) )
print(A)[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
In [12]:
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to one
A = np.ones( (n, n) )
print(A)[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
In [13]:
import numpy as np
n = 10
# define a matrix of dimension 10 x 10 and set all elements to random numbers with x \in [0, 1]
A = np.random.rand(n, n)
print(A)[[0.56237933 0.1025416 0.22316615 0.85503611 0.99057991 0.94485713 0.03845685 0.74210311 0.91828517 0.29234676] [0.20570617 0.55663962 0.14395076 0.63318471 0.88506797 0.79793482 0.21572768 0.70817694 0.98132273 0.47451039] [0.10238488 0.08006058 0.26302678 0.0532921 0.89460147 0.11961481 0.90199101 0.17545974 0.89611233 0.76803496] [0.02942421 0.99685226 0.12021078 0.72362232 0.02251816 0.67824444 0.70638884 0.32589372 0.39505738 0.5378907 ] [0.88579143 0.45698195 0.61143771 0.59118363 0.92409815 0.68270325 0.58619632 0.77509651 0.93481453 0.87482389] [0.40760778 0.62136996 0.7761515 0.76287014 0.81606967 0.17622628 0.65211459 0.55721129 0.20955256 0.20757376] [0.59910949 0.22876398 0.59992922 0.0586431 0.71238022 0.59293896 0.58318087 0.50099904 0.15835637 0.1221625 ] [0.3510257 0.5364078 0.15260744 0.21019486 0.53797608 0.92789992 0.91110946 0.33914178 0.20822698 0.62281444] [0.88111434 0.05117772 0.30607722 0.34939146 0.30612395 0.06384183 0.23829156 0.11534663 0.44474604 0.49985968] [0.17578147 0.63422131 0.86986521 0.53418362 0.30679815 0.33175971 0.83105371 0.97275788 0.37250759 0.05652557]]
In [14]:
# Importing various packages
import numpy as np
n = 100
x = np.random.normal(size=n)
print(np.mean(x))
y = 4+3*x+np.random.normal(size=n)
print(np.mean(y))
z = x**3+np.random.normal(size=n)
print(np.mean(z))
W = np.vstack((x, y, z))
Sigma = np.cov(W)
print(Sigma)
Eigvals, Eigvecs = np.linalg.eig(Sigma)
print(Eigvals)-0.07252404965482757 3.80181024148491 -0.6317631821295671 [[ 0.91103528 2.76447289 2.63577152] [ 2.76447289 9.56058946 7.79271587] [ 2.63577152 7.79271587 15.61196303]] [21.62109277 0.08978688 4.37270812]
In [15]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy import sparse
eye = np.eye(4)
print(eye)
sparse_mtx = sparse.csr_matrix(eye)
print(sparse_mtx)
x = np.linspace(-10,10,100)
y = np.sin(x)
plt.plot(x,y,marker='x')
plt.show()[[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] (0, 0) 1.0 (1, 1) 1.0 (2, 2) 1.0 (3, 3) 1.0
In [16]:
"""
Simple code that tests various numpy functions
"""
import numpy as np
# Simple test-matrix of dim 3 x 4
a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9],[10, 11, 12]],dtype=np.float64)
print(f"The test matrix:{a}")
# This is the total mean summed over all elements, which here has to be 6.5
print(f"This is the total mean summed over all elements:{np.mean(a,dtype=np.float64)}")
# This is the mean for each column, it returns an array with the mean values for each column. It returns a row-like vector
print(f"This is the mean for each column:{np.mean(a, axis=0, keepdims=True,dtype=np.float64)}")
# This is the mean value for each row, it returns an array via the keepdims option which is a column-like vector if
# keepdims=True. Else it return a row-like vector
# Try setting keepdims=False
print(f"This is the mean value for each row:{np.mean(a, axis=1, keepdims=True,dtype=np.float64)}")
# We print then the mean value for each row by setting keepdims=False
print(f"This is the mean value for each row with keepdims false:{np.mean(a, axis=1, keepdims=False,dtype=np.float64)}")The test matrix:[[ 1. 2. 3.] [ 4. 5. 6.] [ 7. 8. 9.] [10. 11. 12.]] This is the total mean summed over all elements:6.5 This is the mean for each column:[[5.5 6.5 7.5]] This is the mean value for each row:[[ 2.] [ 5.] [ 8.] [11.]] This is the mean value for each row with keepdims false:[ 2. 5. 8. 11.]
In [17]:
# Ravel return a contiguous flattened array.
print(f"Flatten the matrix:{np.ravel(a)}")
# It is the same as reshaping the matrix into a one-dimensional array
print(f"Reshape the matrix to a one-dim array:{a.reshape(-1)}")
# ‘C’ means to index the elements in row-major, C-style order, with the last axis index changing fastest, back to the first axis index changing slowest.
# ‘F’ means to index the elements in column-major, Fortran-style order, with the first index changing fastest, and the last index changing slowest
print(np.ravel(a, order='F'))
# When order is ‘A’, it will preserve the array’s ‘C’ or ‘F’ ordering
# ‘A’ means to read the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.
# ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.
# Transposing it
print(np.ravel(a.T))
print(np.ravel(a.T, order='A'))Flatten the matrix:[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.] Reshape the matrix to a one-dim array:[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.] [ 1. 4. 7. 10. 2. 5. 8. 11. 3. 6. 9. 12.] [ 1. 4. 7. 10. 2. 5. 8. 11. 3. 6. 9. 12.] [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.]
Warning:
Output truncated. This notebook contains too many cells to display efficiently.