python_cheatsheet_4
python_cheatsheet_4
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? What‘s the explanation of the code example?
Support Vector Machines Explanation: A Study Recommendation System with SVM
Alternatives: SVM, supportvector networks
Learning: Classification, Regression • NumPy array holds labeled training data (one row per user and one
Advantages: Robust for highdimensional 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 highdimensional space
No classification probabilities like Decision trees • Labels: last column is recommended study field.
Boundary: Linear and Nonlinear
• 3D data → SVM separates data using 2D planes (the linear separator)
rather than 1D lines.
lst.clear() Removes all elements from the list >>> lst = [1, 2, 3, 4, 5]
lst–which becomes empty. >>> lst.clear()
[]
lst.copy() Returns a copy of the list lst. Copies only >>> lst = [1, 2, 3]
the list, not the elements in the list (shallow >>> lst.copy()
[1, 2, 3]
copy).
lst.count(x) Counts the number of occurrences of >>> lst = [1, 2, 42, 2, 1, 42, 42]
element x in the list lst. >>> lst.count(42)
3
>>> lst.count(2)
2
lst.extend(iter) Adds all elements of an iterable iter (e.g. >>> lst = [1, 2, 3]
another list) to the list lst. >>> lst.extend([4, 5, 6])
[1, 2, 3, 4, 5, 6]
lst.index(x) Returns the position (index) of the first >>> lst = ["Alice", 42, "Bob", 99]
occurrence of value x in the list lst. >>> lst.index("Alice")
0
>>> lst.index(99, 1, 3)
ValueError: 99 is not in list
lst.insert(i, x) Inserts element x at position (index) i in >>> lst = [1, 2, 3, 4]
the list lst. >>> lst.insert(3, 99)
[1, 2, 3, 99, 4]
lst.pop() Removes and returns the final element of >>> lst = [1, 2, 3]
the list lst. >>> lst.pop()
3
>>> lst
[1, 2]
lst.remove(x) Removes and returns the first occurrence >>> lst = [1, 2, 99, 4, 99]
of element x in the list lst. >>> lst.remove(99)
>>> lst
[1, 2, 4, 99]
lst.reverse() Reverses the order of elements in the list >>> lst = [1, 2, 3, 4]
lst. >>> lst.reverse()
>>> lst
[4, 3, 2, 1]
lst.sort() Sorts the elements in the list lst in >>> lst = [88, 12, 42, 11, 2]
ascending order. >>> lst.sort()
# [2, 11, 12, 42, 88]
>>> lst.sort(key=lambda x: str(x)[0])
# [11, 12, 2, 42, 88]
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 (atmost '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}
→ Complexity reduces productivity and focus. It’ll consume your precious time. Keep it simple!