0% found this document useful (0 votes)
187 views

Python Cheat Sheet

The document contains several puzzles and cheat sheets related to coding, machine learning, and git. The first section includes a test sheet with several puzzles labeled to help "Alice find her coding dad." The second section provides a cheat sheet with summaries of six common machine learning algorithms. The third section shows a simple git workflow diagram and descriptions of basic git commands like clone, add, commit, and push. The final section contains a machine learning cheat sheet focusing on support vector machines.

Uploaded by

Sebas Corvalán
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views

Python Cheat Sheet

The document contains several puzzles and cheat sheets related to coding, machine learning, and git. The first section includes a test sheet with several puzzles labeled to help "Alice find her coding dad." The second section provides a cheat sheet with summaries of six common machine learning algorithms. The third section shows a simple git workflow diagram and descriptions of basic git commands like clone, add, commit, and push. The final section contains a machine learning cheat sheet focusing on support vector machines.

Uploaded by

Sebas Corvalán
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

[Test Sheet] Help Alice Find Her Coding Dad!

+ BONUS

Solve puzzle 332! Solve puzzle 93!

Solve puzzle 137!


Solve puzzle 441!

+ BONUS
Solve puzzle 369!

Solve puzzle 377!

+ BONUS

Solve puzzle 366!

Continuous Improvement in Your


[Cheat Sheet] 6 Pillar Machine Learning Algorithms
Complete Course: https://academy.finxter.com/

Linear Regression K-Means Clustering


https://blog.finxter.com/logistic-regression-in-one-line-python/ https://blog.finxter.com/tutorial-how-to-run-k-means-
clustering-in-1-line-of-python/

K Nearest Neighbors Support Vector Machine


https://blog.finxter.com/k-nearest-neighbors-as-a-
python-one-liner/
Classification
https://blog.finxter.com/support-vector-machines-python/

Support vector

A:
Logic skills

B:
House Price ($)

C:
A
B

C 3NN Decision
boundary
Computer Scientist
D: Artist
House Size (square meter)
Creativity skills

Decision Tree Classification Multilayer Perceptron


https://blog.finxter.com/decision-tree-learning-in-one-line-python/
https://blog.finxter.com/tutorial-how-to-create-your-first-neural-
network-in-1-line-of-python-code/

Like maths?
N
Y

Like language?
computer
Y N
science

Like painting?
linguistics
Y N

art
history

Continuous Improvement in Your


The Simple Git Cheat Sheet A Helpful Illustrated Guide
The Centralized Git Workflow

Every coder has own copy of project


Independence of workflow Git Master Branch
No advanced branching and merging needed

Remote Repository:
Master Branch

Alice Bob
git init

Clone Create new repository


git clone alice@host:/path/repos

Clone repository
Clone
git add main.py </> git clone bob@host:/path/repos
Add file to project

git commit new file

Commit change and add message


new file to the master branch

git push origin master

Send master branch to remote


repository Push
</>

new file

Pull
git pull </>

Update local repository


with master branch new file

git add * </> </>


Add all changes to master

git rm main.py </>


Remove file from
master branch

git commit add 2,


rem
Commit change and add
message to the master

git push origin master


</> </> Send master branch to
Push remote repository

</>
to Learn, Code,
add 2, rem
[Machine Learning Cheat Sheet] Support Vector Machines
Based on Article: https://blog.finxter.com/support-vector-machines-python/

Main idea: Maximize width of separator zone increases margin of safety for classification

Machine Learning Classification Support Vector Machine Classification

Computer Scientist Support vectors

Artist

Logic skills
Logic skills

Decision boundary
Decision
boundaries

Creativity skills Creativity skills

What are basic SVM properties? the explanation of the code example?
Support Vector Machines Explanation: A Study Recommendation System with SVM
Alternatives: SVM, support-vector networks
Learning: Classification, Regression NumPy array holds labeled training data (one row per user and one
Advantages: Robust for high-dimensional space column per feature).
Memory efficient (only uses support vectors)
Flexible and customizable Features: skill level in maths, language, and creativity.
Disadvantages: Danger of overfitting in high-dimensional space
No classification probabilities like Decision trees Labels: last column is recommended study field.
Boundary: Linear and Non-linear
3D data SVM separates data using 2D planes (the linear separator)
rather than 1D lines.

the most basic Python code example? One-liner:

## Dependencies 1. Create model using constructor of scikit- svm.SVC class


from sklearn import svm (SVC = support vector classification).
import numpy as np
2. Call fit function to perform training based on labeled training
data.
## Data: student scores in (math, language, creativity)
## --> study field Results: call predict function on new observations
X = np.array([[9, 5, 6, "computer science"],
[10, 1, 2, "computer science"], student_0 (skills maths=3, language=3, and creativity=6)
[1, 8, 1, "literature"],
[4, 9, 3, "literature"],
[0, 1, 10, "art"], student_1 (maths=8, language=1, and creativity=1) SVM
[5, 7, 9, "art"]]) predicts

Final output of one-liner:


