\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{Justin1209 (Justin1209)} \pdfinfo{ /Title (numpy.pdf) /Creator (Cheatography) /Author (Justin1209 (Justin1209)) /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}{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 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Justin1209 (Justin1209)} via \textcolor{DarkBackground}{\uline{cheatography.com/101982/cs/21247/}}} \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}Justin1209 (Justin1209) \\ \uline{cheatography.com/justin1209} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 28th November, 2019.\\ Updated 18th December, 2019.\\ 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Import Statement}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{import}} {\emph{numpy}} {\bf{as}} {\emph{np}}} \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Creating Arrays}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# {\emph{Create a numpy array}} \newline array\_1 = np.{\bf{array}}({[}92, 94, 88, 91, 87{]}) \newline \newline \# {\emph{Create a numpy array from a CSV}} \newline test\_2 = np.{\bf{genfromtxt}}('test\_2.csv', delimiter=',') \newline \newline \# {\emph{Create a two-dimensional array}} \newline test\_1 = np.array({[}92, 94, 88, 91, 87{]}) \newline test\_2 = np.array({[}79, 100, 86, 93, 91{]}) \newline test\_3 = np.array({[}87, 85, 72, 90, 92{]}) \newline \newline np.array({[}{[}92, 94, 88, 91, 87{]}, \newline {[}79, 100, 86, 93, 91{]}, \newline {[}87, 85, 72, 90, 92{]}{]})} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Operations with Arrays}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{arr = {[}1, 2, 3, 4, 5{]} \newline \newline \# {\bf{Adding 3 to each entry}} \newline \textgreater{}\textgreater{}\textgreater{} a = np.array(arr) \newline \textgreater{}\textgreater{}\textgreater{} a\_plus\_3 = a + 3 \newline \newline \# {\bf{Adding arrays}} \newline \textgreater{}\textgreater{}\textgreater{} a = np.array({[}1, 2, 3, 4, 5{]}) \newline \textgreater{}\textgreater{}\textgreater{} b = np.array({[}6, 7, 8, 9, 10{]}) \newline \textgreater{}\textgreater{}\textgreater{} c = a + b \newline \newline \# {\bf{Logical Operations}} \newline \textgreater{}\textgreater{}\textgreater{} a = np.array({[}10, 2, 2, 4, 5, 3, 9, 8, 9, 7{]}) \newline \newline {\bf{\textgreater{}\textgreater{}\textgreater{} a \textgreater{} 5}} \newline array({[}True, False, False, False, False, False, True, True, True, True{]}, dtype=bool) \newline \newline {\bf{\textgreater{}\textgreater{}\textgreater{} a{[}a \textgreater{} 5{]}}} \newline array({[}10, 9, 8, 9, 7{]}) \newline \newline {\bf{\textgreater{}\textgreater{}\textgreater{} a{[}(a \textgreater{} 5) | (a \textless{} 2){]}}} \newline array({[}10, 9, 8, 9, 7{]}) \newline \newline -\textgreater{} {\emph{c: array({[} 7, 9, 11, 13, 15{]})}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Selecting from Arrays (1 Dimension)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{a = np.array({[}5, 2, 7, 0, 11{]}) \newline \newline \textgreater{}\textgreater{}\textgreater{} {\bf{a{[}0{]}}} \newline -\textgreater{} {\emph{5}} \newline \newline \textgreater{}\textgreater{}\textgreater{} {\bf{a{[}-1{]}}} \newline -\textgreater{} {\emph{11}} \newline \newline \textgreater{}\textgreater{}\textgreater{} {\bf{a{[}-2{]}}} \newline -\textgreater{} {\emph{0}} \newline \newline \textgreater{}\textgreater{}\textgreater{} {\bf{a{[}0:5:2{]}}} \newline -\textgreater{} {\emph{array({[}5, 7, 11{]}) \newline \newline \textgreater{}\textgreater{}\textgreater{} {\bf{a{[}1:3{]}}} \newline -\textgreater{} }}array({[}2, 7{]}){\emph{ \newline \newline \textgreater{}\textgreater{}\textgreater{} {\bf{a{[}:3{]}}} \newline -\textgreater{} }}array({[}5, 2, 7{]}){\emph{ \newline \newline \textgreater{}\textgreater{}\textgreater{} {\bf{a{[}-3:{]}}} \newline -\textgreater{} }}array({[}7, 0, 11{]})*} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Selecting from Arrays (2 Dimensions)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{-\textgreater{} {\bf{Basic Procedure}} {\emph{a{[}row,column{]}}} \newline \newline a = np.array({[}{[}32, 15, 6, 9, 14{]}, \newline {[}12, 10, 5, 23, 1{]}, \newline {[}2, 16, 13, 40, 37{]}{]}) \newline \newline \# selects the first column \newline \textgreater{}\textgreater{}\textgreater{} {\bf{a{[}:,0{]}}} \newline -\textgreater{} {\emph{array({[}32, 12, 2{]})}} \newline \newline \# selects the second row \newline \textgreater{}\textgreater{}\textgreater{} {\bf{a{[}1,:{]}}} \newline -\textgreater{} {\emph{array({[}12, 10, 5, 23, 1{]})}} \newline \newline \# selects the first three elements of the first row \newline \textgreater{}\textgreater{}\textgreater{} {\bf{a{[}0,0:3{]}}} \newline -\textgreater{} {\emph{array({[}32, 15, 6{]})}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Selecting Elements}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{np.{\bf{count\_nonzero}}(poodle\_colors == "brown") \newline -\textgreater{} {\emph{returns the number of poodles with brown hair}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Mean and Logical Operations (On arrays)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{np.{\bf{mean}}(array \textgreater{} 8) \newline -\textgreater{} {\emph{returns the percentage of values in the array that meet the criteria}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{We can use {\bf{np.mean}} to calculate the percent of array elements that have a certain property.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Mean over 2 Dimensional Arrays}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\textgreater{}\textgreater{}\textgreater{} ring\_toss = np.array({[}{[}1, 0, 0{]}, \newline {[}0, 0, 1{]}, \newline {[}1, 0, 1{]}{]}) \newline \newline \textgreater{}\textgreater{}\textgreater{} np.{\bf{mean}}(ring\_toss) \newline 0.44 -\textgreater{} {\emph{Overall Average}} \newline \newline \textgreater{}\textgreater{}\textgreater{} np.{\bf{mean}}(ring\_toss, {\bf{axis=1}}) \newline array({[} 0.33, 0.33, 0.67{]}) -\textgreater{} {\emph{Average per row}} \newline \newline \textgreater{}\textgreater{}\textgreater{} np.{\bf{mean}}(ring\_toss, {\bf{axis=0}}) \newline array({[} 0.67, 0. , 0.67{]}) -\textgreater{} {\emph{Average per column}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Dealing with Outliers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# {\bf{Sort the Dataset}} \newline np.{\bf{sort}}(array) \newline -\textgreater{} {\emph{Outliers are clearly visible now}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Percentiles}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{d = np.array({[}1, 2, 3, 4, 4, 4, 6, 6, 7, 8, 8{]}) \newline np.{\bf{percentile}}(d, 40) \newline -\textgreater{} {\emph{4.00}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Shape (dimensions) of an array}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{The {\bf{.shape}} attribute for NumPy arrays returns the dimensions of the array. If array has n rows × m columns, then array.shape returns (n, m).% Row Count 3 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Generate Normal Distribution}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# {\bf{Generate own Normal Distribution Set}} \newline -\textgreater{} {\emph{np.random.normal(loc, scale, size)}} \newline {\bf{loc}}: the mean for the normal distribution \newline {\bf{scale}}: the standard deviation of the distribution \newline {\bf{size}}: the number of random numbers to generate} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{68\%}} of our samples will fall between {\bf{+/- 1 standard deviation}} of the mean \newline \newline {\bf{95\%}} of our samples will fall between {\bf{+/- 2 standard deviations}} of the mean \newline \newline {\bf{99.7\%}} of our samples will fall between {\bf{+/- 3 standard deviations}} of the mean} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Binomial Distribution}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{np.{\bf{random.binomial}}{\emph{(N, P, size)}} \newline \newline {\bf{N:}} The number of samples or trials \newline {\bf{P:}} The probability of success \newline {\bf{size:}} The number of experiments \newline \newline \#{\bf{Basketball Example}} \newline Let's generate 10,000 "experiments" \newline N = 10 shots \newline P = 0.30 (30\% he'll get a free throw) \newline -\textgreater{} a = np.{\bf{random.binomial}}{\emph{(10, 0.3, 10000)}} \newline \newline \# {\emph{Probability that he makes 4 Shots:}} \newline prob = np.mean(a == 4)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The {\bf{binomial distribution}} can help us. It tells us {\bf{how likely}} it is for a {\bf{certain number of "successes"}} to happen, given a probability of success and a number of trials.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}