108 KiB
108 KiB
In [1]:
import numpy as np
n = 10
x = np.random.normal(size=n)
print(x)[ 0.93937564 0.92308867 0.34427423 -1.37685349 2.80413696 0.25555619 1.121292 -0.42392359 -0.13913033 -0.7228852 ]
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)[[7.39090680e-02 6.22755839e-01 6.49123502e-01 2.88135577e-01 8.94704941e-01 3.03201179e-01 3.18840034e-01 8.41366128e-01 8.95033155e-01 5.99812668e-01] [9.16312434e-01 9.43984553e-02 9.29253213e-01 5.26464303e-01 2.21390371e-01 1.24224776e-01 6.89710385e-01 4.27861634e-01 7.43779698e-03 8.17834506e-01] [1.51914183e-01 5.02447063e-01 5.99647992e-01 8.36235052e-01 8.14046224e-01 1.06662069e-01 2.91416944e-01 5.89463761e-01 7.07712197e-01 4.57819238e-01] [6.21192602e-01 1.27597777e-01 9.92527681e-01 3.95743466e-01 2.66757105e-01 7.72309979e-01 1.18019623e-01 9.40415979e-03 9.27352535e-01 9.26378176e-02] [3.52989287e-02 1.01080465e-01 7.27602210e-01 4.01826811e-01 2.33826500e-01 8.47478922e-01 4.73646130e-01 4.59491404e-01 1.56857739e-01 8.65889641e-01] [1.29100624e-01 1.30387411e-01 2.49156266e-01 1.86123808e-01 3.06942115e-01 7.77873845e-01 6.61979664e-01 7.08699295e-01 4.47297587e-02 5.88738394e-01] [5.17405911e-01 9.84789579e-01 7.30334592e-01 5.61602014e-01 9.95053694e-01 5.73305640e-01 9.36545697e-01 5.29033075e-01 1.00989834e-04 6.96379806e-01] [8.23093598e-01 9.99201684e-01 9.42197297e-01 8.48268005e-01 1.65900052e-01 2.60605083e-01 3.83884553e-01 5.91410559e-02 6.89766337e-01 7.91434750e-01] [6.39621036e-03 4.53512750e-01 2.85259666e-01 6.82623709e-01 4.62905281e-01 9.88236598e-01 6.74272285e-02 5.17547294e-01 5.67238764e-01 5.67819487e-01] [5.16172219e-01 1.42083463e-01 2.01779091e-01 8.29541992e-02 8.23994188e-01 1.28183460e-01 4.64564368e-01 4.61106937e-01 7.93891335e-01 6.25458506e-01]]
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.2160073466353432 4.720889407643293 1.0362483569261072 [[ 1.17260271 3.60999067 4.15283463] [ 3.60999067 12.23423987 13.18824338] [ 4.15283463 13.18824338 21.31571995]] [31.70587399 0.09156055 2.92512799]
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.