\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 (python-snippits.pdf) /Creator (Cheatography) /Author (datamansam) /Subject (Python Snippits 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{Python Snippits Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{datamansam} via \textcolor{DarkBackground}{\uline{cheatography.com/139410/cs/29923/}}} \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 15th May, 2022.\\ Updated 9th July, 2022.\\ 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.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Operators}} \tn % Row 0 \SetRowColor{LightBackground} Arithmetic & (Addition(+), Substraction(-), Multiplication(*), Division(/), Modulus(\%) \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} Relational & \textless{}, \textgreater{}, \textless{}=, \textgreater{}=, ==, != (not equal), \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} Assignment & =. +=, -=, /=, *=, \%= \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} Logical & and. or, not \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} Membership & in, not in \tn % Row Count 9 (+ 1) % Row 5 \SetRowColor{white} Identity (same memory location) & is, is not \tn % Row Count 11 (+ 2) \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}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} len() & determine the length of a string, a list, an array & \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} split() & split a string into shorter string using defined seperatos & \seqsplit{string.split("},") \tn % Row Count 9 (+ 5) % Row 2 \SetRowColor{LightBackground} sum(),mean(), count(),std(). & functions that can be used by grouby in pandas & \seqsplit{grouped\_multiple} = df.groupby({[}'Team', 'Pos'{]}).agg(\{'Age': {[}'mean', 'min', 'max'{]}\}) \seqsplit{grouped\_multiple}.columns = {[}'age\_mean', 'age\_min', 'age\_max'{]} \seqsplit{grouped\_multiple} = \seqsplit{grouped\_multiple}.reset\_index() \tn % Row Count 23 (+ 14) % Row 3 \SetRowColor{white} & & df.groupby({[}"Team","College"{]}){[}"Salary"{]}.max() \tn % Row Count 27 (+ 4) % Row 4 \SetRowColor{LightBackground} agg() & Allows for multiple or custom aggregations & \seqsplit{defpct30(column):} \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \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}{Functions (cont)}} \tn % Row 5 \SetRowColor{LightBackground} & & return \seqsplit{column.quantile(0.3)} \tn % Row Count 2 (+ 2) % Row 6 \SetRowColor{white} & & dogs{[}"weight\_kg"{]}.agg(pct30) \tn % Row Count 4 (+ 2) % Row 7 \SetRowColor{LightBackground} keys() & We can use the Keys function of a Group By object to describe how rows of a dataset has been split & data.groupby({[}'month'{]}).groups.keys() \tn % Row Count 11 (+ 7) % Row 8 \SetRowColor{white} join() and ravel() & An effective way to rename columns after a group & grouped = \seqsplit{data.groupby('month').agg("duration":} {[}min, max, mean{]}) \tn % Row Count 16 (+ 5) % Row 9 \SetRowColor{LightBackground} & & \seqsplit{grouped.columns} = {[}"\_".join(x) for x in \seqsplit{grouped.columns}.ravel(){]} \tn % Row Count 21 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Custom Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{User-Defined Functions \newline \newline By adding {\emph{ to a parameter, we can add any number of arguments to that parameters \newline \newline def func\_with\_var\_pos\_args(}}args): \newline for arg in args: \newline print(arg) \newline \newline Simiarly, by adding {\emph{ to an argument, we can add any number of arguments to that parameters \newline \newline def func\_with\_var\_pos\_args(}}args): \newline for arg in args: \newline print(arg)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.39356 cm} x{3.58344 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Naming Conventions}} \tn % Row 0 \SetRowColor{LightBackground} Funciton & function, my\_function \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Variable & x, var, my\_variable \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Class & Model, MyClass \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} Method & class\_method, method \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Packaging and Displaying}} \tn % Row 0 \SetRowColor{LightBackground} from pprint import pprint & pprint(dir(my\_dict)) \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Pychecker & detects bugs from the source code and warns about its style and complexity \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} Pylint & Checks whether the module matches upto a coding standard. \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} Modules & Each Python program file is a module, importing other attributes and objects. \tn % Row Count 13 (+ 4) % Row 4 \SetRowColor{LightBackground} Package & folder of modules \tn % Row Count 14 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4177 cm} x{1.08602 cm} x{1.33664 cm} x{1.33664 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{5.377cm}}{\bf\textcolor{white}{Map, Filter and Lambda}} \tn % Row 0 \SetRowColor{LightBackground} Map & Applies a function to the input list & \seqsplit{map(function\_to\_apply}, \seqsplit{list\_of\_inputs)} & items = {[}1, 2, 3, 4, 5{]} squared = \seqsplit{list(map(lambda} x: x**2, items)) \tn % Row Count 6 (+ 6) % Row 1 \SetRowColor{white} \seqsplit{filter} & creates a list of elements for which a function returns true. & \seqsplit{filter(function\_to\_apply},n \seqsplit{lisst\_to\_select\_From)} & \seqsplit{number\_list} = range(-5, 5) \seqsplit{less\_than\_zero} = \seqsplit{list(filter(lambda} x: x \textless{} 0, \seqsplit{number\_list))} \tn % Row Count 14 (+ 8) % Row 2 \SetRowColor{LightBackground} \seqsplit{Reduce} & applies a rolling \seqsplit{computation} to \seqsplit{sequential} pairs of values in a list & from functools import reduce & product = \seqsplit{reduce((lambda} x, y: x * y), {[}1, 2, 3, 4{]}) \tn % Row Count 21 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Scikit Learn - Regression}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{poly\_reg = \seqsplit{PolynomialFeatures(degree} = 2) \newline X\_poly = \seqsplit{poly\_reg.fit\_transform(xtrain)} \newline X\_poly.predict(xtest) \newline \newline xtrainp= X\_poly{[}:11900*3{]} \newline \newline \newline \# polynomial regression model \newline poly\_reg\_model = LinearRegression() \newline poly\_reg\_model.fit(xtrainp, ytrain) \newline poly\_reg\_model.predict(xtest) \newline print( \seqsplit{metrics.mean\_squared\_error(y\_test}, \seqsplit{poly\_reg\_model.predict(xtest)} ) ) \newline \newline \newline svr\_regressor = SVR(kernel='rbf', gamma='auto') \newline svr\_regressor.fit(xtrain, ytrain) \newline \newline tree\_regressor = \seqsplit{DecisionTreeRegressor(random\_state} = 0) \newline tree\_regressor.fit(xtrain, ytrain) \newline \newline forest\_regressor = \seqsplit{RandomForestRegressor(n\_estimators} = 300, random\_state = 0) \newline forest\_regressor.fit(xtrain, ytrain) \newline \newline from sklearn import linear\_model \newline reg = \seqsplit{linear\_model.LassoLars(alpha=.1}, normalize=False) \newline reg.fit(xtrain, ytrain) \newline \newline reg.coef\_ \newline reg.predict(xtest) \newline \newline est = SGDClassifier() \newline est.fit(xtrain, ytrain) \newline est.predict(xtest) \newline \newline linear\_regression = LinearRegression() \newline \newline y\_pred\_lr = \seqsplit{linear\_regression.fit(xtrain}, ytrain).predict(xtest) \newline \newline \newline xgbmodel = \seqsplit{xgboost.XGBRegressor(colsample\_bytree=0.4}, \newline gamma=0, \newline learning\_rate=0.07, \newline max\_depth=3, \newline min\_child\_weight=1.5, \newline n\_estimators=10000, \newline reg\_alpha=0.75, \newline reg\_lambda=0.45, \newline subsample=0.6, \newline seed=42) \newline xgbmodel.fit(xtrain, ytrain) \newline \newline \newline print( \seqsplit{svr\_regressor.predict(xtest))} \newline print( \seqsplit{tree\_regressor.predict(xtest))} \newline print( y\_pred\_lr) \newline print( \seqsplit{forest\_regressor.predict(xtest))} \newline model.predict(xtest) \newline \newline \newline print( \seqsplit{metrics.mean\_squared\_error(y\_test}, \seqsplit{svr\_regressor.predict(xtest)} ) ) \newline \newline print( \seqsplit{metrics.mean\_squared\_error(y\_test}, \seqsplit{tree\_regressor.predict(xtest)} ) ) \newline print( \seqsplit{metrics.mean\_squared\_error(y\_test}, y\_pred\_lr) ) \newline print( \seqsplit{metrics.mean\_squared\_error(y\_test}, \seqsplit{forest\_regressor.predict(xtest)} ) ) \newline \newline \newline \newline forestrev = \seqsplit{forest\_regressor.predict(xtest)} \newline \newline xgbmodel.predict(xtest).mean() \newline print( \seqsplit{metrics.mean\_squared\_error(y\_test}, xgbmodel.predict(xtest) ) ) \newline \newline \newline ytest.mean() \newline \newline bas.REVENUE.mean() \newline \newline \newline \newline \newline \newline xtrain, ytrain = np.array(xtrain), np.array(ytrain) \newline \newline xtrain = np.reshape(xtrain, (xtrain.shape{[}0{]},xtrain.shape{[}1{]},1)) \newline \newline \# create and fit the LSTM network \newline model = Sequential() \newline model.add(LSTM(units=50, return\_sequences=True, input\_shape=(xtrain.shape{[}1{]},1))) \newline model.add(LSTM(units=50)) \newline model.add(Dense(1)) \newline \newline ytrain = \seqsplit{ytrain.astype(np.float32)} \newline xtrain = \seqsplit{xtrain.astype(np.float32)} \newline xtrain = np.reshape(xtrain, (xtrain.shape{[}0{]},xtrain.shape{[}1{]},1))} \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}{Looping Data Structures}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{1) With One Column: \newline import pandas as pd \newline \#The column to look through \newline brics = pd.read\_csv("brics.csv", index\_col = 0) \newline for val in brics : \newline print(val) \newline \newline 2) Index then all cols in row: \newline for lab, row in brics.iterrows(): \newline print(lab) \newline print(row) \newline \newline 3) Index then one col in row: \newline for lab, row in brics.iterrows(): \newline brics.loc{[}lab, "name\_length"{]} = len(row{[}"country"{]}) \newline \newline 4) Apply \newline brics{[}"name\_length"{]} = brics{[}"country"{]}.apply(len)} \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}{Scikit Learn - Classification}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#\# Classifier imports \newline from sklearn.neighbors import KNeighborsClassifier \newline from sklearn.naive\_bayes import GaussianNB, MultinomialNB, BernoulliNB \newline from sklearn.linear\_model import LogisticRegression, SGDClassifier \newline from sklearn.svm import SVC, LinearSVC, NuSVC \newline from sklearn.linear\_model import Ridge \newline from sklearn.ensemble import AdaBoostClassifier \newline from sklearn.ensemble import \seqsplit{GradientBoostingClassifier} \newline \newline \# Defining our models \newline gnb = GaussianNB() \newline KNN = \seqsplit{KNeighborsClassifier(n\_neighbors=1)} \newline MNB = MultinomialNB() \newline BNB = BernoulliNB() \newline LR = LogisticRegression() \newline SDG = SGDClassifier() \newline \#SVC = SVC(kernel='linear', C=1e3) \newline LSVC = LinearSVC() \newline NSVC = NuSVC() \newline \newline \# Train our classifier and print accuracy scores \newline gnb.fit(x1, y1) \newline y2\_GNB\_model = gnb.predict(x2) \newline print("GaussianNB Accuracy :", accuracy\_score(y2, y2\_GNB\_model)) \newline \newline KNN.fit(x1,y1) \newline y2\_KNN\_model = KNN.predict(x2) \newline print("KNN Accuracy :", accuracy\_score(y2, y2\_KNN\_model)) \newline \newline \#MNB.fit(x1,y1) \newline \#y2\_MNB\_model = MNB.predict(x2) \newline \#print("MNB Accuracy :", accuracy\_score(y2, y2\_MNB\_model)) \newline \newline BNB.fit(x1,y1) \newline y2\_BNB\_model = BNB.predict(x2) \newline print("BNB Accuracy :", accuracy\_score(y2, y2\_BNB\_model)) \newline \newline LR.fit(x1,y1) \newline y2\_LR\_model = LR.predict(x2) \newline print("LR Accuracy :", accuracy\_score(y2, y2\_LR\_model)) \newline \newline SDG.fit(x1,y1) \newline y2\_SDG\_model = SDG.predict(x2) \newline print("SDG Accuracy :", accuracy\_score(y2, y2\_SDG\_model)) \newline \newline \# SVC.fit(x1,y1) \newline \# y2\_SVC\_model = SVC.predict(x2) \newline \# print("SVC Accuracy :", accuracy\_score(y2, y2\_SVC\_model)) \newline \newline LSVC.fit(x1,y1) \newline y2\_LSVC\_model = LSVC.predict(x2) \newline print("LSVC Accuracy :", accuracy\_score(y2, y2\_LSVC\_model)) \newline \newline NSVC.fit(x1,y1) \newline y2\_NSVC\_model = NSVC.predict(x2) \newline print("NSVC Accuracy :", accuracy\_score(y2, y2\_NSVC\_model))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}