## One-liner
svm = svm.SVC().fit(X[:,:-1], X[:,-1]) ## Result & puzzle
student_0 = svm.predict([[3, 3, 6]])
print(student_0)
## Result & puzzle # ['art']
student_0 = svm.predict([[3, 3, 6]])
student_1 = svm.predict([[8, 1, 1]])
print(student_0)
print(student_1)
## ['computer science']
student_1 = svm.predict([[8, 1, 1]])
print(student_1)

to Learn
The Ultimate Python Cheat Sheet
Keywords Basic Data Structures
Keyword Description Code Examples Type Description Code Examples

Boolean The Boolean data type is ## Evaluates to True:


False, Boolean data type False == (1 > 2)
either True or False. 1<2 and 0<=1 and 3>2 and 2>=2 and 1==1
True True == (2 > 1)
Boolean operators are and 1!=0
ordered by priority:
Logical operators ## Evaluates to False:
not and or
and, Both are true True and True # True bool(None or 0 or 0.0 or '' or [] or
or, Either is true True or False # True {} or set())
not Flips Boolean not False # True
Rule: None, 0, 0.0, empty strings, or empty container
1, 2, 3 types evaluate to False
break Ends loop prematurely while True:
break # finite loop Integer, An integer is a positive or ## Arithmetic Operations
Float negative number without x, y = 3, 2
continue Finishes current loop iteration while True: decimal point such as 3. print(x + y) # = 5
continue print(x - y) # = 1
print("42") # dead code A float is a positive or print(x * y) # = 6
negative number with print(x / y) # = 1.5
class Defines new class class Coffee: floating point precision print(x // y) # = 1
# Define your class such as 3.1415926. print(x % y) # = 1
print(-x) # = -3
def Defines a new function or class def say_hi(): Integer division rounds print(abs(-x)) # = 3
method. print('hi') toward the smaller integer print(int(3.9)) # = 3
(example: 3//2==1). print(float(3)) # = 3.0
if, Conditional execution: x = int(input("ur val:")) print(x ** y) # = 9
elif, - condition == True? if x > 3: print("Big")
else - "elif" condition == True? elif x == 3: print("3") String Python Strings are ## Indexing and Slicing
- Fallback: else branch else: print("Small") sequences of characters. s = "The youngest pope was 11 years"
s[0] # 'T'
String Creation Methods: s[1:3] # 'he' Slice [::2]
for, # For loop # While loop does same
while for i in [0,1,2]: j = 0 1. Single quotes s[-3:-1] # 'ar'
print(i) while j < 3: >>> 'Yes' s[-3:] # 'ars' 1 2 3 4
print(j); j = j + 1 2. Double quotes
>>> "Yes" x = s.split() 0 1 2 3
in Sequence membership 42 in [2, 39, 42] # True 3. Triple quotes (multi-line) x[-2] + " " + x[2] + "s" # '11 popes'
>>> """Yes
is Same object memory location y = x = 3 We Can""" ## String Methods
x is y # True 4. String method y = " Hello world\t\n "
[3] is [3] # False >>> str(5) == '5' y.strip() # Remove Whitespace
True "HI".lower() # Lowercase: 'hi'
None Empty value constant print() is None # True 5. Concatenation "hi".upper() # Uppercase: 'HI'
>>> "Ma" + "hatma" "hello".startswith("he") # True
lambda Anonymous function (lambda x: x+3)(3) # 6 'Mahatma' "hello".endswith("lo") # True
"hello".find("ll") # Match at 2
return Terminates function. Optional def increment(x): Whitespace chars: "cheat".replace("ch", "m") # 'meat'
return value defines function return x + 1 Newline \n, ''.join(["F", "B", "I"]) # 'FBI'
result. increment(4) # returns 5 Space \s, len("hello world") # Length: 15
Tab \t "ear" in "earth" # True

Complex Data Structures


Type Description Example Type Description Example

List Stores a sequence of l = [1, 2, 2] Dictionary Useful data structure for cal = {'apple' : 52, 'banana' : 89,
elements. Unlike strings, you print(len(l)) # 3 storing (key, value) pairs 'choco' : 546} # calories
can modify list objects (they're
Reading Read and write elements by print(cal['apple'] < cal['choco'])
mutable).
and specifying the key within the # True
Adding Add elements to a list with (i) [1, 2].append(4) # [1, 2, 4] writing brackets. Use the keys() cal['cappu'] = 74
elements append, (ii) insert, or (iii) list [1, 4].insert(1,9) # [1, 9, 4] elements and values() functions to print(cal['banana'] < cal['cappu'])
concatenation. [1, 2] + [4] # [1, 2, 4] access all keys and values of # False
the dictionary
print('apple' in cal.keys()) # True
Removal Slow for lists [1, 2, 2, 4].remove(1) # [2, 2, 4]
print(52 in cal.values()) # True
Reversing Reverses list order [1, 2, 3].reverse() # [3, 2, 1]
Dictionary You can access the (key, for k, v in cal.items():
Sorting Sorts list using fast Timsort [2, 4, 2].sort() # [2, 2, 4] Iteration value) pairs of a dictionary print(k) if v > 500 else ''
with the items() method. # 'choco'
Indexing Finds the first occurrence of [2, 2, 4].index(2)
an element & returns index. # index of item 2 is 0 Member- Check with the in keyword if basket = {'apple', 'eggs',
Slow worst case for whole list [2, 2, 4].index(2,1) ship set, list, or dictionary contains 'banana', 'orange'}
traversal. # index of item 2 after pos 1 is 1 operator an element. Set membership print('eggs' in basket) # True
is faster than list membership. print('mushroom' in basket) # False
Stack Use Python lists via the list stack = [3]
operations append() and pop() stack.append(42) # [3, 42] List & set List comprehension is the l = ['hi ' + x for x in ['Alice',
stack.pop() # 42 (stack: [3]) comprehe concise Python way to create 'Bob', 'Pete']]
stack.pop() # 3 (stack: []) nsion lists. Use brackets plus an # ['Hi Alice', 'Hi Bob', 'Hi Pete']
expression, followed by a for
Set An unordered collection of basket = {'apple', 'eggs', clause. Close with zero or l2 = [x * y for x in range(3) for y
unique elements (at-most- 'banana', 'orange'} more for or if clauses. in range(3) if x>y] # [0, 0, 2]
once) fast membership O(1) same = set(['apple', 'eggs', Set comprehension works
squares = { x**2 for x in [0,2,4]
'banana', 'orange']) similar to list comprehension.
if x < 4 } # {0, 4}

Subscribe to the 11x FREE Python Cheat Sheet Course:


https://blog.finxter.com/python-cheat-sheets/
Book:
Complexity appears in Project Lifecycle Cyclomatic Complexity Runtime Complexity
Complexity
Project Lifecycle
Code Development
of parts difficult to Algorithmic Theory
analyze, understand, Processes
or explain". Social Networks
Learning & Your Daily Life

Keep it simple!

80/20 Principle Pareto Tips Minimum Viable


1. Figure out your success metrics.
Majority of effects come 2. Figure out your big goals in life. Product (MVP)
from the minority of causes. 3. Look for ways to achieve the same A minimum viable
things with fewer resources. product in the
4. Reflect on your own successes software sense is code
5. Reflect on your own failures that is stripped from
6. Read more books in your industry. all features to focus on
7. Spend much of your time the core functionality.
improving and tweaking existing How to MVP?
products Formulate
8. Smile. hypothesis
9. Don't do things that reduce value Omit needless
Maximize Success Metric: features
#lines of code written Split test to validate
each new feature
Clean Code Principles Unix Philosophy Focus on product-
1. You Ain't Going to Need It 1. market fit
2. The Principle of Least Surprise Complex Seek high-value and
3. Don't Repeat Yourself 2. Small is Beautiful (Again) low-cost features
4. Code For People Not Machines 3. Make Each Program Do One Performance Tuning 101
5. Stand on the Shoulders of Giants Thing Well Premature Optimization
1. Measure, then improve
6. Use the Right Names 4. Build a Prototype First "Programmers waste enormous 2. Focus on the slow 20%
7. Single-Responsibility Principle 5. Portability Over Efficiency 3. Algorithmic optimization
8. Use Comments 6. Store Data in Flat Text Files the speed of noncritical parts of their wins
9. Avoid Unnecessary Comments 7. Use Software Leverage programs. We should forget about 4. All hail to the cache
10. Be Consistent 8. Avoid Captive User small efficiencies, say about 97 % of 5. Solve an easier problem
11. Test Interfaces the time: premature optimization is version
12. Think in Big Pictures 9. Program = Filter the root of all evil." Donald Knuth 6. Know when to stop
13. Only Talk to Your Friends 10. Worse is Better
14. Refactor 11. Clean > Clever Code the source code of ultimate human performance" Kotler
15. 12. Design Connected Programs Flow
Flow Tips for Coders
16. 13. Make Your Code Robust
1. Always work on an explicit
17. Small is Beautiful 14. Repair What You Can But
practical code project
18. Use Metrics Fail Early and Noisily
2. Work on fun projects that
19. Boy Scout Rule: Leave Camp 15. Write Programs to Write
fulfill your purpose
Cleaner Than You Found It Programs
3. Perform from your
strengths
Less Is More in Design How to Simplify Design? 4. Big chunks of coding time
1. Use whitespace 5. Reduce distractions:
2. Remove design elements smartphone + social
3. Remove features How to Achieve Flow? (1) clear 6. Sleep a lot, eat healthily,
4. Reduce variation of fonts, read quality books, and
font types, colors goals, (2) immediate feedback, and
exercise garbage in,
5. Be consistent across UIs (3) balance opportunity & capacity. garbage out!

Focus 3-Step Approach of


Efficient Software Creation
You can take raw resources and 1. Plan your code
move them from a state of high 2. Apply focused effort
entropy into a state of low entropy to make it real.
using focused effort towards the 3. Seek feedback
attainment of a greater plan.
Figure: Same effort, different result.
Subscribe to the 11x FREE Python Cheat Sheet Course:
https://blog.finxter.com/python-cheat-sheets/

You might also like