Python Cheat Sheet
Python Cheat Sheet
+ BONUS
+ BONUS
Solve puzzle 369!
+ BONUS
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
Like maths?
N
Y
Like language?
computer
Y N
science
Like painting?
linguistics
Y N
art
history
Remote Repository:
Master Branch
Alice Bob
git init
Clone repository
Clone
git add main.py </> git clone bob@host:/path/repos
Add file to project
new file
Pull
git pull </>
</>
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
Artist
Logic skills
Logic skills
Decision boundary
Decision
boundaries
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.
to Learn
The Ultimate Python Cheat Sheet
Keywords Basic Data Structures
Keyword Description Code Examples Type Description Code Examples
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}
Keep it simple!