\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Padma (padma-it)} \pdfinfo{ /Title (python-for-data-science-numpy-library.pdf) /Creator (Cheatography) /Author (Padma (padma-it)) /Subject (Python For Data Science - NumPy Library Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{141414} \definecolor{LightBackground}{HTML}{F7F7F7} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Python For Data Science - NumPy Library Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Padma (padma-it)} via \textcolor{DarkBackground}{\uline{cheatography.com/121896/cs/22473/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Padma (padma-it) \\ \uline{cheatography.com/padma-it} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 28th April, 2020.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Numpy 1D Arrays}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Code}} & {\bf{Explanation \& OUTPUT}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} import numpy as np & np is simply an alias \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} array\_1d = np.array({[}2, 4, 5, 6, 7, 9{]}) & Creating a 1-D array using a list.\{\{nl\}\}np.array() takes in a list or a tuple as argument, and converts into an array \tn % Row Count 10 (+ 6) % Row 3 \SetRowColor{white} print(array\_1d) & {[}2 4 5 6 7 9{]} \tn % Row Count 11 (+ 1) % Row 4 \SetRowColor{LightBackground} \seqsplit{print(type(array\_1d))} & \textless{}class 'numpy.ndarray'\textgreater{} \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} np.array({[}iterator, dtype)\{\{nl\}\} np.array({[}1, 2, 3, 4{]}, dtype='float32') & explicitly set the data type\{\{nl\}\}array({[} 1{\bf{.}}, 2{\bf{.}}, 3{\bf{.}}, 4{\bf{.}}{]}, dtype=float32) \tn % Row Count 18 (+ 5) % Row 6 \SetRowColor{LightBackground} array\_from\_list = np.array({[}2, 5, 6, 7{]})\{\{nl\}\} array\_from\_tuple = np.array((4, 5, 8, 9)) & Convert lists or tuples to arrays\{\{nl\}\}np.array(2, 5, 6, 7) will throw an error \tn % Row Count 23 (+ 5) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{17.67cm}}{list\_1 = {[}3, 6, 7, 5{]}\{\{nl\}\} list\_2 = {[}4, 5, 1, 7{]}} \tn % Row Count 24 (+ 1) % Row 8 \SetRowColor{LightBackground} array\_1 = np.array(list\_1)\{\{nl\}\} array\_2 = np.array(list\_2) & Create two 1-D arrays \tn % Row Count 27 (+ 3) % Row 9 \SetRowColor{white} array\_3 = array\_1*array\_2 & Multiple each element of array 1 with array2\{\{nl\}\}{\bf{OUTPUT}}:{[}12, 30, 7, 35{]} \tn % Row Count 31 (+ 4) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{8.635 cm} x{8.635 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{17.67cm}}{\bf\textcolor{white}{Numpy 1D Arrays (cont)}} \tn % Row 10 \SetRowColor{LightBackground} array\_4 = array\_1 ** 2 & {[}9,36,49,25{]} \# no loop required \tn % Row Count 2 (+ 2) % Row 11 \SetRowColor{white} np.array({[}3.14, 4, 2, 3{]})\{\{nl\}\}Unlike Python lists, NumPy is constrained to arrays that all contain the {\bf{same type}} & array({[} 3.14, 4. , 2. , 3. {]})\{\{nl\}\}If types do not match, NumPy will {\bf{upcast if possible}} e.g. {\bf{int upcasted to float}} \tn % Row Count 9 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Multi-Dimensional Arrays}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Syntax and Concepts}} & {\bf{Example Code}} & {\bf{Explanation \& OUTPUT}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} In NumPy, {\bf{dimensions}} are called {\bf{axes}} & array\_2d = np.array({[}{[}2, 3, 4{]}, {[}5, 8, 7{]}{]}) & Creating a 2-D array using two lists \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} axis = 0 refers to the {\bf{rows}}\{\{nl\}\} axis = 1 refers to the {\bf{columns}} & print(array\_2d)\{\{nl\}\}{\bf{Note}} arrays dont have commas unlike lists on printing & {[}{[}2 3 4{]}\{\{nl\}\}~ {[}5 8 7{]}{]} \tn % Row Count 12 (+ 6) % Row 3 \SetRowColor{white} {\bf{np.ones((row\_count,column,count),datatype)\{\{nl\}\}}}Default is float & {\bf{Create array of 1s}}\{\{nl\}\}{\bf{np.ones((5, 3))}}\{\{nl\}\} 2D array of axes(5 x 3 )of ones\{\{nl\}\}\{\{nl\}\}\{\{nl\}\}{\bf{np.ones((5, 3),dtype=int)}} & array({[}{[}1{\bf{.}}, 1., 1.{]},\{\{nl\}\}~~~~~~ {[}1., 1., 1.{]},\{\{nl\}\}~~~~~~ {[}1., 1., 1.{]},\{\{nl\}\}~~~~~~ {[}1., 1., 1.{]},\{\{nl\}\}~~~~~~ {[}1., 1., 1.{]}{]})\{\{nl\}\}array({[}{[}1, 1, 1{]},\{\{nl\}\}~~~~~~ {[}1, 1, 1{]},\{\{nl\}\}~~~~~~ {[}1, 1, 1{]},\{\{nl\}\}~~~~~~ {[}1, 1, 1{]},\{\{nl\}\}~~~~~~{[}1, 1, 1{]}{]}) \tn % Row Count 53 (+ 41) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Multi-Dimensional Arrays (cont)}} \tn % Row 4 \SetRowColor{LightBackground} {\bf{np.zeros((row\_count,column,count),datatype):\{\{nl\}\}}}Default is float & {\bf{Create array of 0s}}\{\{nl\}\}\{\{nl\}\}np.zeros(4, dtype = np.int)\{\{nl\}\}\{\{nl\}\}np.zeros((4,2,2), dtype = np.int) & array({[}0, 0, 0, 0{]})\{\{nl\}\}array({[}{[}{[}0, 0{]},\{\{nl\}\}~~~~~~ {[}0, 0{]}{]},\{\{nl\}\}\{\{nl\}\}~~~~~~ {[}{[}0, 0{]},\{\{nl\}\}~~~~~~ {[}0, 0{]}{]},\{\{nl\}\}\{\{nl\}\}~~~~~~ {[}{[}0, 0{]},\{\{nl\}\}~~~~~~ {[}0, 0{]}{]},\{\{nl\}\}\{\{nl\}\}~~~~~~ {[}{[}0, 0{]},\{\{nl\}\}~~~~~~ {[}0, 0{]}{]}{]}) \tn % Row Count 36 (+ 36) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Multi-Dimensional Arrays (cont)}} \tn % Row 5 \SetRowColor{LightBackground} {\bf{np.random.random():\{\{nl\}\}}}Default is float & {\bf{Create array of random numbers}}\{\{nl\}\}\{\{nl\}\}np.random.random({[}3, 4{]}) & array({[}{[}9.53309987e-01, \seqsplit{7.61005241e-04}, \seqsplit{4.11978739e-01}, 4.54277232e-01{]},\{\{nl\}\}~~ {[}6.87842577e-01, \seqsplit{9.02965509e-01}, \seqsplit{5.32139081e-01}, 5.41951709e-01{]},\{\{nl\}\}~~ {[}8.58188784e-01, \seqsplit{1.11375267e-01}, \seqsplit{8.11638970e-05}, 7.04121020e-01{]}{]}) \tn % Row Count 20 (+ 20) % Row 6 \SetRowColor{white} {\bf{np.arange(start,stop,step,dtype):}}\{\{nl\}\} similar to range\{\{nl\}\}If {\bf{dtype}} is not given, infer the data type from other input arguments & {\bf{Create array with increments of a fixed step size}}\{\{nl\}\}\{\{nl\}\}numbers = np.arange(10, 100, 5) & {[}10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95{]} \tn % Row Count 31 (+ 11) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Multi-Dimensional Arrays (cont)}} \tn % Row 7 \SetRowColor{LightBackground} {\bf{np.linspace(start,stop,number of elements,dtype):}}\{\{nl\}\}Default is {\bf{float}} & {\bf{Create array of fixed \seqsplit{length;Sometimes}, you know the length of the array, not the step size}}\{\{nl\}\}\{\{nl\}\}np.linspace(10, 100, 19,dtype=int) & array({[} 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100{]}) \tn % Row Count 11 (+ 11) % Row 8 \SetRowColor{white} {\bf{np.full((dimensions),element\_to\_be\_filled)\{\{nl\}\}}}Default is {\bf{int}} & Creating a 4 x 3 array of 7s using np.full(); default is {\bf{int}}\{\{nl\}\}np.full((4,3), 7) & array({[}{[}7, 7, 7{]},\{\{nl\}\}~~~~ {[}7, 7, 7{]},\{\{nl\}\}~~~~ {[}7, 7, 7{]},\{\{nl\}\}~~~~ {[}7, 7, 7{]}{]}) \tn % Row Count 24 (+ 13) % Row 9 \SetRowColor{LightBackground} {\bf{np.tile(arr,repeat\_count)}}\{\{nl\}\}Default is {\bf{int}} & creates a new array by repeating the given array for the given repeated count\{\{nl\}\}arr = ({[}0, 1, 2{]}) \{\{nl\}\}np.tile(arr, 3)\{\{nl\}\}np.tile(arr, (3,2)) & array({[}0, 1, 2, 0, 1, 2, 0, 1, 2{]})\{\{nl\}\}array({[}{[}0, 1, 2, 0, 1, 2{]},\{\{nl\}\}~~~~ {[}0, 1, 2, 0, 1, 2{]},\{\{nl\}\}~~~~ {[}0, 1, 2, 0, 1, 2{]}{]}) \tn % Row Count 38 (+ 14) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Multi-Dimensional Arrays (cont)}} \tn % Row 10 \SetRowColor{LightBackground} {\bf{np.eye(identity\_matrix\_element,dtype)}}\{\{nl\}\}Default type is {\bf{float}} & Create a 3 x 3 identity matrix\{\{nl\}\}np.eye(3, dtype = int) & array({[}{[}1, 0, 0{]},\{\{nl\}\}~~~~ {[}0, 1, 0{]},\{\{nl\}\}~~~~ {[}0, 0, 1{]}{]}) \tn % Row Count 9 (+ 9) % Row 11 \SetRowColor{white} {\bf{np.random.randint(start,stop,(dimensions))}}\{\{nl\}\}Only integers & \seqsplit{rand\_array=np}.random.randint(0, 10, (4,4)) & array({[}{[}9, 3, 6, 0{]},\{\{nl\}\}~~~~ {[}1, 5, 7, 5{]},\{\{nl\}\}~~~~ {[}4, 2, 6, 4{]},\{\{nl\}\}~~~~ {[}5, 3, 4, 6{]}{]}) \tn % Row Count 23 (+ 14) % Row 12 \SetRowColor{LightBackground} Print the second row & print(rand\_array{[}1, {]}) & {[}1, 5, 7, 5{]} \tn % Row Count 25 (+ 2) % Row 13 \SetRowColor{white} np.empty(3) & Create an \seqsplit{uninitialized} array of three integers & array({[} 1., 1., 1.{]}) {\bf{Could be anything from memory}} \tn % Row Count 30 (+ 5) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Multi-Dimensional Arrays (cont)}} \tn % Row 14 \SetRowColor{LightBackground} & array\_1d = np.arange(10)\{\{nl\}\}print(array\_1d) & {[}0 1 2 3 4 5 6 7 8 9{]} \tn % Row Count 4 (+ 4) % Row 15 \SetRowColor{white} {\bf{Iteration of 1D array is similar to lists}} & for i in array\_1d:\{\{nl\}\}~~ print(i**2) & 0\{\{nl\}\} 1\{\{nl\}\} 4\{\{nl\}\} 9 \tn % Row Count 8 (+ 4) % Row 16 \SetRowColor{LightBackground} {\bf{Iterating on 2-D arrays is done with respect to the first axis (which is row, the second axis is column)}} & for row in array\_2d: print(row) & {[}2 3 4{]}\{\{nl\}\} {[}5 8 7{]} \tn % Row Count 17 (+ 9) % Row 17 \SetRowColor{white} {\bf{3D array}} & array\_3d = \seqsplit{np.arange(24).reshape(2}, 3, 4)\{\{nl\}\} \seqsplit{print(array\_3d)} & {[}{[}{[} 0 1 2 3{]}\{\{nl\}\}~~ {[} 4 5 6 7{]}\{\{nl\}\}~~ {[} 8 9 10 11{]}{]}\{\{nl\}\}\{\{nl\}\} {[}{[}12 13 14 15{]}\{\{nl\}\}~~ {[}16 17 18 19{]}\{\{nl\}\}~~ {[}20 21 22 23{]}{]}{]} \tn % Row Count 32 (+ 15) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Multi-Dimensional Arrays (cont)}} \tn % Row 18 \SetRowColor{LightBackground} {\bf{Iterating over 3-D arrays: Done with respect to the first axis}} & for row in array\_3d:\{\{nl\}\}~~ print(row) & {[}{[}{[} 0 1 2 3{]}\{\{nl\}\}~~ {[} 4 5 6 7{]}\{\{nl\}\}~~ {[} 8 9 10 11{]}{]}\{\{nl\}\} {[}{[}12 13 14 15{]}\{\{nl\}\}~~ {[}16 17 18 19{]}\{\{nl\}\}~~ {[}20 21 22 23{]}{]}{]} \tn % Row Count 14 (+ 14) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{6.5793 cm} x{6.4106 cm} x{3.8801 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Array Attributes}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Syntax and Concepts}} & {\bf{Example Code}} & {\bf{Explanation \& OUTPUT}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{array.shape}}\{\{nl\}\}Returns Shape of array in the form {\bf{(n x m)}} & print("Shape: \{\}".format(rand\_array.shape)) & Shape: (4, 4) \tn % Row Count 8 (+ 5) % Row 2 \SetRowColor{LightBackground} {\bf{array.ndim}}\{\{nl\}\} Returns number of dimensions (or axes) & \seqsplit{print("Dimensions:} \{\}".format(rand\_array.ndim)) & \seqsplit{Dimensions:} 2 \tn % Row Count 12 (+ 4) % Row 3 \SetRowColor{white} {\bf{array.dtype}}\{\{nl\}\}Returns data type (int, float etc.) & print("dtype: \{\}".format(rand\_array.dtype)) & dtype: int32 \tn % Row Count 16 (+ 4) % Row 4 \SetRowColor{LightBackground} {\bf{array.size}}\{\{nl\}\} Returns total number of elements in the array & print("Size: ", \seqsplit{rand\_array.size)} & Size: 16 \tn % Row Count 21 (+ 5) % Row 5 \SetRowColor{white} {\bf{array.itemsize}}\{\{nl\}\}Returns Memory used by each array element in bytes & print("Item size: \{\}".format(rand\_array.itemsize)) & Item size: 4 \tn % Row Count 26 (+ 5) % Row 6 \SetRowColor{LightBackground} {\bf{array.nbytes}}\{\{nl\}\} Returns the total size (in bytes) of the array & \seqsplit{print("nbytes:"}, \seqsplit{rand\_array.nbytes}, "bytes") & nbytes: 64 bytes \tn % Row Count 31 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{7.7602 cm} x{4.3862 cm} x{4.7236 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Array Indexing}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Syntax and Concepts}} & {\bf{Example Code}} & {\bf{Explanation \& OUTPUT}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{Access single elements}}: x{[}start:stop:step{]}\{\{nl\}\}Print third element & print(array\_1d{[}2{]}) & 2 \tn % Row Count 7 (+ 4) % Row 2 \SetRowColor{LightBackground} {\bf{Access single elements}}: x{[}start:stop:step{]}\{\{nl\}\}Print last element & print(array\_1d{[}-1{]}) & 9 \tn % Row Count 11 (+ 4) % Row 3 \SetRowColor{white} {\bf{Access single element in a 2D array}}\{\{nl\}\}Prints second row third column & print(array\_2d{[}1, 2{]}) & 8 \tn % Row Count 16 (+ 5) % Row 4 \SetRowColor{LightBackground} {\bf{Access single element in a 2D array}}\{\{nl\}\}Prints second row last column & print(array\_2d{[}1, -1{]}) & 7 \tn % Row Count 21 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Array Slicing}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Syntax and Concepts}} & {\bf{Example Code}} & {\bf{Explanation \& OUTPUT}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Accessing subarrays}}\{\{nl\}\}Return specific elements; {\bf{Index has to be a list}} & x{[}start:stop:step{]}\{\{nl\}\}print(array\_1d{[}{[}2, 5, 6{]}{]})\{\{nl\}\}print(array\_1d{[}2, 5, 6{]})\{\{nl\}\}{\bf{Default}}~start: 0, stop: size of dimension, step = 1 & {[}2 5 6{]}\{\{nl\}\} using {[}{]} instead of{[}{[}{]}{]} Will throw error \tn % Row Count 14 (+ 12) % Row 2 \SetRowColor{LightBackground} & array\_1d = np.arange(10)\{\{nl\}\}print(array\_1d) & {[}0 1 2 3 4 5 6 7 8 9{]} \tn % Row Count 18 (+ 4) % Row 3 \SetRowColor{white} {\bf{Slice third element onwards}} & print(array\_1d{[}2:{]}) & {[}2 3 4 5 6 7 8 9{]} \tn % Row Count 21 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Slice first three elements}} & print(array\_1d{[}:3{]}) & {[}0 1 2{]} \tn % Row Count 24 (+ 3) % Row 5 \SetRowColor{white} {\bf{Slice third to seventh elements}} & print(array\_1d{[}2:7{]}) & {[}2 3 4 5 6{]} \tn % Row Count 27 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{Subset starting 0 at increment of 2}} & print(array\_1d{[}0::2{]}) & {[}0 2 4 6 8{]} \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Array Slicing (cont)}} \tn % Row 7 \SetRowColor{LightBackground} {\bf{Slicing a 2D array}} & print(array\_2d{[}1, :{]}) & {[}5 8 7{]} \tn % Row Count 2 (+ 2) % Row 8 \SetRowColor{white} {\bf{Slicing a 2D array returns an array}} & print(type(array\_2d{[}1, :{]})) & \textless{}class 'numpy.ndarray'\textgreater{} \tn % Row Count 5 (+ 3) % Row 9 \SetRowColor{LightBackground} {\bf{Slicing all rows and the third column}} & print(array\_2d{[}:, 2{]}) & {[}4 7{]} \tn % Row Count 9 (+ 4) % Row 10 \SetRowColor{white} {\bf{Slicing all rows and the first three columns}} & print(array\_2d{[}:, :3{]}) & {[}{[}2 3 4{]}\{\{nl\}\}~~ {[}5 8 7{]}{]} \tn % Row Count 13 (+ 4) % Row 11 \SetRowColor{LightBackground} {\bf{Slicing elements within range with step size}} & print(array\_1d{[}2:7:2{]}) & {[}2 4 6{]} \tn % Row Count 17 (+ 4) % Row 12 \SetRowColor{white} import numpy as np\{\{nl\}\} arr1 = np.array({[}1,2,3,4{]})\{\{nl\}\} print(arr1)\{\{nl\}\} arr2 = arr1{[}1:{]}\{\{nl\}\} print(arr2)\{\{nl\}\} arr2{[}1{]} = 8\{\{nl\}\} print(arr1)\{\{nl\}\} print(arr2) & Numpy array on slicing will not return new copy. \{\{nl\}\}Numpy array slicing will only return a view or reference to original array(like shallow copy) & {[}1 2 3 4{]}\{\{nl\}\} {[}2 3 4{]}\{\{nl\}\} {[}1 2 8 4{]}\{\{nl\}\} {[}2 8 4{]} \tn % Row Count 30 (+ 13) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Array Slicing (cont)}} \tn % Row 13 \SetRowColor{LightBackground} list1 = {[}1,2,3,4{]}\{\{nl\}\} print(list1)\{\{nl\}\} list2 = list1{[}1:{]}\{\{nl\}\} print(list2)\{\{nl\}\} list2{[}1{]} = 8\{\{nl\}\} print(list1)\{\{nl\}\} print(list2) & Lists on slicing will create new copy. & {[}1, 2, 3, 4{]}\{\{nl\}\} {[}2, 3, 4{]}\{\{nl\}\} {[}1, 2, 3, 4{]}\{\{nl\}\} {[}2, 8, 4{]} \tn % Row Count 11 (+ 11) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Reshaping of NumPy Arrays}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Syntax and Concepts}} & {\bf{Example Code}} & {\bf{Explanation \& OUTPUT}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{array.reshape(dimensions)}}\{\{nl\}\}Default is {\bf{int}}\{\{nl\}\}x = np.arange(24)\{\{nl\}\} print(x)\{\{nl\}\} gives {[} 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23{]} \{\{nl\}\}\{\{nl\}\}print(x.reshape(3, 2, 4)) & {\bf{3D array created from 1D array using reshape()}}\{\{nl\}\}\{\{nl\}\}{\emph{ The last axis has 4 elements, and is printed from left to right.\{\{nl\}\} }} The second last has 3, and is printed top to bottom\{\{nl\}\} * The other axis has 2, and is printed in the two separated blocks & {[}{[}{[} 0 1 2 3{]}\{\{nl\}\}~~ {[} 4 5 6 7{]}{]}\{\{nl\}\}\{\{nl\}\}~ {[}{[} 8 9 10 11{]}\{\{nl\}\}~~ {[}12 13 14 15{]}{]}\{\{nl\}\}\{\{nl\}\}~ {[}{[}16 17 18 19{]}\{\{nl\}\}~~ {[}20 21 22 23{]}{]}{]} \tn % Row Count 23 (+ 21) % Row 2 \SetRowColor{LightBackground} {\bf{array{[}np.newaxis,:{]}}} creates a row vector\{\{nl\}\}{\bf{array{[}:,np.newaxis{]}}} creates a column vector & print(x{[}np.newaxis,:{]})\{\{nl\}\} print(x{[}:,np.newaxis{]}) & {[}{[} 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23{]}{]}\{\{nl\}\}{[}{[} 0{]}\{\{nl\}\} {[} 1{]}\{\{nl\}\} {[} 2{]}\{\{nl\}\} .... {[} 22{]}\{\{nl\}\} {[} 23{]}{]}\{\{nl\}\} \tn % Row Count 35 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Array Concatenation}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Syntax and Concepts}} & {\bf{Example Code}} & {\bf{Explanation \& OUTPUT}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{np.concatenate({[}array1, 2,...{]},axis=0)}}\{\{nl\}\} Default is along {\bf{x-axis}}\{\{nl\}\}Along x-axis means adding rows(row-wise)(axis=0)\{\{nl\}\}Along y-axis means adding \seqsplit{columns(xolumn-wise)(axis=1)} & x = np.array({[}1, 2, 3{]})\{\{nl\}\} y = np.array({[}3, 2, 1{]})\{\{nl\}\} z = {[}99, 99, 99{]}\{\{nl\}\} print(np.concatenate({[}x, y, z{]})) & The dimensions should match on the axis the arrays are being concatenated.\{\{nl\}\}Here x dimensions are 1 x 3\{\{nl\}\}y dimensions are 1 x 3\{\{nl\}\}z dimensions are 1 x 3\{\{nl\}\} Since columns are same, they can be concatenated across x-axis\{\{nl\}\}{[} 1 2 3 3 2 1 99 99 99{]} \tn % Row Count 23 (+ 21) % Row 2 \SetRowColor{LightBackground} {\bf{Concatenate on 2D arrays with same dimensions}} & grid = np.array({[}{[}1, 2, 3{]},\{\{nl\}\}~~~~~~~~ {[}4, 5, 6{]}{]})\{\{nl\}\} np.concatenate({[}grid, grid{]})\{\{nl\}\} np.concatenate({[}grid, grid{]}, axis=1) & array({[}{[}1, 2, 3{]},\{\{nl\}\}~~~~ {[}4, 5, 6{]},\{\{nl\}\}~~ {[}1, 2, 3{]},\{\{nl\}\}~~ {[}4, 5, 6{]}{]})\{\{nl\}\} array({[}{[}1, 2, 3, 1, 2, 3{]},\{\{nl\}\}~~ {[}4, 5, 6, 4, 5, 6{]}{]}) \tn % Row Count 38 (+ 15) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Array Concatenation (cont)}} \tn % Row 3 \SetRowColor{LightBackground} {\bf{Concatenate on arrays with different dimensions}} & np.vstack({[}arrays{]})\{\{nl\}\}np.hstack({[}arrays{]})\{\{nl\}\}np.dstack({[}arrays{]}) & \tn % Row Count 6 (+ 6) % Row 4 \SetRowColor{white} {\bf{np.vstack({[}array1, array2{]})}}\{\{nl\}\}array1 and 2 should have same column size & print(np.vstack({[}x,grid{]})) & {[}{[}1 2 3{]}\{\{nl\}\}~~ {[}1 2 3{]}\{\{nl\}\}~~ {[}4 5 6{]}{]} \tn % Row Count 12 (+ 6) % Row 5 \SetRowColor{LightBackground} {\bf{np.hstack({[}array1, array2{]})}}\{\{nl\}\}array1 and 2 should have same row size & print(np.hstack({[}x,y{]}))\{\{nl\}\}y = np.array({[}{[}99{]},{[}99{]}{]})\{\{nl\}\} np.hstack({[}grid, y{]}) & {[}1 2 3 3 2 1{]}\{\{nl\}\} {[}{[} 1 2 3 99{]}\{\{nl\}\}~ {[} 4 5 6 99{]}{]} \tn % Row Count 19 (+ 7) % Row 6 \SetRowColor{white} {\bf{np.dstack({[}arrays{]})}} & same as **np.concatenate({[}arrays{]},axis=2) & will stack arrays along the third axis \tn % Row Count 23 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Array Splitting}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Syntax and Concepts}} & {\bf{Example Code}} & {\bf{Explanation \& OUTPUT}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{np.split(array, {[}split indices as list{]})}} & x = {[}1, 2, 3, 99, 99, 3, 2, 1{]}\{\{nl\}\}x1, x2, x3 = np.split(x, {[}3, 5{]})\{\{nl\}\}print(x1, x2, x3) & Splitting is opposite of \seqsplit{Concatenation}. \{\{nl\}\}N slice points mentioned will give N+1 arrays\{\{nl\}\}{\bf{{[}1 2 3{]} {[}99 99{]} {[}3 2 1{]}}} \tn % Row Count 12 (+ 10) % Row 2 \SetRowColor{LightBackground} & grid = np.array({[}1,2,3,4,5,6,7,8,9{]}).reshape((3,3))\{\{nl\}\} print(grid) & \tn % Row Count 18 (+ 6) % Row 3 \SetRowColor{white} {\bf{vsplit}} only works on arrays of 2 or more dimensions & upper, lower = \seqsplit{np.vsplit(grid}, {[}2{]})\{\{nl\}\} print(upper)\{\{nl\}\} print(lower) & {[}{[}1 2 3{]}\{\{nl\}\}~ {[}4 5 6{]}{]}\{\{nl\}\}\{\{nl\}\} {[}{[}7 8 9{]}{]} \tn % Row Count 24 (+ 6) % Row 4 \SetRowColor{LightBackground} & left, middle, right = \seqsplit{np.hsplit(grid}, {[}1,2{]})\{\{nl\}\} print(left)\{\{nl\}\} print(middle)\{\{nl\}\} print(right)\{\{nl\}\} & {[}{[}1{]}\{\{nl\}\}~ {[}4{]}\{\{nl\}\}~ {[}7{]}{]}\{\{nl\}\}\{\{nl\}\} {[}{[}2{]}\{\{nl\}\}~ {[}5{]}\{\{nl\}\}~ {[}8{]}{]}\{\{nl\}\}\{\{nl\}\} {[}{[}3{]}\{\{nl\}\}~ {[}6{]}\{\{nl\}\}~ {[}9{]}{]} \tn % Row Count 35 (+ 11) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Aggregation functions}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Syntax and Concepts}} & {\bf{Example Code}} & {\bf{Explanation \& OUTPUT}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{1D array}} Aggregate operations & m = np.arange(10)\{\{nl\}\} print(m)\{\{nl\}\} print(sum(m))\{\{nl\}\} print(np.sum(m))\{\{nl\}\} print(np.min(m))\{\{nl\}\} print(np.max(m))\{\{nl\}\} & {[}0 1 2 3 4 5 6 7 8 9{]}\{\{nl\}\} 45\{\{nl\}\} 45\{\{nl\}\} 0\{\{nl\}\} 9\{\{nl\}\} \tn % Row Count 12 (+ 10) % Row 2 \SetRowColor{LightBackground} {\bf{2D array}} aggregate vs normal functions & p = \seqsplit{np.arange(9).reshape(3},3)\{\{nl\}\} print(p)\{\{nl\}\} print(sum(p))\{\{nl\}\} print(np.sum(p))\{\{nl\}\} \seqsplit{print(p.sum())} & {[}{[}0 1 2{]}\{\{nl\}\}~ {[}3 4 5{]}\{\{nl\}\}~ {[}6 7 8{]}{]}\{\{nl\}\} {[} 9 12 15{]}\{\{nl\}\} 36\{\{nl\}\} 36\{\{nl\}\} \tn % Row Count 21 (+ 9) % Row 3 \SetRowColor{white} & print(p.min())\{\{nl\}\} print(p.min(axis=0))\{\{nl\}\} \seqsplit{print(p.min(axis=1))} & 0\{\{nl\}\} {[}0 1 2{]}\{\{nl\}\} {[}0 3 6{]} \tn % Row Count 27 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{3.7114 cm} x{4.3862 cm} x{8.7724 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{NumPy Inbuilt Aggregate Functions}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Function Name}} & {\bf{NaN-safe Version}} & {\bf{Description}} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{3}{x{17.67cm}}{Most aggregates have a NaN-safe counterpart that computes the result while ignoring missing values,} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} np.sum & np.nansum & Compute sum of elements \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} np.prod & \seqsplit{np.nanprod} & Compute product of elements \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} np.mean & \seqsplit{np.nanmean} & Compute median of elements \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} np.std & np.nanstd & Compute standard deviation \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} np.var & np.nanvar & Compute variance \tn % Row Count 14 (+ 1) % Row 7 \SetRowColor{white} np.min & np.nanmin & Find minimum value \tn % Row Count 15 (+ 1) % Row 8 \SetRowColor{LightBackground} np.max & np.nanmax & Find maximum value \tn % Row Count 16 (+ 1) % Row 9 \SetRowColor{white} \seqsplit{np.argmin} & \seqsplit{np.nanargmin} & Find index of minimum value \tn % Row Count 18 (+ 2) % Row 10 \SetRowColor{LightBackground} \seqsplit{np.argmax} & \seqsplit{np.nanargmax} & Find index of maximum value \tn % Row Count 20 (+ 2) % Row 11 \SetRowColor{white} \seqsplit{np.median} & \seqsplit{np.nanmedian} & Compute median of elements \tn % Row Count 22 (+ 2) % Row 12 \SetRowColor{LightBackground} \seqsplit{np.percentile} & \seqsplit{np.nanpercentile} & Compute rank-based statistics of elements \tn % Row Count 25 (+ 3) % Row 13 \SetRowColor{white} np.any & N/A & Evaluate whether any elements are true \tn % Row Count 27 (+ 2) % Row 14 \SetRowColor{LightBackground} np.all & N/A & Evaluate whether all elements are true \tn % Row Count 29 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Mathematical Operations on NumPy Arrays}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Syntax and Concepts}} & {\bf{Example Code}} & {\bf{Explanation \& OUTPUT}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{np.sin(array\_name)}}\{\{nl\}\}{\bf{np.cos(array\_name)}} & a = np.arange(1, 5)\{\{nl\}\} print(np.sin(a))\{\{nl\}\} print(np.cos(a))\{\{nl\}\} print(np.exp(a))\{\{nl\}\} \seqsplit{print(np.log(a))} & {[} 0.84147098 0.90929743 0.14112001 -0.7568025 {]}\{\{nl\}\} {[} 0.54030231 -0.41614684 -0.9899925 -0.65364362{]}\{\{nl\}\} {[} 2.71828183 7.3890561 20.08553692 54.59815003{]}\{\{nl\}\} {[}0. 0.69314718 1.09861229 1.38629436{]} \tn % Row Count 19 (+ 17) % Row 2 \SetRowColor{LightBackground} {\bf{np.vectorize(custom\_function)}} & a = np.arange(5)\{\{nl\}\}\{\{nl\}\} f = \seqsplit{np.vectorize(lambda} x: x+10)\{\{nl\}\} print(f(a)) & {[}10 11 12 13 14{]} \tn % Row Count 26 (+ 7) % Row 3 \SetRowColor{white} {\bf{Custom function on 2D array}}\{\{nl\}\} Previous functions can be reused for 2D arrays too & b = \seqsplit{np.linspace(1}, 100, 10,dtype=int)\{\{nl\}\} print(b)\{\{nl\}\} print(f(b)) & {[} 1 12 23 34 45 56 67 78 89 100{]}\{\{nl\}\} {[} 11 22 33 44 55 66 77 88 99 110{]} \tn % Row Count 33 (+ 7) \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{x{5.7358 cm} x{5.5671 cm} x{5.5671 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{17.67cm}}{\bf\textcolor{white}{Mathematical Operations on NumPy Arrays (cont)}} \tn % Row 4 \SetRowColor{LightBackground} {\bf{np.linalg}} & Applies common linear algebra operations & \tn % Row Count 4 (+ 4) % Row 5 \SetRowColor{white} {\bf{np.linalg.inv(array\_name)}} & returns array of inverse of a matrix & \tn % Row Count 7 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{np.linalg.det(array\_name)}} & returns determinant value of the matrix & \tn % Row Count 10 (+ 3) % Row 7 \SetRowColor{white} {\bf{np.linalg.eig(array\_name)}} & returns eigenvalues and eigenvectors of the matrix & \tn % Row Count 14 (+ 4) % Row 8 \SetRowColor{LightBackground} {\bf{np.dot(array1,array2)}}) & returns matrix \seqsplit{multiplication} & \tn % Row Count 17 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \end{document}