[Machine Learning Cheat Sheet] Support Vector Machines
Based on Article: https://blog.finxter.com/supportvectormachinespython/
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? 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.
What‘s the most basic Python code example? • Oneliner:
## Dependencies 1. Create model using constructor of scikitlearn’s 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"], SVM predicts “art”
[4, 9, 3, "literature"],
[0, 1, 10, "art"], • student_1 (maths=8, language=1, and creativity=1) → SVM
[5, 7, 9, "art"]]) predicts “computer science”
• Final output of oneliner:
## 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)
„A Puzzle A Day to Learn, Code, and Play!“
Python Cheat Sheet: List Methods
“A puzzle a day to learn, code, and play” → Visit f inxter.com
Method Description Example
lst.append(x) Appends element x to the list lst. >>> l = []
>>> l.append(42)
>>> l.append(21)
[42, 21]
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
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, “if” 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 (multiline) 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 (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}
Subscribe to the 11x FREE Python Cheat Sheet Course:
https://blog.finxter.com/pythoncheatsheets/
Book:
Complexity appears in Project Lifecycle Cyclomatic Complexity Runtime Complexity
Complexity
• Project Lifecycle
“A whole, made up • Code Development
of parts—difficult to • Algorithmic Theory
analyze, understand, • Processes
or explain". • Social Networks
• Learning & Your Daily Life
→ Complexity reduces productivity and focus. It’ll consume your precious time. 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. Simple’s Better Than market fit
2. The Principle of Least Surprise Complex • Seek highvalue and
3. Don't Repeat Yourself 2. Small is Beautiful (Again) lowcost 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. SingleResponsibility Principle 5. Portability Over Efficiency amounts of time thinking about […] 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. Don’t Overengineer 12. Design Connected Programs Flow
Flow Tips for Coders
16. Don’t Overuse Indentation 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 3Step 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/pythoncheatsheets/