1. NumPy#
"Let's be clear: the work of science has nothing whatever to do with consensus. Consensus is the business of politics. Science, on the contrary, requires only one investigator who happens to be right, which means that he or she has results that are verifiable by reference to the real world. In science consensus is irrelevant. What is relevant is reproducible results." -- Michael Crichton
Anaconda-യിലുള്ളതിന് പുറമെ, ഈ lecture-ന് താഴെപ്പറയുന്ന libraries ആവശ്യമാണ്:
!pip install quantecon
1.1. Overview#
NumPy numerical programming-നുള്ള ഒരു first-rate library ആണ്
academia, finance-ഉം industry-യിലും വ്യാപകമായി ഉപയോഗിക്കപ്പെടുന്നു.
Mature, fast, stable-ഉം continuous development-ൽ ഉള്ളതും.
മുൻ lectures-ൽ NumPy ഉൾപ്പെടുന്ന കുറച്ച് code നാം ഇതിനകം കണ്ടിട്ടുണ്ട്.
ഈ lecture-ൽ, നാം ഇവയെക്കുറിച്ച് കൂടുതൽ systematic ആയ ഒരു discussion ആരംഭിക്കും:
NumPy arrays-ഉം
NumPy നൽകുന്ന fundamental array processing operations-ഉം.
(ഒരു alternative reference-ന്, the official NumPy documentation കാണുക.)
നാം താഴെപ്പറയുന്ന imports ഉപയോഗിക്കും.
import numpy as np
import random
import quantecon as qe
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
from matplotlib import cm
1.2. NumPy Arrays#
NumPy പരിഹരിക്കുന്ന essential problem, fast array processing ആണ്.
NumPy define ചെയ്യുന്ന ഏറ്റവും പ്രധാനപ്പെട്ട structure ഒരു array data type ആണ്, ഔപചാരികമായി numpy.ndarray എന്നറിയപ്പെടുന്നു.
NumPy arrays scientific Python ecosystem-ന്റെ വളരെ വലിയൊരു ഭാഗത്തിന് ശക്തി പകരുന്നു.
1.2.1. Basics#
zeros മാത്രം അടങ്ങിയ ഒരു NumPy array create ചെയ്യാൻ നാം np.zeros ഉപയോഗിക്കുന്നു
a = np.zeros(3)
a
array([0., 0., 0.])
type(a)
numpy.ndarray
NumPy arrays native Python lists-നോട് ഒരു തരത്തിൽ സാമ്യമുള്ളതാണ്, ഒഴികെ
Data homogeneous ആയിരിക്കണം (എല്ലാ elements-ഉം ഒരേ type-ലുള്ളവ).
ഈ types NumPy നൽകുന്ന data types (
dtypes)-ൽ ഒന്നായിരിക്കണം.
ഈ dtypes-ൽ ഏറ്റവും പ്രധാനപ്പെട്ടവ:
float64: 64 bit floating-point number
int64: 64 bit integer
bool: 8 bit True or False
complex numbers, unsigned integers മുതലായവ represent ചെയ്യാൻ dtypes-ഉം ഉണ്ട്.
modern machines-ൽ, arrays-ക്കുള്ള default dtype float64 ആണ്
a = np.zeros(3)
type(a[0])
numpy.float64
integers ഉപയോഗിക്കണമെങ്കിൽ താഴെപ്പറയുന്ന വിധം specify ചെയ്യാം:
a = np.zeros(3, dtype=int)
type(a[0])
numpy.int64
1.2.2. Shape and Dimension#
താഴെപ്പറയുന്ന assignment പരിഗണിക്കുക
z = np.zeros(10)
ഇവിടെ z ഒരു flat array ആണ് --- row-യും column vector-ഉം അല്ല.
z.shape
(10,)
ഇവിടെ shape tuple-ന് ഒരു element മാത്രമേ ഉള്ളൂ, അത് array-യുടെ length ആണ് (ഒരു element ഉള്ള tuples ഒരു comma-യിൽ അവസാനിക്കും).
ഇതിന് ഒരു additional dimension നൽകാൻ, നമുക്ക് shape attribute change ചെയ്യാം
z.shape = (10, 1) # Convert flat array to column vector (two-dimensional)
z
array([[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.],
[0.]])
z = np.zeros(4) # Flat array
z.shape = (2, 2) # Two-dimensional array
z
array([[0., 0.],
[0., 0.]])
അവസാന case-ൽ, 2x2 array ഉണ്ടാക്കാൻ, zeros() function-ന് ഒരു tuple pass ചെയ്യാനും കഴിയും,
z = np.zeros((2, 2)) എന്നപോലെ.
1.2.3. Creating Arrays#
നാം കണ്ടതുപോലെ, np.zeros function zeros-ന്റെ ഒരു array create ചെയ്യുന്നു.
np.ones എന്താണ് create ചെയ്യുന്നതെന്ന് നിങ്ങൾക്ക് ഊഹിക്കാൻ കഴിയും.
ബന്ധപ്പെട്ട ഒന്നാണ് np.empty, ഇത് memory-യിൽ arrays create ചെയ്യുന്നു, പിന്നീട് data ഉപയോഗിച്ച് populate ചെയ്യാവുന്നത്
z = np.empty(3)
z
array([0., 0., 0.])
ഇവിടെ കാണുന്ന numbers garbage values ആണ്.
(Python 3 contiguous 64 bit memory pieces allocate ചെയ്യുന്നു, ആ memory slots-ന്റെ existing contents float64 values ആയി interpret ചെയ്യപ്പെടുന്നു)
evenly spaced numbers-ന്റെ ഒരു grid set up ചെയ്യാൻ np.linspace ഉപയോഗിക്കുക
z = np.linspace(2, 4, 5) # From 2 to 4, with 5 elements
ഒരു identity matrix create ചെയ്യാൻ np.identity അല്ലെങ്കിൽ np.eye ഉപയോഗിക്കുക
z = np.identity(2)
z
array([[1., 0.],
[0., 1.]])
കൂടാതെ, np.array ഉപയോഗിച്ച് Python lists, tuples മുതലായവയിൽ നിന്ന് NumPy arrays create ചെയ്യാം
z = np.array([10, 20]) # ndarray from Python list
z
array([10, 20])
type(z)
numpy.ndarray
z = np.array((10, 20), dtype=float) # Here 'float' is equivalent to 'np.float64'
z
array([10., 20.])
z = np.array([[1, 2], [3, 4]]) # 2D array from a list of lists
z
array([[1, 2],
[3, 4]])
np.asarray കൂടി കാണുക, ഇത് സമാനമായ ഒരു function ചെയ്യുന്നു, പക്ഷേ ഇതിനകം ഒരു NumPy array-ൽ ഉള്ള data-യുടെ
distinct copy ഉണ്ടാക്കുന്നില്ല.
numeric data അടങ്ങിയ ഒരു text file-ൽ നിന്ന് array data read ചെയ്യാൻ np.loadtxt ഉപയോഗിക്കുക ---വിശദാംശങ്ങൾക്ക് the documentation കാണുക.
1.2.4. Array Indexing#
ഒരു flat array-ക്ക്, indexing Python sequences-ന് സമാനമാണ്:
z = np.linspace(1, 2, 5)
z
array([1. , 1.25, 1.5 , 1.75, 2. ])
z[0]
np.float64(1.0)
z[0:2] # Two elements, starting at element 0
array([1. , 1.25])
z[-1]
np.float64(2.0)
2D arrays-ന്, index syntax താഴെപ്പറയുന്നത് ആണ്:
z = np.array([[1, 2], [3, 4]])
z
array([[1, 2],
[3, 4]])
z[0, 0]
np.int64(1)
z[0, 1]
np.int64(2)
ഇങ്ങനെ.
Columns-ഉം rows-ഉം താഴെപ്പറയുന്ന വിധം extract ചെയ്യാം
z[0, :]
array([1, 2])
z[:, 1]
array([2, 4])
elements extract ചെയ്യാൻ integers-ന്റെ NumPy arrays-ഉം ഉപയോഗിക്കാം
z = np.linspace(2, 4, 5)
z
array([2. , 2.5, 3. , 3.5, 4. ])
indices = np.array((0, 2, 3))
z[indices]
array([2. , 3. , 3.5])
അവസാനമായി, dtype bool-ന്റെ ഒരു array elements extract ചെയ്യാൻ ഉപയോഗിക്കാം
z
array([2. , 2.5, 3. , 3.5, 4. ])
d = np.array([0, 1, 1, 0, 0], dtype=bool)
d
array([False, True, True, False, False])
z[d]
array([2.5, 3. ])
താഴെ ഇത് എന്തുകൊണ്ട് ഉപകാരപ്രദമാണെന്ന് നാം കാണും.
ഒരു aside: slice notation ഉപയോഗിച്ച് ഒരു array-യുടെ എല്ലാ elements-ഉം ഒരു number-ന് equal ആയി set ചെയ്യാം
z = np.empty(3)
z
array([2. , 3. , 3.5])
z[:] = 42
z
array([42., 42., 42.])
1.2.5. Array Methods#
Arrays-ന് ഉപകാരപ്രദമായ methods ഉണ്ട്, അവയെല്ലാം carefully optimize ചെയ്യപ്പെട്ടതാണ്
a = np.array((4, 3, 2, 1))
a
array([4, 3, 2, 1])
a.sort() # Sorts a in place
a
array([1, 2, 3, 4])
a.sum() # Sum
np.int64(10)
a.mean() # Mean
np.float64(2.5)
a.max() # Max
np.int64(4)
a.argmax() # Returns the index of the maximal element
np.int64(3)
a.cumsum() # Cumulative sum of the elements of a
array([ 1, 3, 6, 10])
a.cumprod() # Cumulative product of the elements of a
array([ 1, 2, 6, 24])
a.var() # Variance
np.float64(1.25)
a.std() # Standard deviation
np.float64(1.118033988749895)
a.shape = (2, 2)
a.T # Equivalent to a.transpose()
array([[1, 3],
[2, 4]])
അറിഞ്ഞിരിക്കേണ്ട മറ്റൊരു method searchsorted() ആണ്.
z ഒരു nondecreasing array ആണെങ്കിൽ, z.searchsorted(a), z-ന്റെ >= a ആയ ആദ്യത്തെ
element-ന്റെ index return ചെയ്യുന്നു
z = np.linspace(2, 4, 5)
z
array([2. , 2.5, 3. , 3.5, 4. ])
z.searchsorted(2.2)
np.int64(1)
1.3. Arithmetic Operations#
+, -, *, /, ** എന്നീ operators എല്ലാം arrays-ൽ elementwise ആയി act ചെയ്യുന്നു
a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])
a + b
array([ 6, 8, 10, 12])
a * b
array([ 5, 12, 21, 32])
താഴെപ്പറയുന്ന വിധം ഓരോ element-ലേക്കും ഒരു scalar add ചെയ്യാം
a + 10
array([11, 12, 13, 14])
Scalar multiplication സമാനമാണ്
a * 10
array([10, 20, 30, 40])
two-dimensional arrays-ഉം ഇതേ general rules follow ചെയ്യുന്നു
A = np.ones((2, 2))
B = np.ones((2, 2))
A + B
array([[2., 2.],
[2., 2.]])
A + 10
array([[11., 11.],
[11., 11.]])
A * B
array([[1., 1.],
[1., 1.]])
പ്രത്യേകിച്ച്, A * B matrix product അല്ല, ഇത് ഒരു element-wise product ആണ്.
1.4. Matrix Multiplication#
matrix multiplication-ന് നാം @ symbol ഉപയോഗിക്കുന്നു, താഴെപ്പറയുന്ന വിധം:
A = np.ones((2, 2))
B = np.ones((2, 2))
A @ B
array([[2., 2.],
[2., 2.]])
flat arrays-ൽ ഈ syntax പ്രവർത്തിക്കുന്നു --- നിങ്ങൾക്ക് എന്താണ് വേണ്ടതെന്ന് NumPy ഒരു educated guess ഉണ്ടാക്കുന്നു:
A @ (0, 1)
array([1., 1.])
നാം post-multiplying ചെയ്യുന്നതിനാൽ, tuple ഒരു column vector ആയി treat ചെയ്യപ്പെടുന്നു.
1.5. Broadcasting#
(ഈ section, Jake VanderPlas നൽകിയ broadcasting-നെക്കുറിച്ചുള്ള excellent discussion extend ചെയ്യുന്നു.)
Note
Broadcasting, NumPy-യുടെ ഒരു വളരെ പ്രധാനപ്പെട്ട aspect ആണ്. അതേസമയം, advanced broadcasting താരതമ്യേന complex ആണ്, താഴെയുള്ള ചില details ആദ്യ pass-ൽ skim ചെയ്യാവുന്നതാണ്.
element-wise operations-ൽ, arrays-ന് ഒരേ shape ഉണ്ടായിരിക്കണമെന്നില്ല.
ഇത് സംഭവിക്കുമ്പോൾ, സാധ്യമാകുമ്പോഴൊക്കെ NumPy automatic ആയി arrays-നെ ഒരേ shape-ലേക്ക് expand ചെയ്യും.
NumPy-യിലെ ഈ ഉപകാരപ്രദമായ (എന്നാൽ ചിലപ്പോൾ confusing) feature-നെ broadcasting എന്ന് വിളിക്കുന്നു.
Broadcasting-ന്റെ value എന്തെന്നാൽ
forloops ഒഴിവാക്കാം, ഇത് numerical code fast ആയി run ചെയ്യാൻ സഹായിക്കുന്നു, കൂടാതെbroadcasting, arrays-ന്റെ dimensions memory-യിൽ actually create ചെയ്യാതെ operations implement ചെയ്യാൻ നമ്മെ അനുവദിക്കുന്നു, arrays വലുതായിരിക്കുമ്പോൾ ഇത് പ്രധാനമാണ്.
ഉദാഹരണത്തിന്, a ഒരു \(3 \times 3\) array (a -> (3, 3)) ആണെന്ന് കരുതുക, b മൂന്ന് elements ഉള്ള ഒരു flat array (b -> (3,)) ആണെന്നും.
ഇവ ഒരുമിച്ച് add ചെയ്യുമ്പോൾ, NumPy automatic ആയി b -> (3,), b -> (3, 3) ആയി expand ചെയ്യും.
element-wise addition ഒരു \(3 \times 3\) array-ൽ result ചെയ്യും
a = np.array(
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
b = np.array([3, 6, 9])
a + b
array([[ 4, 8, 12],
[ 7, 11, 15],
[10, 14, 18]])
ഈ broadcasting operation-ന്റെ ഒരു visual representation ഇതാ:
b -> (3, 1) ആണെങ്കിലോ?
ഈ case-ൽ, NumPy automatic ആയി b -> (3, 1), b -> (3, 3) ആയി expand ചെയ്യും.
Element-wise addition അപ്പോൾ ഒരു \(3 \times 3\) matrix-ൽ result ചെയ്യും
b.shape = (3, 1)
a + b
array([[ 4, 5, 6],
[10, 11, 12],
[16, 17, 18]])
ഈ broadcasting operation-ന്റെ ഒരു visual representation ഇതാ:
ചില cases-ൽ, രണ്ട് operands-ഉം expand ചെയ്യപ്പെടും.
a -> (3,)-ഉം b -> (3, 1)-ഉം ഉള്ളപ്പോൾ, a, a -> (3, 3) ആയി expand ചെയ്യപ്പെടും, b, b -> (3, 3) ആയും expand ചെയ്യപ്പെടും.
ഈ case-ൽ, element-wise addition ഒരു \(3 \times 3\) matrix-ൽ result ചെയ്യും
a = np.array([3, 6, 9])
b = np.array([2, 3, 4])
b.shape = (3, 1)
a + b
array([[ 5, 8, 11],
[ 6, 9, 12],
[ 7, 10, 13]])
ഈ broadcasting operation-ന്റെ ഒരു visual representation ഇതാ:
Broadcasting വളരെ ഉപകാരപ്രദമാണെങ്കിലും, ചിലപ്പോൾ ഇത് confusing ആയി തോന്നാം.
ഉദാഹരണത്തിന്, a -> (3, 2)-ഉം b -> (3,)-ഉം add ചെയ്യാൻ ശ്രമിക്കാം.
a = np.array(
[[1, 2],
[4, 5],
[7, 8]])
b = np.array([3, 6, 9])
a + b
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[62], line 7
3 [4, 5],
4 [7, 8]])
5 b = np.array([3, 6, 9])
6
----> 7 a + b
ValueError: operands could not be broadcast together with shapes (3,2) (3,)
ValueError, operands ഒരുമിച്ച് broadcast ചെയ്യാൻ കഴിഞ്ഞില്ല എന്ന് നമ്മോട് പറയുന്നു.
ഈ broadcasting എന്തുകൊണ്ട് execute ചെയ്യാൻ കഴിയില്ല എന്ന് കാണിക്കാൻ ഒരു visual representation ഇതാ:
NumPy-ക്ക് arrays-നെ ഒരേ size-ലേക്ക് expand ചെയ്യാൻ കഴിയില്ല എന്ന് നമുക്ക് കാണാം.
കാരണം, b, b -> (3,)-ൽ നിന്ന് b -> (3, 3) ആയി expand ചെയ്യുമ്പോൾ, NumPy-ക്ക് b-യെ a -> (3, 2)-യുമായി match ചെയ്യാൻ കഴിയില്ല.
higher dimensions-ലേക്ക് നീങ്ങുമ്പോൾ കാര്യങ്ങൾ കൂടുതൽ tricky ആകുന്നു.
സഹായത്തിന്, നമുക്ക് താഴെപ്പറയുന്ന rules-ന്റെ list ഉപയോഗിക്കാം:
Step 1: രണ്ട് arrays-ന്റെ dimensions match ചെയ്യാത്തപ്പോൾ, existing dimensions-ന്റെ ഇടതുവശത്ത് dimension(s) add ചെയ്ത് fewer dimensions ഉള്ളതിനെ NumPy expand ചെയ്യും.
ഉദാഹരണത്തിന്,
a -> (3, 3)-ഉംb -> (3,)-ഉം ആണെങ്കിൽ, broadcasting ഇടതുവശത്തേക്ക് ഒരു dimension add ചെയ്യും, അതിനാൽb -> (1, 3);a -> (2, 2, 2)-ഉംb -> (2, 2)-ഉം ആണെങ്കിൽ, broadcasting ഇടതുവശത്തേക്ക് ഒരു dimension add ചെയ്യും, അതിനാൽb -> (1, 2, 2);a -> (3, 2, 2)-ഉംb -> (2,)-ഉം ആണെങ്കിൽ, broadcasting ഇടതുവശത്തേക്ക് രണ്ട് dimensions add ചെയ്യും, അതിനാൽb -> (1, 1, 2)(ഇത് Step 1 രണ്ട് പ്രാവശ്യം കടന്നുപോകുന്നതായും കാണാം).
Step 2: രണ്ട് arrays-ന് ഒരേ dimension ഉണ്ടെങ്കിലും വ്യത്യസ്ത shapes ഉള്ളപ്പോൾ, shape index 1 ആയ dimensions expand ചെയ്യാൻ NumPy ശ്രമിക്കും.
ഉദാഹരണത്തിന്,
a -> (1, 3)-ഉംb -> (3, 1)-ഉം ആണെങ്കിൽ, broadcastinga-യിലുംb-യിലും shape 1 ഉള്ള dimensions expand ചെയ്യും, അതിനാൽa -> (3, 3)-ഉംb -> (3, 3)-ഉം ആകും;a -> (2, 2, 2)-ഉംb -> (1, 2, 2)-ഉം ആണെങ്കിൽ, broadcastingb-ന്റെ ആദ്യ dimension expand ചെയ്യും, അതിനാൽb -> (2, 2, 2);a -> (3, 2, 2)-ഉംb -> (1, 1, 2)-ഉം ആണെങ്കിൽ, broadcastingb-യെ shape 1 ഉള്ള എല്ലാ dimensions-ലും expand ചെയ്യും, അതിനാൽb -> (3, 2, 2).
Step 3: Step 1-ഉം 2-ഉം കഴിഞ്ഞ്, രണ്ട് arrays-ഉം ഇപ്പോഴും match ചെയ്യുന്നില്ലെങ്കിൽ, ഒരു
ValueErrorraise ചെയ്യപ്പെടും. ഉദാഹരണത്തിന്,a -> (2, 2, 3)-ഉംb -> (2, 2)-ഉം ആണെന്ന് കരുതുകStep 1 പ്രകാരം,
b,b -> (1, 2, 2)ആയി expand ചെയ്യപ്പെടും;Step 2 പ്രകാരം,
b,b -> (2, 2, 2)ആയി expand ചെയ്യപ്പെടും;ആദ്യ രണ്ട് steps-ന് ശേഷം അവ പരസ്പരം match ചെയ്യുന്നില്ല എന്ന് നമുക്ക് കാണാം. അതിനാൽ, ഒരു
ValueErrorraise ചെയ്യപ്പെടും
1.6. Mutability and Copying Arrays#
NumPy arrays, Python lists പോലെ mutable data types ആണ്.
മറ്റൊരു വിധത്തിൽ പറഞ്ഞാൽ, initialization-ന് ശേഷം അവയുടെ contents memory-യിൽ alter (mutate) ചെയ്യാവുന്നതാണ്.
ഇത് സൗകര്യപ്രദമാണ്, പക്ഷേ, Python-ന്റെ naming, reference model-ഉമായി combine ചെയ്യുമ്പോൾ, NumPy beginners-ന് mistakes-ലേക്ക് നയിക്കാം.
ഈ section-ൽ നാം ചില key issues review ചെയ്യും.
1.6.1. Mutability#
മുകളിൽ mutability-യുടെ examples നാം ഇതിനകം കണ്ടു.
NumPy array-യുടെ mutation-ന്റെ മറ്റൊരു ഉദാഹരണം ഇതാ
a = np.array([42, 44])
a
array([42, 44])
a[-1] = 0 # Change last element to 0
a
array([42, 0])
Mutability താഴെപ്പറയുന്ന behavior-ലേക്ക് നയിക്കുന്നു (MATLAB programmers-ന് ഇത് shocking ആയേക്കാം...)
rng = np.random.default_rng()
a = rng.standard_normal(3)
a
array([-0.95155504, -0.92233002, 0.65538328])
b = a
b[0] = 0.0
a
array([ 0. , -0.92233002, 0.65538328])
സംഭവിച്ചത്, b change ചെയ്ത് നാം a-യെ change ചെയ്തു എന്നതാണ്.
b എന്ന name, a-യുമായി bind ചെയ്യപ്പെട്ടിരിക്കുന്നു, array-യിലേക്കുള്ള മറ്റൊരു reference മാത്രമായി ഇത് മാറുന്നു
(Python assignment model later in the course-ൽ കൂടുതൽ വിശദമായി describe ചെയ്യപ്പെട്ടിട്ടുണ്ട്).
അതിനാൽ, ആ array-യിൽ changes വരുത്താൻ ഇതിന് equal rights ഉണ്ട്.
ഇത് യഥാർത്ഥത്തിൽ ഏറ്റവും sensible default behavior ആണ്!
copies ഉണ്ടാക്കുന്നതിന് പകരം data-യിലേക്കുള്ള pointers മാത്രമേ നാം pass ചെയ്യുന്നുള്ളൂ എന്നാണ് ഇതിനർത്ഥം.
copies ഉണ്ടാക്കുന്നത് speed-ന്റെയും memory-യുടെയും കാര്യത്തിൽ expensive ആണ്.
1.6.2. Making Copies#
ആവശ്യമുള്ളപ്പോൾ b-യെ a-യുടെ independent copy ആക്കുന്നത് തീർച്ചയായും സാധ്യമാണ്.
ഇത് np.copy ഉപയോഗിച്ച് ചെയ്യാം
a = rng.standard_normal(3)
a
array([-0.86805085, -0.79205297, -0.39914216])
b = np.copy(a)
b
array([-0.86805085, -0.79205297, -0.39914216])
ഇപ്പോൾ b ഒരു independent copy ആണ് (ഇതിനെ deep copy എന്ന് വിളിക്കുന്നു)
b[:] = 1
b
array([1., 1., 1.])
a
array([-0.86805085, -0.79205297, -0.39914216])
b-യിലെ change, a-യെ ബാധിച്ചിട്ടില്ല എന്നത് ശ്രദ്ധിക്കുക.
1.7. Additional Features#
NumPy-യുടെ മറ്റ് ചില ഉപകാരപ്രദമായ features നോക്കാം.
1.7.1. Universal Functions#
NumPy, log, exp, sin മുതലായ standard functions-ന്റെ versions നൽകുന്നു, ഇവ arrays-ൽ element-wise ആയി act ചെയ്യുന്നു
z = np.array([1, 2, 3])
np.sin(z)
array([0.84147098, 0.90929743, 0.14112001])
ഇത് explicit element-by-element loops-ന്റെ ആവശ്യകത ഇല്ലാതാക്കുന്നു, ഉദാഹരണത്തിന്
n = len(z)
y = np.empty(n)
for i in range(n):
y[i] = np.sin(z[i])
arrays-ൽ element-wise ആയി act ചെയ്യുന്നതിനാൽ, ഈ functions-നെ ചിലപ്പോൾ vectorized functions എന്ന് വിളിക്കുന്നു.
NumPy-speak-ൽ, അവയെ ufuncs, അല്ലെങ്കിൽ universal functions എന്നും വിളിക്കുന്നു.
മുകളിൽ നാം കണ്ടതുപോലെ, usual arithmetic operations (+, *, etc.)-ഉം
element-wise ആയി പ്രവർത്തിക്കുന്നു, ഇവയെ ufuncs-ഉമായി combine ചെയ്യുന്നത് fast element-wise functions-ന്റെ വളരെ വലിയൊരു set നൽകുന്നു.
z
array([1, 2, 3])
(1 / np.sqrt(2 * np.pi)) * np.exp(- 0.5 * z**2)
array([0.24197072, 0.05399097, 0.00443185])
എല്ലാ user-defined functions-ഉം element-wise ആയി act ചെയ്യില്ല.
ഉദാഹരണത്തിന്, താഴെ define ചെയ്ത f function-ന് ഒരു NumPy array pass ചെയ്യുന്നത് ഒരു ValueError-ന് കാരണമാകുന്നു
def f(x):
return 1 if x > 0 else 0
NumPy function np.where, ഒരു vectorized alternative നൽകുന്നു:
x = rng.standard_normal(4)
x
array([-0.93935317, 1.96917983, -1.84740532, 0.53304951])
np.where(x > 0, 1, 0) # Insert 1 if x > 0 true, otherwise 0
array([0, 1, 0, 1])
ഒരു given function vectorize ചെയ്യാൻ np.vectorize-ഉം ഉപയോഗിക്കാം
f = np.vectorize(f)
f(x) # Passing the same vector x as in the previous example
array([0, 1, 0, 1])
എന്നിരുന്നാലും, ഈ approach, ഒരു കൂടുതൽ carefully crafted vectorized function-ന്റെ speed ലഭിക്കില്ല എപ്പോഴും.
(പിന്നീട് JAX-ന്, np.vectorize-ന്റെ ഒരു powerful version ഉണ്ടെന്ന് നാം കാണും, ഇത് highly efficient code generate ചെയ്യാൻ കഴിയുന്നതും സാധാരണയായി ചെയ്യുന്നതും ആണ്.)
1.7.2. Comparisons#
Rule ആയി, arrays-ൽ comparisons element-wise ആയി ചെയ്യപ്പെടുന്നു
z = np.array([2, 3])
y = np.array([2, 3])
z == y
array([ True, True])
y[0] = 5
z == y
array([False, True])
z != y
array([ True, False])
>, <, >=, <=-ന്റെ situation സമാനമാണ്.
Scalars-നെതിരെയും നമുക്ക് comparisons ചെയ്യാം
z = np.linspace(0, 10, 5)
z
array([ 0. , 2.5, 5. , 7.5, 10. ])
z > 3
array([False, False, True, True, True])
conditional extraction-ന് ഇത് പ്രത്യേകിച്ച് ഉപകാരപ്രദമാണ്
b = z > 3
b
array([False, False, True, True, True])
z[b]
array([ 5. , 7.5, 10. ])
തീർച്ചയായും നമുക്ക് ഇത് ഒരു step-ൽ perform ചെയ്യാം---സാധാരണയായി നാം ചെയ്യുകയും ചെയ്യുന്നു
z[z > 3]
array([ 5. , 7.5, 10. ])
1.7.3. Sub-packages#
NumPy, അതിന്റെ sub-packages വഴി scientific programming-മായി ബന്ധപ്പെട്ട additional functionality നൽകുന്നു.
NumPy-യുടെ
random Generator ഉപയോഗിച്ച് random variables generate ചെയ്യുന്നത് നാം ഇതിനകം കണ്ടു.
z = rng.standard_normal(10000) # Generate standard normals
y = rng.binomial(10, 0.5, size=1000) # 1,000 draws from Bin(10, 0.5)
y.mean()
np.float64(5.018)
സാധാരണയായി ഉപയോഗിക്കുന്ന മറ്റൊരു subpackage np.linalg ആണ്
A = np.array([[1, 2], [3, 4]])
np.linalg.det(A) # Compute the determinant
np.float64(-2.0000000000000004)
np.linalg.inv(A) # Compute the inverse
array([[-2. , 1. ],
[ 1.5, -0.5]])
ഈ functionality-യുടെ ഭൂരിഭാഗവും SciPy-ലും ലഭ്യമാണ്, ഇത് NumPy-ന്റെ മുകളിൽ built ചെയ്ത modules-ന്റെ ഒരു collection ആണ്.
SciPy versions നാം soon കൂടുതൽ വിശദമായി cover ചെയ്യും.
NumPy-ൽ ലഭ്യമായതിന്റെ ഒരു comprehensive list-ന്, this documentation കാണുക.
1.7.4. Implicit Multithreading#
Previously നാം multithreading വഴി parallelization-ന്റെ concept discuss ചെയ്തു.
NumPy, അതിന്റെ compiled code-ന്റെ ഭൂരിഭാഗത്തിലും multithreading implement ചെയ്യാൻ ശ്രമിക്കുന്നു.
ഇത് action-ൽ കാണാൻ ഒരു example നോക്കാം.
randomly generate ചെയ്ത matrices-ന്റെ ഒരു large number-ന്റെ eigenvalues, അടുത്ത code compute ചെയ്യുന്നു.
ഇത് run ചെയ്യാൻ കുറച്ച് seconds എടുക്കുന്നു.
n = 20
m = 1000
for i in range(n):
X = rng.standard_normal((m, m))
λ = np.linalg.eigvals(X)
ഇപ്പോൾ, ഈ code run ചെയ്യുമ്പോൾ നമ്മുടെ machine-ലെ htop system monitor-ന്റെ output നോക്കാം:
8 CPUs-ൽ 4-ഉം full speed-ൽ run ചെയ്യുന്നത് നമുക്ക് കാണാം.
NumPy-യുടെ eigvals routine, tasks neatly split ചെയ്ത് different threads-ലേക്ക്
distribute ചെയ്യുന്നതിനാലാണ് ഇത്.
1.8. Exercises#
Exercise 1.1
താഴെപ്പറയുന്ന polynomial expression പരിഗണിക്കുക
Earlier, efficiency പരിഗണിക്കാതെ (1.1) evaluate ചെയ്യാൻ നിങ്ങൾ ഒരു simple function p(x, coeff) എഴുതി.
ഇപ്പോൾ ഇതേ job ചെയ്യുന്ന ഒരു new function എഴുതുക, പക്ഷേ അതിന്റെ computations-ന് ഏത് തരത്തിലുള്ള Python loop-നും പകരം NumPy arrays-ഉം array operations-ഉം ഉപയോഗിക്കണം.
(ഈ functionality ഇതിനകം np.poly1d ആയി implement ചെയ്യപ്പെട്ടിട്ടുണ്ട്, പക്ഷേ exercise-ന്റെ ആവശ്യത്തിന് ഈ class ഉപയോഗിക്കരുത്)
Hint
np.cumprod() ഉപയോഗിക്കുക
Solution
ഈ code job ചെയ്യുന്നു
def p(x, coef):
X = np.ones_like(coef)
X[1:] = x
y = np.cumprod(X) # y = [1, x, x**2,...]
return coef @ y
നമുക്ക് ഇത് test ചെയ്യാം
x = 2
coef = np.linspace(2, 4, 3)
print(coef)
print(p(x, coef))
# For comparison
q = np.poly1d(np.flip(coef))
print(q(x))
[2. 3. 4.]
24.0
24.0
Exercise 1.2
q, q.sum() == 1 ഉള്ള n length-ന്റെ ഒരു NumPy array ആണെന്ന് കരുതുക.
q, ഒരു probability mass function represent ചെയ്യുന്നു എന്ന് കരുതുക.
\(\mathbb P\{x = i\} = q_i\) ആയ ഒരു discrete random variable \(x\) generate ചെയ്യാൻ നാം ആഗ്രഹിക്കുന്നു.
മറ്റൊരു വിധത്തിൽ പറഞ്ഞാൽ, x, range(len(q))-ൽ values എടുക്കുന്നു, x = i, q[i] probability-യിൽ ആണ്.
standard (inverse transform) algorithm താഴെപ്പറയുന്നത് ആണ്:
unit interval \([0, 1]\)-നെ \(n\) subintervals \(I_0, I_1, \ldots, I_{n-1}\) ആയി divide ചെയ്യുക, അതായത് \(I_i\)-യുടെ length \(q_i\) ആയിരിക്കണം.
\([0, 1]\)-ൽ ഒരു uniform random variable \(U\) draw ചെയ്ത്, \(U \in I_i\) ആയ \(i\) return ചെയ്യുക.
\(i\) draw ചെയ്യാനുള്ള probability, \(I_i\)-യുടെ length ആണ്, ഇത് \(q_i\)-ന് equal ആണ്.
നമുക്ക് algorithm താഴെപ്പറയുന്ന വിധം implement ചെയ്യാം
from random import uniform
def sample(q):
a = 0.0
U = uniform(0, 1)
for i in range(len(q)):
if a < U <= a + q[i]:
return i
a = a + q[i]
ഇത് എങ്ങനെ പ്രവർത്തിക്കുന്നു എന്ന് കാണാൻ കഴിയുന്നില്ലെങ്കിൽ, q = [0.25, 0.75] പോലുള്ള ഒരു simple example-ന്റെ flow ചിന്തിച്ച് നോക്കുക.
paper-ൽ intervals sketch ചെയ്യുന്നത് സഹായകമാണ്.
explicit loops ഒഴിവാക്കി, NumPy ഉപയോഗിച്ച് ഇത് speed up ചെയ്യുകയാണ് നിങ്ങളുടെ exercise
Hint
np.searchsorted-ഉം np.cumsum-ഉം ഉപയോഗിക്കുക
നിങ്ങൾക്ക് കഴിയുമെങ്കിൽ, DiscreteRV എന്ന ഒരു class ആയി functionality implement ചെയ്യുക, ഇവിടെ
class-ന്റെ ഒരു instance-ന്റെ data, probabilities-ന്റെ vector
qആണ്class-ന് ഒരു
draw()method ഉണ്ട്, ഇത് മുകളിൽ describe ചെയ്ത algorithm പ്രകാരം ഒരു draw return ചെയ്യുന്നു
നിങ്ങൾക്ക് കഴിയുമെങ്കിൽ, draw(k), q-ൽ നിന്ന് k draws return ചെയ്യുന്ന വിധം method എഴുതുക.
Solution
ഇതാ ഒരു solution-ലേക്കുള്ള നമ്മുടെ first pass:
from numpy import cumsum
class DiscreteRV:
"""
Generates an array of draws from a discrete random variable with vector of
probabilities given by q.
"""
def __init__(self, q, seed=None):
"""
The argument q is a NumPy array, or array like, nonnegative and sums
to 1
"""
self.q = q
self.Q = cumsum(q)
self.rng = np.random.default_rng(seed)
def draw(self, k=1):
"""
Returns k draws from q. For each such draw, the value i is returned
with probability q[i].
"""
return self.Q.searchsorted(self.rng.uniform(0, 1, size=k))
Logic obvious അല്ല, പക്ഷേ നിങ്ങൾ time എടുത്ത് slowly read ചെയ്താൽ, നിങ്ങൾക്ക് ഇത് understand ചെയ്യാൻ കഴിയും.
എന്നിരുന്നാലും, ഇവിടെ ഒരു problem ഉണ്ട്.
discreteRV-യുടെ ഒരു instance create ചെയ്ത ശേഷം q alter ചെയ്യപ്പെട്ടു എന്ന് കരുതുക, ഉദാഹരണത്തിന്
q = (0.1, 0.9)
d = DiscreteRV(q)
d.q = (0.5, 0.5)
Problem, Q accordingly change ആകുന്നില്ല, draw method-ൽ ഉപയോഗിക്കുന്ന data
Q ആണ് എന്നതാണ്.
ഇത് deal ചെയ്യാൻ, ഒരു option, draw method call ചെയ്യുന്ന ഓരോ പ്രാവശ്യവും Q compute
ചെയ്യുക എന്നതാണ്.
പക്ഷേ ഇത്, Q once-off compute ചെയ്യുന്നതിനെ അപേക്ഷിച്ച് inefficient ആണ്.
Descriptors ഉപയോഗിക്കുക എന്നതാണ് ഒരു മികച്ച option.
നാം ആഗ്രഹിക്കുന്ന വിധം behave ചെയ്യുന്ന descriptors ഉപയോഗിച്ചുള്ള quantecon library-ൽ നിന്നുള്ള ഒരു solution ഇവിടെ കാണാം.
Exercise 1.3
empirical cumulative distribution function-നെക്കുറിച്ചുള്ള നമ്മുടെ earlier discussion ഓർക്കുക.
നിങ്ങളുടെ task,
NumPy ഉപയോഗിച്ച്
__call__method കൂടുതൽ efficient ആക്കുക.\([a, b]\)-ൽ ECDF plot ചെയ്യുന്ന ഒരു method add ചെയ്യുക, ഇവിടെ \(a\)-ഉം \(b\)-ഉം method parameters ആണ്.
Solution
താഴെ ഒരു example solution നൽകിയിരിക്കുന്നു.
സത്തയിൽ, QuantEcon-ൽ നിന്നുള്ള this code നാം എടുത്ത് ഒരു plot method add ചെയ്തിരിക്കുന്നു
"""
Modifies ecdf.py from QuantEcon to add in a plot method
"""
class ECDF:
"""
One-dimensional empirical distribution function given a vector of
observations.
Parameters
----------
observations : array_like
An array of observations
Attributes
----------
observations : array_like
An array of observations
"""
def __init__(self, observations):
self.observations = np.asarray(observations)
def __call__(self, x):
"""
Evaluates the ecdf at x
Parameters
----------
x : scalar(float)
The x at which the ecdf is evaluated
Returns
-------
scalar(float)
Fraction of the sample less than x
"""
return np.mean(self.observations <= x)
def plot(self, ax, a=None, b=None):
"""
Plot the ecdf on the interval [a, b].
Parameters
----------
a : scalar(float), optional(default=None)
Lower endpoint of the plot interval
b : scalar(float), optional(default=None)
Upper endpoint of the plot interval
"""
# === choose reasonable interval if [a, b] not specified === #
if a is None:
a = self.observations.min() - self.observations.std()
if b is None:
b = self.observations.max() + self.observations.std()
# === generate plot === #
x_vals = np.linspace(a, b, num=100)
f = np.vectorize(self.__call__)
ax.plot(x_vals, f(x_vals))
plt.show()
ഇതാ usage-ന്റെ ഒരു example
fig, ax = plt.subplots()
rng = np.random.default_rng()
X = rng.standard_normal(1000)
F = ECDF(X)
F.plot(ax)
Exercise 1.4
Numpy-ലെ broadcasting, for loops ഉപയോഗിക്കാതെ different number of dimensions ഉള്ള arrays-ൽ element-wise operations conduct ചെയ്യാൻ നമ്മെ സഹായിക്കുമെന്ന് ഓർക്കുക.
ഈ exercise-ൽ, താഴെപ്പറയുന്ന broadcasting operations-ന്റെ result replicate ചെയ്യാൻ for loops ഉപയോഗിച്ച് ശ്രമിക്കുക.
Part1: for loops ഉപയോഗിച്ച് ഈ simple example replicate ചെയ്യാൻ ശ്രമിക്കുക, താഴെയുള്ള broadcasting operation-ഉമായി നിങ്ങളുടെ results compare ചെയ്യുക.
rng = np.random.default_rng(123)
x = rng.standard_normal((4, 4))
y = rng.standard_normal(4)
A = x / y
ഇതാ output
print(A)
Part2: താഴെപ്പറയുന്ന broadcasting operation-ന്റെ result replicate ചെയ്യാൻ നീങ്ങുക. അതേസമയം, broadcasting-ന്റെയും നിങ്ങൾ implement ചെയ്യുന്ന for loop-ന്റെയും speeds compare ചെയ്യുക.
exercise-ന്റെ ഈ part-ന്, execution time ചെയ്യാൻ quantecon library-യിലെ tic/toc functions ഉപയോഗിക്കാം.
ഈ library install ചെയ്തിട്ടുണ്ട് എന്ന് ഉറപ്പുവരുത്താം.
!pip install quantecon
ഇപ്പോൾ നമുക്ക് quantecon package import ചെയ്യാം.
rng = np.random.default_rng(123)
x = rng.standard_normal((1000, 100, 100))
y = rng.standard_normal(100)
with qe.Timer("Broadcasting operation"):
B = x / y
Broadcasting operation: 0.0093 seconds elapsed
ഇതാ output
print(B)
Solution
Part 1 Solution
rng = np.random.default_rng(123)
x = rng.standard_normal((4, 4))
y = rng.standard_normal(4)
C = np.empty_like(x)
n = len(x)
for i in range(n):
for j in range(n):
C[i, j] = x[i, j] / y[j]
നിങ്ങളുടെ answer check ചെയ്യാൻ results compare ചെയ്യുക
print(C)
നിങ്ങളുടെ answer check ചെയ്യാൻ array_equal()-ഉം ഉപയോഗിക്കാം
print(np.array_equal(A, C))
True
Part 2 Solution
rng = np.random.default_rng(123)
x = rng.standard_normal((1000, 100, 100))
y = rng.standard_normal(100)
with qe.Timer("For loop operation"):
D = np.empty_like(x)
d1, d2, d3 = x.shape
for i in range(d1):
for j in range(d2):
for k in range(d3):
D[i, j, k] = x[i, j, k] / y[k]
For loop operation: 3.7468 seconds elapsed
for loop, broadcasting operation-നെക്കാൾ വളരെയധികം time എടുക്കുന്നു എന്നത് ശ്രദ്ധിക്കുക.
നിങ്ങളുടെ answer check ചെയ്യാൻ results compare ചെയ്യുക
print(D)
print(np.array_equal(B, D))
True