Show Menu
Cheatography

COMP204 Final Cheat Sheet by

Binary Numbers

Binary Numbers

8 bits
1 byte
16 bits
word
32 bits
double word
always at least 1 byte
add one to first number in sequence to make negative, add zeros to get full byte

Machine Learning

-program adjusts itself automa­tically to fit data, end result is a program trained to achieve a given task
Supervised learni­ng-­given examples of input and desired outputs, predict outputs on future unseen inputs­(cl­ass­ifi­cation, regres­sion, time series)
Unsupe­rvised learni­ng-­creates a new repres­ent­ation of the input
Reinfo­rcement learni­ng-­lea­rning action to maximize payoff
Types of supervised learning tasks
1. classi­fic­ation- predict which predefined set of classes and example belongs to
2. regression - predict a real value
2. probab­ility estimation - estimate probab­ility of an event
Sensit­ivity = fraction of positive examples predicted to be positive
TP/(TP+FN)
Specif­icity= proportion of negative examples predicted negative
TN/(FP+TN)
False-­pos­itive rate(FPR)= negatives predicted to be positive
FP/(FP+TN)

Sets

Set
stores an unordered set of objects, CANT BE INDEXED, no duplic­ates, only immutable objects contained
myset = set([])
creates a set from a list
len(myset)
length of the set
if x in myset:
evaluates boolean condition
for element in myset:
iterate through set
for set1 = A and set2 = B, A&B = inters­ection
elements in common to A and B
A | B or A.union(B)
union of two sets
A-B
difference of sets, elements in A that are not in B
 
all these rules can be applied to multiple sets
 

Integer Sequences

range(­start, end, step)
(start default is zero, step default 1)
range(5)
0 1 2 3 4
range(3,8)
3 4 5 6 7
range(­len­(seq))
sequence of index of values in seq
range(­2,12,3)
2 5 8 11

Functions

def functi­onname( argume­nts):
defines a function of given name with given arguments
return
only returns a certain value or string generated by the function, doesn't print, exits at this value
list = [[0 for i in range(­ncols)] for j in range(­nro­wns)]
creates a two dimens­ional list of nrows and ncols filled with zeros

Operations on Lists

lst = []
creates empty list
lst.ap­pen­d(val)
adds item to list at end
lst.ex­ten­d(seq)
add sequence of items at end
lst.in­ser­t(idx, val)
inserts item at index
lst.re­mov­e(val)
remove first item in the list with value val
lst.po­p(idx)
remove and return item at index
lst.so­rt(­)/l­st.r­ev­erse()
sort/r­everse list in place
lst.mi­n()­/.max()
finds the min/max

matplo­tli­b.p­yplot

.plot(x,y, color)
plots the x values to x values in a certain color
.show()
shows the graph
.ylabe­l(name)
names y axis
.xlabe­l(name)
names x axis
.savefig( figname)
saves image under figname

OOP

class
defines attrib­ute­s(i­nfo­rmation we want to keep together) and methods( operations want to be able to perform on that data)
class some_c­las­s_name:
defines a class of some_c­las­s_name
def __int_­_(self, other attrib­utes):
defines and initia­lizes attributes
object.me­thod()
calls method on an object of that class
 

Bugs

Sytax errors
violation of a writing rule
Except­ion­s(r­untime)
syntax is fine, some other thing is occasi­onally flawed­:Ze­roD­ivi­sio­nEr­ror­(can't divide by zero), NameEr­ror­(can't access the variable), IndexE­rro­r(When the index you are attempting to access does not exist), TypeError( operation on non-co­mpa­tible type)
Logical (semantic) errors
code runs, but doesn't do what its supposed to
try:
does something that may cause an exception
except <So­meE­xce­pti­onT­ype­>:
do something to handle the exception
raise[ exception object ]
raises a certain, defined type of exception
try, except, else
try something, that could cause the exception, if its fine, go to else
finally:
adds statement that happens no matter what
for a, b in zip(list 1, list 2):
iterates over elements of two lists in parallel, yields a tuple with both iterations

File Input/­Output

f = open(m­yfile, 'x')
opens the file, x=r for reading only, x=w for writing only, x=a for appending, x=b for file in binary format, x=wr+ reading and writing and so on
.read(­size)
reads the entire file, returns a single string, size is optional number of characters
.readl­ine­s(size)
reads all lines and returns them as a list of strings
.rstrip()
returns a string with the end-of­-line charac­ter(\n) removed from the end of the string
.readl­ine()
reads single line from file, returns empty string if end of file
.close()
closes file
gzip.o­pen()
interface to read/write compressed files, .gz extension

Operations on Dictio­naries

dict = {}
sets up an empty dictionary
dict[key] = value
sets value of said at that key to value
dict[key]
calls the value at that key
del dict[key]
deletes a key from dict
dict.c­lear()
clears the dict of all keys
dict.u­pda­te(­dict2)
can update/add associ­ations from another
dict.k­eys­()/­dic­t.i­tem­s()­/di­ct.v­al­ues()
looks at either all keys, values, or all items in the dict
dict.p­op(key)
removes element associated to that key
dict.p­opi­tem()
removes key, value pair and returns it as a tuple
dict.g­et(key)
returns value at that key
dict.s­etd­efa­ult­(key)
either returns value of the key or creates the given key if not previously existent
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Python 3 Cheat Sheet by Finxter