108 KiB
108 KiB
In [1]:
import numpy as np
n = 10
x = np.random.normal(size=n)
print(x)[-0.87136737 1.4300745 -0.3322326 -0.66758934 -1.00283636 -0.27625974 1.89249454 -0.25006757 0.73565195 0.33697405]
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.52428467 0.89421873 0.57286194 0.35473061 0.34626037 0.77640601 0.60707801 0.60515083 0.41335011 0.16061448] [0.51508374 0.7651899 0.07657669 0.14045566 0.92863147 0.32541271 0.62761922 0.20965182 0.55051175 0.14072037] [0.70311568 0.03617972 0.22335823 0.45797073 0.2223808 0.86208133 0.64452882 0.0770789 0.7866024 0.45739087] [0.38469441 0.16105443 0.09873132 0.21913656 0.34561469 0.521986 0.89261458 0.75628425 0.25070907 0.96504592] [0.40506137 0.48241947 0.32158804 0.45112054 0.5783575 0.3267061 0.96261297 0.25216021 0.94271589 0.60651016] [0.91410101 0.48217977 0.39116042 0.99397049 0.80768431 0.68650491 0.04184636 0.5174581 0.86144422 0.46794929] [0.62405149 0.27498198 0.63550707 0.85902603 0.38644889 0.86858926 0.68282653 0.65554133 0.81051173 0.00281835] [0.66001778 0.64858537 0.90034533 0.62389761 0.5333734 0.75390337 0.97926642 0.9893405 0.61605739 0.51905011] [0.59277468 0.52678301 0.68347072 0.76707201 0.08821204 0.55220861 0.14648477 0.17606773 0.59609892 0.83723539] [0.40961677 0.07325301 0.34652523 0.72201591 0.66250644 0.6367311 0.79577619 0.85212606 0.86811137 0.43001767]]
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.03499693245669077 4.187268182169147 -0.19627430151896047 [[ 1.0622197 3.16232481 2.98018358] [ 3.16232481 10.48442771 8.58039072] [ 2.98018358 8.58039072 13.66351927]] [21.70244042 0.07978179 3.42794448]
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.