\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{datamansam} \pdfinfo{ /Title (numpy-crib.pdf) /Creator (Cheatography) /Author (datamansam) /Subject (Numpy Crib 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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 Crib Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{datamansam} via \textcolor{DarkBackground}{\uline{cheatography.com/139410/cs/29575/}}} \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}datamansam \\ \uline{cheatography.com/datamansam} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 23rd November, 2021.\\ Updated 23rd November, 2021.\\ 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.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Numpy - Single dimensional arrays}} \tn % Row 0 \SetRowColor{LightBackground} Creating an array from a list & x = np.array({[}'a', 'b', '9', '8'{]} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Array Data Types & Consist of integers, floating-point numbers, or strings. Data type must be consistent. Numpy's record array gives mixed DTypes \tn % Row Count 9 (+ 7) % Row 2 \SetRowColor{LightBackground} Find the length on an array & np.len(x) \tn % Row Count 11 (+ 2) % Row 3 \SetRowColor{white} Accessing elements from an array & x{[}index\_num{]} {\emph{x{[}1{]}}} \tn % Row Count 13 (+ 2) % Row 4 \SetRowColor{LightBackground} Assign elements from index & {\emph{x{[}1{]}=c}} \tn % Row Count 15 (+ 2) % Row 5 \SetRowColor{white} Slicing x{[}start:end{]}) & print(x{[}1:2{]}) {\emph{{[}'b', '9'{]}}} \tn % Row Count 17 (+ 2) % Row 6 \SetRowColor{LightBackground} Slicing x{[}start:end: step{]}) & print(x{[}1:3:2{]}) {\emph{{[}'b', '8'{]}}} \tn % Row Count 19 (+ 2) % Row 7 \SetRowColor{white} Modify a new version of an array without changing the original & y = x.copy() \tn % Row Count 23 (+ 4) % Row 8 \SetRowColor{LightBackground} Negative slices, single value x{[}-Distance from end{]} & x{[}-2{]} {\emph{b}} \tn % Row Count 26 (+ 3) % Row 9 \SetRowColor{white} Negative slices, reversal x{[}start:end: -step{]}) & x{[}3:0:-2{]}) {\emph{8, b}} \tn % Row Count 29 (+ 3) % Row 10 \SetRowColor{LightBackground} Adding to an array & x.append('7') \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Numpy - Single dimensional arrays (cont)}} \tn % Row 11 \SetRowColor{LightBackground} Saving to binary file & \seqsplit{np.save(open('data.npy'}, 'wb'), data) \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Useful Numpy Functions}} \tn % Row 0 \SetRowColor{LightBackground} Array Creation: & arange, array, copy, empty, empty\_like, eye, fromfile, fromfunction, identity, linspace, logspace, mgrid, ogrid, ones, ones\_like, r\_, zeros, zeros\_like \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} Conversions & ndarray.astype, atleast\_1d, atleast\_2d, atleast\_3d, mat \tn % Row Count 9 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{Manipulations:} & array\_split, column\_stack, concatenate, diagonal, dsplit, dstack, hsplit, hstack, ndarray.item, newaxis, ravel, repeat, reshape, resize, squeeze, swapaxes, take, transpose, vsplit, vstack \tn % Row Count 17 (+ 8) % Row 3 \SetRowColor{white} Questions: & all, any, nonzero, where \tn % Row Count 18 (+ 1) % Row 4 \SetRowColor{LightBackground} Ordering: & argmax, argmin, argsort, max, min, ptp, searchsorted, sort \tn % Row Count 21 (+ 3) % Row 5 \SetRowColor{white} Operations: & choose, compress, cumprod, cumsum, inner, ndarray.fill, imag, prod, put, putmask, real, sum \tn % Row Count 25 (+ 4) % Row 6 \SetRowColor{LightBackground} Basic Statistics: & cov, mean, std, var \tn % Row Count 27 (+ 2) % Row 7 \SetRowColor{white} Basic Linear Algebra: & cross, dot, outer, linalg.svd, vdot \tn % Row Count 29 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Combining Arrays}} \tn % Row 0 \SetRowColor{LightBackground} np.vtack((a1, a2) = & np.concatenate((a1, a2, axis = 0) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} np.hstack(a1,a2) = & np.concatenate((a1, a2, axis = 1) \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} vsplit & splits along the vertical axis \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} hsplit & splits along the horizontal axis \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Universal Functions (ufuncts)}} \tn % Row 0 \SetRowColor{LightBackground} Implement vectorization (operations applied to whole arrays instead of individual elements) in NumPy which is way faster than iterating over elements. & Also provide broadcasting (when smaller array is cast across the larger array so that they have compatible shapes) \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} x = {[}1, 2, 3, 4{]} y = {[}4, 5, 6, 7{]} z = np.add(x, y) & {[} 5 7 9 11{]} \tn % Row Count 11 (+ 3) % Row 2 \SetRowColor{LightBackground} Check if a function is a ufunc: & print(type(np.add)) \tn % Row Count 13 (+ 2) % Row 3 \SetRowColor{white} arr1 = np.array({[}10, 20, 30, 40, 50, 60{]}) & arr2 = np.array({[}20, 21, 22, 23, 24, 25{]}) \tn % Row Count 16 (+ 3) % Row 4 \SetRowColor{LightBackground} np.subtract(arr1, arr2) & newarr = np.multiply(arr1, arr2) \tn % Row Count 18 (+ 2) % Row 5 \SetRowColor{white} newarr = np.divide(arr1, arr2) & newarr = np.power(arr1, arr2) \tn % Row Count 20 (+ 2) % Row 6 \SetRowColor{LightBackground} newarr = np.remainder(arr1, arr2) & newarr = np.absolute(arr) \tn % Row Count 22 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Iterating}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{arr = np.array({[}{[}1, 2, 3{]}, {[}4, 5, 6{]}{]})} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} for x in arr: print(x) & {[}1, 2, 3{]}, {[}4, 5, 6{]} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} for x in arr: for y in x: print(y) & 123456 \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} for x in np.nditer(arr): & 123456 \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} for x in np.nditer(arr, flags={[}'buffered'{]}, op\_dtypes={[}'S'{]}): print(x) & b '1', b'2', b'3' \tn % Row Count 11 (+ 4) % Row 5 \SetRowColor{white} for x in np.nditer(arr{[}:, ::2{]}): print(x) & 1, 3, 5, 7 \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Enumeration means mentioning sequence number of somethings one by one.} \tn % Row Count 16 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{for idx, x in np.ndenumerate(arr): print(idx, x)} \tn % Row Count 17 (+ 1) % Row 8 \SetRowColor{LightBackground} ​ for idx, x in \seqsplit{np.ndenumerate(arr):} print(idx, x) ​ ( & 0,) 1 (1,) 2 (2,) 3 \tn % Row Count 20 (+ 3) % Row 9 \SetRowColor{white} for idx, x in \seqsplit{np.ndenumerate(arr):} print(idx, x) & Result Size: 1425 x 1251 import numpy as np ​ arr = np.array({[}{[}1, 2, 3, 4{]}, {[}5, 6, 7, 8{]}{]}) ​ for idx, x in \seqsplit{np.ndenumerate(arr):} print(idx, x) ​ (0, 0) 1 (0, 1) 2 (0, 2) 3 (0, 3) 4 (1, 0) 5 (1, 1) 6 (1, 2) 7 (1, 3) 8 \tn % Row Count 32 (+ 12) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}