\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{Taissir Boukrouba (taissir2002)} \pdfinfo{ /Title (numpy-cheat-sheet.pdf) /Creator (Cheatography) /Author (Taissir Boukrouba (taissir2002)) /Subject (Numpy 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}{268760} \definecolor{LightBackground}{HTML}{F1F7F5} \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{Numpy Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Taissir Boukrouba (taissir2002)} via \textcolor{DarkBackground}{\uline{cheatography.com/193976/cs/40715/}}} \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}Taissir Boukrouba (taissir2002) \\ \uline{cheatography.com/taissir2002} \\ \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 8th November, 2023.\\ 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{multicols*}{3} \begin{tabularx}{5.377cm}{x{2.68758 cm} x{2.28942 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Creating Arrays}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Create simple array}} & np.array({[}list{]}) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Array filled with zeros}} & np.zeros(size) \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Array filled with ones}} & np.ones(size) \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} {\bf{Array filled with a number}} & np.full(size,number) \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{Array from a range of numbers (last not included)}} & np.arange(start,stop,step) \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} {\bf{Array from a range of numbers (last included)}} & np.linspace(start,stop,count) \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{Array filled with random numbers (float)}} & \seqsplit{np.random.random(size)} \tn % Row Count 17 (+ 3) % Row 7 \SetRowColor{white} {\bf{Array filled with random numbers (int)}} & \seqsplit{np.random.randint(start},stop,size) \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} {\bf{Create identity Matrix}} & np.eye(k) \tn % Row Count 21 (+ 2) % Row 9 \SetRowColor{white} {\bf{Create empty Matrix}} & np.empty(k) \tn % Row Count 23 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Size can be the following : \newline {\bf{ 1) one-dimensional (Array)}} : where size = k = (k) \newline {\emph{eg : np.zeros(2) , creates an array of 2 elements}} \newline {\bf{2) two-dimensional (Matrix)}} : where size = (a,b) \newline {\emph{eg : np.zeros((2,3)) , creates a matrix of 2 rows and one column}} \newline {\bf{ 3) n-dimensional }} : where size = (a,b,c....etc) \textgreater{} 2 \newline {\emph{eg : np.zeros((2,3,5)) , creates a 3 dimensional array}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.55618 cm} x{1.51041 cm} x{1.51041 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Reading Array From Text File}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Inspecting the file (without reading it)}} & !head file\_source & !head \seqsplit{data/array\_float}.csv \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} {\bf{Loading File Into Array}} & \seqsplit{np.loadtxt(source}, delimiter, dtype) & B = \seqsplit{np.loadtxt("data/array\_int.csv"}, delimiter = ",", dtype=int) \tn % Row Count 9 (+ 5) % Row 2 \SetRowColor{LightBackground} {\bf{Loading Array Into File}} & \seqsplit{np.savetxt(source}, array, fmt ,delimiter) & \seqsplit{np.savetxt("data/my\_array.csv"}, B, fmt="\%3i", delimiter=",") \tn % Row Count 14 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\emph{Make sure that you are typing the right datatype of the text file or you will receive an error}} \newline \newline The {\bf{fmt}} keyword is used to specify the spacing in our text file where : \newline - `fmt = "\%3i"` means 3 spaces between row elements (numbers are integers) \newline - `fmt = "\%5.1i"` means 5 spaces between row elements and only one number after floating point} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.68298 cm} x{1.29402 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Numpy Array Attributes}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Returns number of dimensions}} & \seqsplit{array.ndim} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Returns size of each dimension}} & \seqsplit{array.shape} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Returns number of elements}} & \seqsplit{array.size} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} {\bf{Returns type of elements}} & \seqsplit{array.dtype} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{Returns size of each element in bytes}} & \seqsplit{array.itemsize} \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} {\bf{Returns size of all elements in bytes}} & \seqsplit{array.nbytes} \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.60195 cm} x{1.69349 cm} x{1.28156 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Array Indexing}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Accessing array element}} & array\_name{[}index{]} & x1{[}5{]} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Modify array element}} & array\_name{[}index{]} = new\_value & x1{[}5{]} = 69 \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{Accessing matrix element}} & matrix\_name{[}row,column{]} & x2{[}2, -1{]} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} {\bf{Modify matrix element}} & matrix\_name{[}row,column{]} = new\_value & x2{[}2, -1{]} = -5 \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Accessing sub-arrays}} & array\_name{[}start,stop,step=1{]} & x{[}1:5{]} \{\{nl\}\} x{[}1:5:3{]} \tn % Row Count 13 (+ 3) % Row 5 \SetRowColor{white} {\bf{Accessing multi-dim arrays}} & array\_name{[}row\_range,column\_range{]} & x2{[}::-1, ::-1{]} \tn % Row Count 16 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{Creating subarrays as no-copy views}} & sub\_array = original\_array{[}slice{]} & x2\_sub = x2{[}:2, :2{]} \tn % Row Count 19 (+ 3) % Row 7 \SetRowColor{white} {\bf{Creating subarrays as copy views}} & sub\_array = original\_array{[}slice{]}.copy() & \seqsplit{x2\_sub\_copy} = x2{[}:2, :2{]}.copy() \tn % Row Count 22 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{We can access multiple matrix rows and columns when both row and columns are lists : \newline \newline x2{[}{[}1,2{]},{[}1,1{]}{]} -\textgreater{} this means access \# Second and Third row ({[}1,2{]}) , Second and Second column} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.96811 cm} x{1.46464 cm} x{1.14425 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Reshaping of Arrays}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Reshape array}} & \seqsplit{array.reshape((rows},columns)) & x2 = \seqsplit{x1.reshape((3}, 3)) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{Reshape into row vector}} & \seqsplit{array.reshape((1},columns)) & x2 = \seqsplit{x1.reshape((1}, 3)) \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{Reshape into row vector using np.newaxis}} & array{[}np.newaxis, :{]} & x2 = x1{[}np.newaxis, :{]} \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} {\bf{Reshape into column vector}} & \seqsplit{array.reshape((rows},1)) & x2 = \seqsplit{x1.reshape((3}, 1)) \tn % Row Count 12 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Reshape into column vector using np.newaxis}} & array{[}:, np.newaxis{]} & x2 = x1{[}:, np.newaxis{]} \tn % Row Count 15 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\bf{It is very important to note that number of rows multiplied by the number of columns must be equal to the number of array elements}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.60195 cm} x{1.60195 cm} x{1.3731 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Array Concatenation and Splitting}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Concatenate Arrays vertically (default)}} & np.concatenate({[}x1,x2,x3....{]}) & np.concatenate({[}x, y{]}) \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} {\bf{Concatenate Arrays horizontally}} & np.concatenate({[}x1,x2,x3....{]} , axis=1) & np.concatenate({[}x, y{]}, axis=1) \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{Stack Arrays vertically}} & np.vstack({[}x1, x2,x3....{]}) & np.vstack({[}x, y{]}) \tn % Row Count 9 (+ 2) % Row 3 \SetRowColor{white} {\bf{Stack Arrays horizontally}} & np.hstack({[}x1, x2,x3....{]}) & np.hstack({[}x, y{]}) \tn % Row Count 12 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Spliting Arrays}} & \seqsplit{np.split(array}, {[}first\_split\_point, \seqsplit{second\_split\_point} , \seqsplit{third\_split\_point}....{]}) & x1, x2, x3 = np.split(x, {[}3, 5{]}) \tn % Row Count 18 (+ 6) % Row 5 \SetRowColor{white} {\bf{Splitting Matrixes vertically}} & \seqsplit{np.vsplit(matrix}, {[}frist\_row\_index,second\_row\_index....{]}) & upper, lower = \seqsplit{np.vsplit(grid}, {[}2{]}) \tn % Row Count 23 (+ 5) % Row 6 \SetRowColor{LightBackground} {\bf{Splitting Matrixes horizontally}} & \seqsplit{np.hsplit(matrix}, {[}frist\_col\_index,second\_col\_index....{]}) & left, right = \seqsplit{np.hsplit(grid}, {[}2{]}) \tn % Row Count 28 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\bf{NOTE :}} \newline {\bf{concatenate or stack vertically :}} must have same number of columns \newline {\bf{concatenate or stack horizontally :}} must have same number of rows \newline \newline {\bf{NOTE 2 :}} \newline The number of split points when splitting an array depends on the number of arrays generated where : \newline {\emph{Number of split points = number of arrays outputted - 1}} \newline \newline `x1, x2, x3 = np.split(x, {[}3, 5{]})` -\textgreater{} split on the third element to x1 , split on the fifth element to x2 (from forth element) , give the rest to x3} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.46464 cm} x{1.19002 cm} x{1.92234 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Numpy Operations \& UFuncs}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Addition}} & Array + number & x = {[}0 1 2 3{]}\{\{nl\}\} x+5 \{\{nl\}\} {\emph{output : {[}5 6 7 8{]}}} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} {\bf{Substraction}} & Array - number & x = {[}5 6 7 8{]}\{\{nl\}\} x-5 \{\{nl\}\} {\emph{output : {[}0 1 2 3{]}}} \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} {\bf{Multiplication}} & Array * number & x*2 \{\{nl\}\} {\emph{output : {[}0 2 4 6{]}}} \tn % Row Count 10 (+ 2) % Row 3 \SetRowColor{white} {\bf{Division}} & Array / number & x/2 \{\{nl\}\} {\emph{output : {[}0,0.5,1,1.5{]}}} \tn % Row Count 13 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Floor Division}} & Array // number & x//2 \{\{nl\}\} {\emph{output : {[}0,0,1,1{]}}} \tn % Row Count 15 (+ 2) % Row 5 \SetRowColor{white} {\bf{Exponential}} & Array ** number & x{\emph{*2 \{\{nl\}\} }}output : {[}0 1 4 9{]}* \tn % Row Count 17 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{Modulus}} & Array \% number & x \% 2 \{\{nl\}\} {\emph{output : {[}0 1 0 1{]}}} \tn % Row Count 20 (+ 3) % Row 7 \SetRowColor{white} {\bf{Dot Multiplication}} & matrix1 @ matrix2 & check the notes \tn % Row Count 22 (+ 2) % Row 8 \SetRowColor{LightBackground} {\bf{UFunc Addition}} & \seqsplit{np.add(array},number) & np.add(x,5)\{\{nl\}\} {\emph{output : {[}5 6 7 8{]}}} \tn % Row Count 25 (+ 3) % Row 9 \SetRowColor{white} {\bf{UFunc Substraction}} & \seqsplit{np.subtract(array},number) & np.subtract(x,5)\{\{nl\}\} {\emph{output : {[}0 1 2 3{]}}} \tn % Row Count 28 (+ 3) % Row 10 \SetRowColor{LightBackground} {\bf{UFunc Multiplication}} & \seqsplit{np.multiply(array},number) & np.multiply(x,2)\{\{nl\}\} {\emph{output : {[}0 2 4 6{]}}} \tn % Row Count 31 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.46464 cm} x{1.19002 cm} x{1.92234 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Numpy Operations \& UFuncs (cont)}} \tn % Row 11 \SetRowColor{LightBackground} {\bf{UFunc Dot}} & \seqsplit{np.dot(matrix1},matrix2) & check the notes \tn % Row Count 3 (+ 3) % Row 12 \SetRowColor{white} {\bf{UFunc Absolute Value}} & np.abs(x) & x = np.array({[}-2, -1, 0, 1, 2{]})\{\{nl\}\}np.abs(x)\{\{nl\}\}{\emph{output : {[}2,1,0,1,2{]}}} \tn % Row Count 8 (+ 5) % Row 13 \SetRowColor{LightBackground} {\bf{UFunc Sinus}} & np.sin(x) & \seqsplit{self-explanatory} \tn % Row Count 10 (+ 2) % Row 14 \SetRowColor{white} {\bf{UFunc Cosinus}} & np.cos(x) & \seqsplit{self-explanatory} \tn % Row Count 12 (+ 2) % Row 15 \SetRowColor{LightBackground} {\bf{UFunc Tangent}} & np.tan(x) & \seqsplit{self-explanatory} \tn % Row Count 14 (+ 2) % Row 16 \SetRowColor{white} {\bf{UFunc Arcsinus (inverse)}} & \seqsplit{np.arcsin(x)} & \seqsplit{self-explanatory} \tn % Row Count 17 (+ 3) % Row 17 \SetRowColor{LightBackground} {\bf{UFunc Arccosinus (inverse)}} & \seqsplit{np.arccos(x)} & \seqsplit{self-explanatory} \tn % Row Count 20 (+ 3) % Row 18 \SetRowColor{white} {\bf{UFunc Arctangent (inverse)}} & \seqsplit{np.arctan(x)} & \seqsplit{self-explanatory} \tn % Row Count 23 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\emph{For matrix dot operation, the number of columns in the first matrix (array) must be equal to the number of rows in the second matrix (array)}}.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.19002 cm} x{1.69349 cm} x{1.69349 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Aggregates Types}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{array.ufunc.reduce()}} & This aggregate help apply the universal function between elements and returns (reduces) the result toone value (last value) & x = np.arange(1, 6)\{\{nl\}\}np.add.reduce(x)\{\{nl\}\} result : 15 \tn % Row Count 9 (+ 9) % Row 1 \SetRowColor{white} {\bf{array.ufunc.accumulate()}} & This aggregate help apply the universal function between elements but returns the result of each element inside a list & x = np.arange(1, 6)\{\{nl\}\}np.add.accumulate(x)\{\{nl\}\} result : array({[} 1, 3, 6, 10, 15{]}) \tn % Row Count 18 (+ 9) % Row 2 \SetRowColor{LightBackground} {\bf{array.ufunc.outer()}} & This aggregate help apply the ufunc operation to all pairs (a, b) with a in A and b in B. & np.multiply.outer({[}1, 2, 3{]}, {[}4, 5, 6{]})\{\{nl\}\} result : array({[}{[} 4, 5, 6{]},{[} 8, 10, 12{]},{[}12, 15, 18{]}{]}) \tn % Row Count 26 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{The aggregate for multiplication is : {\bf{array.multiply.reduce()}} \newline The aggregate for subtraction is : {\bf{array.subtract.reduce()}} \newline The aggregate for division is : {\bf{array.divide.reduce()}} \newline The aggregate for addition {\bf{array.add.reduce()}} is equivalent to {\bf{np.sum(array)}} \newline The aggregate for multiplication {\bf{array.multiply.reduce()}} is equivalent to {\bf{np.prod(array)}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.05271 cm} x{1.00694 cm} x{2.51735 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Other Aggregate Function}} \tn % Row 0 \SetRowColor{LightBackground} np.sum & \seqsplit{np.nansum} & Compute sum of elements \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} np.prod & \seqsplit{np.nanprod} & Compute product of elements \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} np.mean & \seqsplit{np.nanmean} & Compute mean of elements \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} np.std & \seqsplit{np.nanstd} & Compute standard deviation \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} np.var & \seqsplit{np.nanvar} & Compute variance \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} np.min & \seqsplit{np.nanmin} & Find minimum value \tn % Row Count 12 (+ 2) % Row 6 \SetRowColor{LightBackground} np.max & \seqsplit{np.nanmax} & Find maximum value \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} \seqsplit{np.argmin} & \seqsplit{np.nanargmin} & Find index of minimum value \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} \seqsplit{np.argmax} & \seqsplit{np.nanargmax} & Find index of maximum value \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{np.median} & \seqsplit{np.nanmedian} & Compute median of elements \tn % Row Count 20 (+ 2) % Row 10 \SetRowColor{LightBackground} \seqsplit{np.percentile} & \seqsplit{np.nanpercentile} & Compute rank-based statistics of elements \tn % Row Count 22 (+ 2) % Row 11 \SetRowColor{white} \seqsplit{np.any(condition)} & N/A & Evaluate whether any elements are true \tn % Row Count 24 (+ 2) % Row 12 \SetRowColor{LightBackground} \seqsplit{np.all(condition)} & N/A & Evaluate whether all elements are true \tn % Row Count 26 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Python Broadcasting}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{Broadcasting}} allows you to perform operations on arrays with different shapes in a way that makes sense, without the need to explicitly reshape or replicate the arrays \newline % Row Count 4 (+ 4) {\bf{Rules of Broadcasting :}} \newline % Row Count 5 (+ 1) Not any operation is permissible when both of arrays have different shapes where : \newline % Row Count 7 (+ 2) {\bf{Rule 01 - Padded Dimensions : }} \newline % Row Count 8 (+ 1) when two have different dimensions we can always add a padding dimension to the left , meaning pushing the first one to the right (5,) =\textgreater{} (1,5) ( adding new axis ) \newline % Row Count 12 (+ 4) {\bf{Rule 02 - Stretching Dimensions : }} \newline % Row Count 13 (+ 1) if any of the aligned dimensions is 1 , then we can stretch that dimension to match the other one \newline % Row Count 15 (+ 2) A.shape = (2,3) B.shape = (1,3) =\textgreater{} we can stretch B to make it (2,3) \newline % Row Count 17 (+ 2) {\bf{Example :}} \newline % Row Count 18 (+ 1) A.shape = (3, 1) \newline % Row Count 19 (+ 1) B.shape = (3,) \newline % Row Count 20 (+ 1) We can padd B =\textgreater{} B.shape = (1,3) \newline % Row Count 21 (+ 1) This means we can strech both A and B (Both have 1 as aligned dimension) where : \newline % Row Count 23 (+ 2) A.shape = (3, 1) =\textgreater{} (3,3) \newline % Row Count 24 (+ 1) B.shape = (1,3) =\textgreater{} (3,3)% Row Count 25 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{if any of these rules doesn't apply then broadcasting is not possible}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.55618 cm} x{1.51041 cm} x{1.51041 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Numpy Fancy Indexing}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Select Multiple Elements Using Fancy Indexing}} & \seqsplit{select\_elements} = array{[}list\_of\_indexes{]} & \seqsplit{select\_elements} = array1{[}{[}1, 2, 5, 7{]}{]} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} {\bf{Fancy Indexing for Sorting NumPy Array}} & \seqsplit{sorted\_array} = array{[}np.argsort(array){]} & np.argsort retruns indexes of array sorted (to make it reverse we just write np.argsort(- array)) \tn % Row Count 12 (+ 8) % Row 2 \SetRowColor{LightBackground} {\bf{Fancy Indexing to Assign New Values to Specific Elements}} & array{[}list\_of\_indices{]} = new\_values & array1{[}{[}1, 3, 6{]}{]} = {[}10, 20, 30{]} \tn % Row Count 17 (+ 5) % Row 3 \SetRowColor{white} {\bf{Fancy Indexing on N-d Arrays}} & new\_array = array1{[}list\_of\_row\_indices, list\_of\_col\_indices{]} & \seqsplit{selected\_rows} = array1{[}{[}0,2{]}, :{]} \tn % Row Count 22 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{{\bf{In fancy indexing we can either put a list of indices or numpy array of indices}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.46464 cm} x{1.46464 cm} x{1.64772 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Numpy Sorting}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Items sort 1}} & \seqsplit{np.sort(array)} & by default it sorts the values in ascending order \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} {\bf{Items sort 2}} & \seqsplit{array.sort()} & this one changes the content of the original array \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} {\bf{Index Sorting}} & \seqsplit{np.argsort(array)} & this returns the order of the indexes sorted ascendingly \tn % Row Count 12 (+ 4) % Row 3 \SetRowColor{white} {\bf{Item Sorting using Index sorting}} & array{[}np.argsort(array){]} & this returns the items sorted ascendingly \tn % Row Count 15 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Matrix Sorting}} & \seqsplit{np.sort(matrix},axis=0/1) & this sorts either each matrix columns (axis=0) or each matrix row (axis=1) \tn % Row Count 21 (+ 6) % Row 5 \SetRowColor{white} {\bf{Partitioning (Partial Sorting)}} & \seqsplit{np.partition(array}, n) & this takes the smallest n numbers to the left in no particular order (3 numbers if n = 3) \tn % Row Count 28 (+ 7) % Row 6 \SetRowColor{LightBackground} {\bf{Matrix Partitioning}} & \seqsplit{np.partition(array}, n, axis=0/1) & this takes the smallest n numbers (of each row or column) to the left in no particular order \tn % Row Count 35 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.46464 cm} x{1.46464 cm} x{1.64772 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Numpy Sorting (cont)}} \tn % Row 7 \SetRowColor{LightBackground} {\bf{Index Partitioning}} & \seqsplit{np.argpartition(array}, n, axis=0/1) & this is like argsort() but for matrixes \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.46464 cm} x{1.55618 cm} x{1.55618 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Array as a Data Structure}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Empty Structured Arrays}} & \seqsplit{np.zeros(struct\_size}, dtype=\{'names':(col1,col2,col3), \seqsplit{'formats':(form1}, form1, form1)\}) & data = np.zeros(4, dtype=\{'names':('name ', 'age', 'weight'), \seqsplit{'formats':('U10'}, 'i4', 'f8')\}) \{\{nl\}\} \#output : \{\{nl\}\} {[}('', 0, 0.) ('', 0, 0.) ('', 0, 0.) ('', 0, 0.){]} \tn % Row Count 13 (+ 13) % Row 1 \SetRowColor{white} {\bf{Adding Items to Structured Arrays}} & \# List should be same size as the array structure\{\{nl\}\}struc\_array{[}colname{]} = list & data{[}'name'{]} = name4\{\{nl\}\}data{[}'age'{]} = age4\{\{nl\}\}data{[}'weight'{]} = weight4\{\{nl\}\}\#output:\{\{nl\}\}{[}('Alice', 25, 55. ) ('Bob', 45, 85.5) ('Cathy', 37, 68. ) ('Doug', 19, 61.5){]} \tn % Row Count 27 (+ 14) % Row 2 \SetRowColor{LightBackground} {\bf{Accessing array strucure columns}} & struct\_array{[}colname{]} & data{[}'name'{]} \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.46464 cm} x{1.55618 cm} x{1.55618 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{Array as a Data Structure (cont)}} \tn % Row 3 \SetRowColor{LightBackground} {\bf{Accessing array strucure rows}} & struct\_array{[}index{]} & \# Accessing last row \{\{nl\}\} data{[}-1{]} \tn % Row Count 3 (+ 3) % Row 4 \SetRowColor{white} {\bf{Sorting array strucure}} & \seqsplit{np.sort(struct\_array}, order={[}coumns{]}) & np.sort(data, order={[}'age', 'name'{]}) \tn % Row Count 6 (+ 3) % Row 5 \SetRowColor{LightBackground} {\bf{Partitioning Structured Arrays}} & \seqsplit{np.partition(struct\_array}, n, order={[}columns{]}) & \# getting the smallest 2 weights to the left \{\{nl\}\}np.partition(data, 2, order={[}'weight'{]})\{\{nl\}\}array({[}('Alice', 25, 55. ), ('Doug', 19, 61.5), ('Cathy', 37, 68. ), ('Bob', 45, 85.5){]} \tn % Row Count 21 (+ 15) % Row 6 \SetRowColor{white} {\bf{Defining a new data type}} & np.dtype({[}tuple(col\_name,type){]}) & np.dtype({[}('name', 'U10'), ('age', 'i4'), ('weight', 'f8'){]}) \tn % Row Count 26 (+ 5) % Row 7 \SetRowColor{LightBackground} {\bf{Appending new row}} & \seqsplit{np.append(struct\_array}, numpy\_array{]}, dtype=dt)) & \seqsplit{np.append(data}, np.array({[}('John',64,89){]}, dtype=dt))\{\{nl\}\}{[}('Alice', 25, 55. ) ('Bob', 45, 85.5) ('Cathy', 37, 68. ) ('Doug', 19, 61.5) ('John', 64, 89. ){]} \tn % Row Count 38 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}---} \SetRowColor{LightBackground} \mymulticolumn{3}{x{5.377cm}}{The Format For Empty Structured Arrays are as follows : \newline - {\bf{Integer}} : np.int32 or 'i4' \newline - {\bf{Float}} : np.float32 or 'f8' \newline - {\bf{String}} : np.str\_ or 'U10'} \tn \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}