{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lecture 2"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Check versions:\n",
" numpy version :'1.20.3'\n",
" matplotlib version:'3.4.3'\n"
]
}
],
"source": [
"%run set_env.py\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Array attributes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ndarray is the NumPy base/super class.
\n",
"The ndarray class has several fields/methods.
\n",
"Among them:\n",
"* dtype: type of the (homogeneous) elements\n",
"* ndim : dimensionality (#axes) of the array\n",
"* shape: dimensions of the array (tuple of ints)\n",
"* size : #elements in the array\n",
"* itemsize: memory occupied by 1 element\n",
"* nbytes : total #bytes consumed by the el. of the array\n",
"* strides : strides of data in memory\n",
"* flags : dictionary containing info on memory use.\n",
"* T : transpose of the ndarray"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a := np.arange(20).reshape((4,5)) :\n",
"[[ 0 1 2 3 4]\n",
" [ 5 6 7 8 9]\n",
" [10 11 12 13 14]\n",
" [15 16 17 18 19]]\n",
"\n",
" type(a) :''\n",
" a.dtype:'int64'\n",
" a.ndim:'2'\n",
" a.shape:'(4, 5)'\n",
" a.size:'20'\n",
" a.itemsize:'8'\n",
" a.nbytes:'160'\n",
" a.strides:'(40, 8)'\n",
"\n",
" a.real :\n",
"[[ 0 1 2 3 4]\n",
" [ 5 6 7 8 9]\n",
" [10 11 12 13 14]\n",
" [15 16 17 18 19]]\n",
"\n",
" a.imag :\n",
"[[0 0 0 0 0]\n",
" [0 0 0 0 0]\n",
" [0 0 0 0 0]\n",
" [0 0 0 0 0]]\n",
"\n",
" a.flags:\n",
" C_CONTIGUOUS : True\n",
" F_CONTIGUOUS : False\n",
" OWNDATA : False\n",
" WRITEABLE : True\n",
" ALIGNED : True\n",
" WRITEBACKIFCOPY : False\n",
" UPDATEIFCOPY : False\n",
"\n",
" a.T:\n",
"[[ 0 5 10 15]\n",
" [ 1 6 11 16]\n",
" [ 2 7 12 17]\n",
" [ 3 8 13 18]\n",
" [ 4 9 14 19]]\n"
]
}
],
"source": [
"# arange(20) -> Linux x86_64: 'int64'\n",
"a=np.arange(20).reshape(4,5)\n",
"print(f\"a := np.arange(20).reshape((4,5)) :\\n{a}\\n\")\n",
"print(f\" type(a) :'{type(a)}'\")\n",
"print(f\" a.dtype:'{a.dtype}'\")\n",
"print(f\" a.ndim:'{a.ndim}'\")\n",
"print(f\" a.shape:'{a.shape}'\")\n",
"print(f\" a.size:'{a.size}'\")\n",
"print(f\" a.itemsize:'{a.itemsize}'\")\n",
"print(f\" a.nbytes:'{a.nbytes}'\")\n",
"print(f\" a.strides:'{a.strides}'\\n\")\n",
"print(f\" a.real :\\n{a.real}\\n\")\n",
"print(f\" a.imag :\\n{a.imag}\\n\")\n",
"print(f\" a.flags:\\n{a.flags}\")\n",
"print(f\" a.T:\\n{a.T}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice the fundamental difference!"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a := np.arange(20).reshape((4,5)) :\n",
"[[ 0 1 2 3 4]\n",
" [ 5 6 7 8 9]\n",
" [10 11 12 13 14]\n",
" [15 16 17 18 19]]\n",
"\n",
"b :=b np.arange(20.).reshape((4,5)) :\n",
"[[ 0. 1. 2. 3. 4.]\n",
" [ 5. 6. 7. 8. 9.]\n",
" [10. 11. 12. 13. 14.]\n",
" [15. 16. 17. 18. 19.]]\n",
"\n",
" type(a) :''\n",
" type(b) :''\n"
]
}
],
"source": [
"a = np.arange(20).reshape(4,5)\n",
"b = np.arange(20.).reshape(4,5)\n",
"\n",
"print(f\"a := np.arange(20).reshape((4,5)) :\\n{a}\\n\")\n",
"print(f\"b :=b np.arange(20.).reshape((4,5)) :\\n{b}\\n\")\n",
"print(f\" type(a) :'{type(a)}'\")\n",
"print(f\" type(b) :'{type(b)}'\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" a.ndim:'2'\n",
" b.ndim:'2'\n",
" a.shape:'(4, 5)'\n",
" b.shape:'(4, 5)'\n",
" a.size:'20'\n",
" b.size:'20'\n",
" a.dtype:'int64'\n",
" b.dtype:'float64'\n",
" a.itemsize:'8'\n",
" b.itemsize:'8'\n",
" a.nbytes:'160'\n",
" b.nbytes:'160'\n"
]
}
],
"source": [
"print(f\" a.ndim:'{a.ndim}'\")\n",
"print(f\" b.ndim:'{b.ndim}'\")\n",
"print(f\" a.shape:'{a.shape}'\")\n",
"print(f\" b.shape:'{b.shape}'\")\n",
"print(f\" a.size:'{a.size}'\")\n",
"print(f\" b.size:'{b.size}'\")\n",
"print(f\" a.dtype:'{a.dtype}'\")\n",
"print(f\" b.dtype:'{b.dtype}'\")\n",
"print(f\" a.itemsize:'{a.itemsize}'\")\n",
"print(f\" b.itemsize:'{b.itemsize}'\")\n",
"print(f\" a.nbytes:'{a.nbytes}'\")\n",
"print(f\" b.nbytes:'{b.nbytes}'\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create specific arrays (types & content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### a.Data Types"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy support several native data types e.g.:\n",
"* int8, int16 int32, int64\n",
"* uint8, uint16, uint32, uint64\n",
"* float16, float32, float64\n",
"* complex64, complex128 \n",
"* ..."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"i1 := np.arange(10,dtype='int32'):\n",
" [0 1 2 3 4 5 6 7 8 9]\n",
" i1.dtype:'int32'\n",
" i1.size :'10'\n",
" i1.itemsize:'4'\n",
" i1.nbytes:'40'\n",
"\n",
"z1 := np.arange(10,dtype='complex128'):\n",
" [0.+0.j 1.+0.j 2.+0.j 3.+0.j 4.+0.j 5.+0.j 6.+0.j 7.+0.j 8.+0.j 9.+0.j]\n",
"\n",
" z1.dtype:'complex128'\n",
" z1.size :'10'\n",
" z1.itemsize:'16'\n",
" z1.nbytes:'160'\n"
]
}
],
"source": [
"# Create arrays of a certain data type\n",
"i1=np.arange(10,dtype='int32')\n",
"print(f\"i1 := np.arange(10,dtype='int32'):\\n {i1}\")\n",
"print(f\" i1.dtype:'{i1.dtype}'\")\n",
"print(f\" i1.size :'{i1.size}'\")\n",
"print(f\" i1.itemsize:'{i1.itemsize}'\")\n",
"print(f\" i1.nbytes:'{i1.nbytes}'\")\n",
"\n",
"z1=np.arange(10,dtype='complex128')\n",
"print(f\"\\nz1 := np.arange(10,dtype='complex128'):\\n {z1}\\n\")\n",
"print(f\" z1.dtype:'{z1.dtype}'\")\n",
"print(f\" z1.size :'{z1.size}'\")\n",
"print(f\" z1.itemsize:'{z1.itemsize}'\")\n",
"print(f\" z1.nbytes:'{z1.nbytes}'\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" key:int value:[, , , ]\n",
" key:uint value:[, , , ]\n",
" key:float value:[, , , ]\n",
" key:complex value:[, , ]\n",
" key:others value:[, , , , ]\n"
]
}
],
"source": [
"# Native scalar data types in numpy:\n",
"for key in np.sctypes:\n",
" print(f\" key:{key:<8s} value:{np.sctypes[key]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See also:
\n",
"https://docs.scipy.org/doc/numpy/user/basics.types.html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The class np.dtype allows one to create compound data types \n",
"(structured arrays cfr. C structs).
\n",
"For more info:
\n",
"https://docs.scipy.org/doc/numpy/user/basics.rec.html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To perform a data type conversion (cast) between different scalar data types:\n",
"* numpy.astype(dtype) : convert array to type dtype"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"f1 := np.array([1.0,2.5,3.0,7.2]):\n",
" [1. 2.5 3. 7.2]\n",
" i3 := f1.astype(dtype='complex128'):\n",
" [1. +0.j 2.5+0.j 3. +0.j 7.2+0.j]\n",
"\n",
" i3.dtype:'complex128'\n",
" i3.size :'4'\n",
" i3.itemsize:'16'\n",
" i3.nbytes:'64'\n"
]
}
],
"source": [
"# Change dtype of an array \n",
"f1 = np.array([1.0,2.5,3.0,7.2])\n",
"print(f\"\\nf1 := np.array([1.0,2.5,3.0,7.2]):\\n {f1}\")\n",
"i3 = f1.astype('complex128')\n",
"print(f\" i3 := f1.astype(dtype='complex128'):\\n {i3}\\n\")\n",
"print(f\" i3.dtype:'{i3.dtype}'\")\n",
"print(f\" i3.size :'{i3.size}'\")\n",
"print(f\" i3.itemsize:'{i3.itemsize}'\")\n",
"print(f\" i3.nbytes:'{i3.nbytes}'\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### b. Particular forms of ndarray"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy has specific initialization functions
\n",
"A few of the most important ones:\n",
"* diag(v,k=0) \n",
" + either **extracts** the diagonal (if a matrix exists)\n",
" + or **constructs** a diagonal array. \n",
"* empty(shape,dtype='float64',order='C') \n",
" + returns a new array without initializing its entries (i.e. mem. allocation)\n",
"* eye(N,M=None,k=0,dtype='float64') \n",
" + returns a 2-D array with ones on the 'diagonal' and zeros elsewhere\n",
"* fromfunction(myfunc,shape,dtype) \n",
" + returns a new array based on a function\n",
"* identity(n,dtype='float64') \n",
" + returns the $n$ x $n$ identity array\n",
"* ones(shape,dtype='float64',order='C') \n",
" + returns a new array completely filled with 1s \n",
"* zeros(shape,dtype='float64',order='C') \n",
" + returns a new array completely filled with 0s"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" a1:\n",
"[[ 0 1 2 3 4]\n",
" [ 5 6 7 8 9]\n",
" [10 11 12 13 14]\n",
" [15 16 17 18 19]]\n",
" \n",
" np.diag(a1):\n",
"[ 0 6 12 18]\n",
" \n",
" np.diag(a1,k=1):\n",
"[ 1 7 13 19]\n",
" \n",
" np.diag(range(4)):\n",
"[[0 0 0 0]\n",
" [0 1 0 0]\n",
" [0 0 2 0]\n",
" [0 0 0 3]]\n",
" \n",
" np.diag(range(4),k=1):\n",
"[[0 0 0 0 0]\n",
" [0 0 1 0 0]\n",
" [0 0 0 2 0]\n",
" [0 0 0 0 3]\n",
" [0 0 0 0 0]]\n"
]
}
],
"source": [
"# Diag function\n",
"a1 = np.array([i for i in range(20)]).reshape(4,5)\n",
"print(f\" a1:\\n{a1}\")\n",
"\n",
"# Extract the diagonal\n",
"print(f\" \\n np.diag(a1):\\n{np.diag(a1)}\")\n",
"\n",
"# Extract a SHIFTED diagonal\n",
"print(f\" \\n np.diag(a1,k=1):\\n{np.diag(a1,k=1)}\")\n",
"\n",
"# Create a diagonal matrix\n",
"print(f\" \\n np.diag(range(4)):\\n{np.diag(range(4))}\")\n",
"\n",
"# Create a SHIFTED diagonal matrix\n",
"print(f\" \\n np.diag(range(4),k=1):\\n{np.diag(range(4),k=1)}\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a2=np.empty((2,3)) :\n",
"[[4.67277158e-310 0.00000000e+000 0.00000000e+000]\n",
" [0.00000000e+000 0.00000000e+000 0.00000000e+000]]\n",
"\n",
"a3=np.eye(5,4,k=1,dtype='float64') :\n",
"[[0. 1. 0. 0.]\n",
" [0. 0. 1. 0.]\n",
" [0. 0. 0. 1.]\n",
" [0. 0. 0. 0.]\n",
" [0. 0. 0. 0.]]\n",
"\n",
"a4=np.fromfunction(lambda x,y: x+2*y,(2,5),dtype='float64') :\n",
"[[0. 2. 4. 6. 8.]\n",
" [1. 3. 5. 7. 9.]]\n",
"\n",
"a10:[[0. 2. 4. 6. 8.]\n",
" [1. 3. 5. 7. 9.]]\n",
"a5=np.identity(5,dtype='int64') :\n",
"[[1 0 0 0 0]\n",
" [0 1 0 0 0]\n",
" [0 0 1 0 0]\n",
" [0 0 0 1 0]\n",
" [0 0 0 0 1]]\n",
"\n",
"a6=np.ones((2,7),dtype='int64') :\n",
"[[1 1 1 1 1 1 1]\n",
" [1 1 1 1 1 1 1]]\n",
"\n",
"a7=np.zeros((3,4),dtype='complex128') :\n",
"[[0.+0.j 0.+0.j 0.+0.j 0.+0.j]\n",
" [0.+0.j 0.+0.j 0.+0.j 0.+0.j]\n",
" [0.+0.j 0.+0.j 0.+0.j 0.+0.j]]\n",
"\n"
]
}
],
"source": [
"# Memory allocation\n",
"a2=np.empty((2,3))\n",
"print(f\"a2=np.empty((2,3)) :\\n{a2}\\n\")\n",
"\n",
"# Create ones on the diagonal\n",
"a3=np.eye(5,4,k=1)\n",
"print(f\"a3=np.eye(5,4,k=1,dtype='float64') :\\n{a3}\\n\")\n",
"\n",
"# Use a function (index based) to generate entries\n",
"def myfunc(x,y):\n",
" return x+2*y\n",
"\n",
"a4=np.fromfunction(lambda x,y: x+2*y,(2,5),dtype='float64')\n",
"print(f\"a4=np.fromfunction(lambda x,y: x+2*y,(2,5),dtype='float64') :\\n{a4}\\n\")\n",
"\n",
"a10=np.fromfunction(myfunc,(2,5),dtype='float64')\n",
"print(f\"a10:{a10}\")\n",
"\n",
"# Return a SQUARE identity matrix\n",
"a5=np.identity(5,dtype='int64')\n",
"print(f\"a5=np.identity(5,dtype='int64') :\\n{a5}\\n\")\n",
"\n",
"a6=np.ones((2,7),dtype='int64')\n",
"print(f\"a6=np.ones((2,7),dtype='int64') :\\n{a6}\\n\")\n",
"\n",
"a7=np.zeros((3,4),dtype='complex128')\n",
"print(f\"a7=np.zeros((3,4),dtype='complex128') :\\n{a7}\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### c. Some other useful (random) initializations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also fill up your ndarray with random numbers
\n",
"The Numpy random generator is based on the **Mersenne Twister**.
\n",
"https://en.wikipedia.org/wiki/Mersenne_Twister\n",
"* numpy.random.random([size]):
\n",
" Returns random floats in the half-open interval [0.0, 1.0).\n",
"* numpy.random.randint(low, [high=None], [size],[dtype]):
\n",
" Returns random integers from low (inclusive) to high (exclusive).\n",
"* ... "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" a:\n",
"[[3.94606318e-01 8.45238172e-01 3.25814326e-01 3.11498246e-01\n",
" 2.42952454e-01]\n",
" [6.38646025e-01 7.35127033e-01 8.73541195e-01 6.58374658e-01\n",
" 5.52337678e-01]\n",
" [5.85534654e-04 9.65623936e-01 2.86566301e-01 1.88678765e-01\n",
" 1.12736092e-01]\n",
" [3.83845591e-02 4.69818458e-01 2.21441105e-01 8.08761863e-01\n",
" 3.01690511e-01]]\n",
"\n",
" b:\n",
"[[12 6 12 8 7 5]\n",
" [12 9 13 6 5 12]\n",
" [ 8 11 9 12 14 12]\n",
" [ 7 13 12 12 14 11]]\n",
"\n"
]
}
],
"source": [
"import numpy.random as rnd\n",
"a = rnd.random((4,5))\n",
"print(f\" a:\\n{a}\\n\")\n",
" \n",
"b = rnd.randint(5,15,(4,6)) \n",
"print(f\" b:\\n{b}\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Create a 5x5 matrix with the following (staggered) entries:
\n",
" $ \n",
" \\begin{equation*}\n",
" a = \n",
" \\begin{pmatrix}\n",
" 0 & 1 & 2 & 3 & 4 \\\\\n",
" 1 & 2 & 3 & 4 & 5 \\\\\n",
" 2 & 3 & 4 & 5 & 6 \\\\\n",
" 3 & 4 & 5 & 6 & 7 \\\\\n",
" 4 & 5 & 6 & 7 & 8\n",
" \\end{pmatrix}\n",
" \\end{equation*}\n",
" $ \n",
" \n",
" (Hint: use the np.fromfunction) \n",
" \n",
"* Create a 6x6 'tridiagonal' matrix (see https://en.wikipedia.org/wiki/Tridiagonal_matrix)
\n",
" where:\n",
" * the 'tridiagonal' elements are True and \n",
" * the 'non-tridiagonal' elements are False
\n",
" Boolean matrices are key components for fancy indexing (see later)
\n",
" (Hint: 0:False & non-zero:True)\n",
" \n",
" \n",
"* Let P be a 8x8 transition matrix representing a discrete (periodic) Markov Chain:
\n",
" $ \n",
" \\begin{equation*}\n",
" P = \n",
" \\begin{pmatrix}\n",
" 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\\n",
" q & 0 & p & 0 & 0 & 0 & 0 & 0 \\\\\n",
" 0 & q & 0 & p & 0 & 0 & 0 & 0 \\\\\n",
" 0 & 0 & q & 0 & p & 0 & 0 & 0 \\\\\n",
" 0 & 0 & 0 & q & 0 & p & 0 & 0 \\\\\n",
" 0 & 0 & 0 & 0 & q & 0 & p & 0 \\\\\n",
" 0 & 0 & 0 & 0 & 0 & q & 0 & p \\\\\n",
" 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\\\\n",
" \\end{pmatrix}\n",
" \\end{equation*}\n",
" $ \n",
" where $q:=1-p$. \n",
" * Create the matrix $P$ with $p:=0.7$.\n",
" * Generate $P^n$ i.e. where $P^n:= PP \\ldots P$.\n",
" Set $n:=10000$.
\n",
" (Hint: use the function numpy.linalg.matrix_power for the matrix power).
\n",
" Check also $P^n$ when $n=10001,10002$.\n"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ True True False False False False]\n",
" [ True True True False False False]\n",
" [False True True True False False]\n",
" [False False True True True False]\n",
" [False False False True True True]\n",
" [False False False False True True]]\n",
"[1, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7]\n",
"[1, 2, 3, 4]\n"
]
},
{
"data": {
"text/plain": [
"array([[0. , 0.01183409, 0. , 0.06443004, 0. ,\n",
" 0.35078577, 0. , 0.5729501 ],\n",
" [0.00355023, 0. , 0.02761287, 0. , 0.15033676,\n",
" 0. , 0.81850014, 0. ],\n",
" [0. , 0.01183409, 0. , 0.06443004, 0. ,\n",
" 0.35078577, 0. , 0.5729501 ],\n",
" [0.00355023, 0. , 0.02761287, 0. , 0.15033676,\n",
" 0. , 0.81850014, 0. ],\n",
" [0. , 0.01183409, 0. , 0.06443004, 0. ,\n",
" 0.35078577, 0. , 0.5729501 ],\n",
" [0.00355023, 0. , 0.02761287, 0. , 0.15033676,\n",
" 0. , 0.81850014, 0. ],\n",
" [0. , 0.01183409, 0. , 0.06443004, 0. ,\n",
" 0.35078577, 0. , 0.5729501 ],\n",
" [0.00355023, 0. , 0.02761287, 0. , 0.15033676,\n",
" 0. , 0.81850014, 0. ]])"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def myfunc(x,y):\n",
" return x +y\n",
"\n",
"A = np.fromfunction(lambda x,y: x+y,(5,5))\n",
"A\n",
"myfunc(3,4)\n",
"\n",
"B=np.eye(6) + np.eye(6,k=1) + np.eye(6,k=-1)\n",
"print(B.astype('bool'))\n",
"\n",
"\n",
"p = 0.7\n",
"q = 0.3\n",
"lst=[1] + 6*[p]\n",
"print(lst)\n",
"\n",
"a =[1]\n",
"a.extend([2,3,4])\n",
"print(a)\n",
"\n",
"b=[1]\n",
"b\n",
" \n",
"\n",
"C=np.diag([1]+6*[p],k=1)+np.diag(6*[q]+[1],k=-1)\n",
"import numpy.linalg as la\n",
"la.matrix_power(C,10001)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Solutions:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %load ../solutions/ex2.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Addendum:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1. np.empty_like(), np.zeros_like(), np.ones_like() functions:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy allows one to create empty, zeros, ones ndarrays
\n",
"with the SAME shape as an existing ndarrays.
\n",
"(Syntax: y = np.zeros_like(x), etc.)\n",
" * np.empty_like()\n",
" * np.zeros_like()\n",
" * np.ones_like()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.set_printoptions(precision=4)\n",
"a = np.random.random((4,6))\n",
"print(f\" a:\\n{a}\")\n",
"b = np.zeros_like(a,dtype='int32')\n",
"print(f\" b:\\n{b}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2. Caveat:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# To create a proper vector a Python array-type object is required\n",
"a = np.array(5)\n",
"print(f\" a:{a}\")\n",
"print(f\" ndim:{a.ndim}\")\n",
"print(f\" shape:{a.shape}\")\n",
"print(f\" type:{type(a)}\")\n",
"\n",
"# should either be\n",
"b = np.array([5])\n",
"print(f\"\\n b:{b}\")\n",
"print(f\" ndim:{b.ndim}\")\n",
"print(f\" shape:{b.shape}\")\n",
"print(f\" type:{type(b)}\")\n",
"\n",
"# OR\n",
"c = np.atleast_1d(5)\n",
"print(f\"\\n c:{c}\")\n",
"print(f\" ndim:{c.ndim}\")\n",
"print(f\" shape:{c.shape}\")\n",
"print(f\" type:{type(c)}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}