Python Formula Sheet Numpy (import numpy as np):
Python built-in methods: Axes: axis=1 is columns, axis=0 is rows.
abs()-Returns the absolute value of a number Shapes: represented as a tuple (x,y) where x is rows.
ascii()-Returns a readable version of an object. [Link]([[x],[y]]) - creates an array from a list of lists.
chr() - takes ASCII value, returns character [Link](start, stop, step) - like range, but as np array.
ord(): returns ASCII value of char len() Returns the length of object [Link](start, stop, num_points) - makes a “range”
max()/min():Returns max /min/next item in an iterator but with equally spaced points between start and stop.
reversed() Returns a reversed iterator [Link]((shape), dtype=np.uint8) - makes a zero
round():Rounds a numbers slice():Returns a slice object matrix of size defined by shape.
sum():Sums the items of an iterator [Link]() : array of random floats between 0–1
Pandas Data Analysis Example: [Link]()*n: array of random floats between 0–n
Strings(denoting string by ‘s’): Analysis for the Witcher’s quests [Link](n,size=(x,y)) : x*y array with random
Slicing: s[start:end(not including):step] – returns a copy of the str ints between 0–n-1
–
#A Open the csv file of missions and [Link] - returns size of matrix as tuple (x,y), get num
capitalize():Converts the first character to upper case
set kingdoms as indices
casefold():Converts string into lower case of rows by [Link][0] and num of cols by [Link][1]
def read_missions_file(file_name):
count() : returns count char (-1 if none)
try: [Link](axis=0) - sums the array, provide axis=0 or
df1 = pd.read_csv(file_name)
index()/find():Returns firstmost index, or -1 if not found. axis=1 to sum over columns or rows.
return df1.set_index("Kingdom")
join(): Joins the elements of an iterable to the end of the string [Link](axis=0) \ [Link](axis=0) - get min or max values,
except IOError: provide axis to get min/max column or row.
lower()/upper():Converts a string into lower/upper
raise IOError('An IOcase
error
replace():Returns str with a specoccurred')
val is replaced with a spec val [Link](B) - get mean of the array (can use axis).
rfind():returns the last position of where it was found
Output: [Link](B) - get median of the array(can use axis).
Bounty and
split():Splits string at the specified separator, Expenses
returnsDuration
a list [Link](dtype) :Convert arr elements to type dtype
Kingdom [Link]() :Sorts arr if axis is spec then sorts along that axis
splitlines():Splits the string at line breaks and returns
Temeria 1000 250
a list
5
strip():Returns a trimmed version of the 1500string 500 arr.T :Transposes arr
Redania 3
swapcase():Swaps cases, lower becomesKaedwen upper 500 and100 vice7 versa [Link](arr,values): Appends values to end of arr
title() Converts the first character Cintraof each
2500 word 2000to upper
3 [Link](arr,2,values): Inserts vals into arr before index 2
Lists: #B Add a column of daily gain to the [Link](arr,n,axis=0/1):Dels row/col on index n of arr
dataframe given as an i/p [Link]((arr1,arr2),axis=0/1) | Adds arr2 as
append() Adds an elem at the end defof the list
add_daily_gain_col(bounties):
clear() Removes all the elems from d = the list
(bounties["Bounty"]-
rows/cols to the end of arr1
copy() Returns a copy of the listbounties[“Expenses”]) [Link]():Returns indices of the min vals along an axis
bounties["Daily
count() Returns the no. of elems with the specified value gain"] = [Link](): Returns indices of the max vals along an axis
d/(bounties[“Duration”]) [Link]():Returns q-th percentile(s) of arr elems
extend() Add the elems of list (or any return iter), to end of the curr list
bounties
index() Returns the index of theOutput:
first elem with the spec value [Link]():Returns cum-sum of elems along a given axis.
insert() Adds an element at the specified Bounty position
Expenses Duration [Link]([Link]()): Returns a histogram of arr values
pop() Removes the element at the Daily gain
specified position Recursion and Memoisation Examples:
Kingdom Recursion Example: calculate the min distance between two strings
remove() Removes the first item with the specified value
Temeria 1000 250 5
reverse() Reverses the order of the list def edit_distance(str1, str2):
150.000000
if str1 == str2: # both strings are identical
sort() Sorts the list Redania 1500 500 3
return 0
Dictionaries: 333.333333
if len(str1) == 0 or len(str2) == 0: # the len of remaining str is the dist
Kaedwen 500 100 7
clear() Removes all the elems from the dict return len(str1) + len(str2)
57.142857
copy() Returns a copy of the dict if str1[-1] == str2[-1]:
Cintra 2500 2000 3
return edit_distance(str1[:-1], str2[:-1]) #if end characters are same
get() Returns the value of the166.666667
specified key
insert = edit_distance(str1, str2[:-1]) #insert is same as rem char from 2
items() Returns a list containing aC tuple
# for each
Sum rewards keybyvalue pair
earned
delete = edit_distance(str1[:-1], str2)#deleting a char from str1
keys() Returns a list containingsubtracting Expenseskeys
the dictionary's from bounties
replace = edit_distance(str1[:-1], str2[:-1])#repl same as 3rd if stat
def sum_rewards(bounties):
pop() Removes the element withreturn the specified key above
popitem() Removes the last inserted key-value pair return min(insert, replace, delete) + 1
int([Link](axis=0).iloc[0] -
update() Updates the dict with the specified key-value pairs
[Link](axis=0).iloc[1])
Output:in2650 Memoised Knapsack Optimisation:
values()Returns a list of all the values the dict def ks_mem_helper(offers,i,j,memo = None):
#D Kingdom which gives maximum
Note: keys are unique and immutable! if i == -1 or j == 0: # no more offers or no more capacity
Daily gain
Adding key-value pairs: def find_best_kingdom(bounties): return 0
d[key] = value - if key exists, the val will be overwritten.
df3 = add_daily_gain_col(bounties) if memo == None : # initialising a dict
return(df3[“Daily memo = {}
File IO Methods: ‘w’ : write, ‘a’:Gain”]).idxmax(axis
append ,‘r’ : read = 0) mem_key = (i,j) #unique key
if mem_key not in memo:
close() Closes the file Output: Redania
v_i = offers[i][0] #offered reward
readable() :Returns whether the#E: fileduration
streammaximizing the average
can be read or not w_i = offers[i][1] #offered weight
daily gain when the average is taken
readline():Returns one line fromoverthemissions
file of the chosen duration. if w_i > j: #offered weight > curr cap
readlines():Returns list of lines from the file
def find_best_duration(bounties): memo[mem_key] = ks_mem_helper(offers, i-1, j,memo)
writable():Returns whether the filedf2can be written to or not
= add_daily_gain_col(bounties) # Else either take the offer or reject the offer and return the max of two
else:
writelines():Writes a list of strings return
to the([Link](['Duration'])
file memo[mem_key] = max(ks_mem_helper(offers,i-1,j,memo),\
['Daily gain'].mean()).idxmax(axis = 0)
Immutability: Lists and dictionaries
Output:are mutable,
3 the values in v_i + ks_mem_helper(offers,i-1,j-w_i,memo))
them can be changed directly return memo[mem_key]
Strings and tuples are immutable - make a copy.
Pandas (import pandas as pd)
Pandas can have a different data type for each column.
Axes: axis=1 is columns, axis=0 is rows.
Creating a Dataframe:
df = DataFrame(my_dictionary) - create dataframe from
dictionary.
df = DataFrame(my_array, columns=['a', 'b', 'c']) -
creates dataframe from numpy array, specify column
names as a list.
df = pd.read_csv("[Link]") - read csv file into df.
Basic Operations:
df.set_index(): in order to change the row indices
[Link](i) - shows the first i rows.
[Link](i) - shows the last i rows.
[Link](<dictionary/nparray>) -append to dataframe.
[Link]([’names’, ‘of’, ‘cols’]) - remove from dataframe.
[Link]() - get mean of values
df[[Link] > 7] - extract rows by criteria to new df
Using a function on a column or row:
df[’my_col’] = df[’my_col’].apply(<function/lambda>)
Working With Data:
df[’my_col’].idxmax() - returns the index of the row with
the maximum value
[Link][row_index, col_index] - returns the value of the
dataframe at specific coordinates.
[Link][row_name or index, col_name] - returns the value
of the dataframe at the specified row and column.
[Link](’col_name’) - combines repeating elements in
a column, use with mean() or max() usually.
Working With Multiple Files/Dataframes:
[Link]([df1, df2], ignore_index = True) - merge two
dataframes together, if ignore_index is used, each line
will have its own index.
Outer join – Union of two tables horizontally
Inner join – Intersection of two tables horizontally
[Link](df1,df2,on = ‘Column Name’ , how = ‘inner /outer’)
Or –‘ |’ , And – ‘&’
[Link](df2): Compute the matrix mult between the df and df2
Image Processing Examples: File IO Example: Given CSV file with row names in first column, return a list of
A) Compute Entropy of an Image: row headers named row_headers, and a matrix (list of lists) named table holding
def compute_entropy(img): the numeric values.
img1 = [Link](img) # Opening the image file def load_crops(filename):
n1 = [Link]([Link]()) # creating a histogram row_headers = []
p = (n1[n1 != 0])/(256*256) # using masking to discard prob vals = 0 table = []
entropy = 0 try:
lg_p = np.log2(p) f = open(filename,'r')
for i in range([Link][0]): # going over each row in 1-D m^2*1 array for line in f:
c = -(p[i]) *(lg_p[i]) # computing entropy for one pixel
tokens = [Link]().split(',')
entropy += c # summing the entropy
return entropy row_headers.append(tokens[0])
B) Nearest Enlarge: [Link]([int(x) for x in tokens[1:]])
def nearest_enlarge(img, a): except IOError:
img1 = [Link](img) print("Failed parsing the file :(")
l = tuple([k*a for k in [Link]()]) finally:
new_img = [Link](0,l) # creating a new enlarged image if f != None :
for i in range(new_img.shape[0]):# going over each pixel [Link]()
for j in range(new_img.shape[1]): return row_headers, table
new_img[i,j] = img[(i//a),(j//a)]
return new_img Object Oriented Programming :
C) Censoring: Class – creates a class
def censor_rectangle(im, x, y, length, k): # with copying image Abstraction ->Encapsulation->Inheritance->Polymorphism
im2 = [Link]()
for i in range(x, x + length, k):
Built-in methods:
for j in range(y, y + length, k): def __init__(self, param1, param2): - constructor, takes
im2[i:i + k, j:j + k] = int(im[i:i + k, j:j + k].mean()) in vals from the outside and assigns them to the object.
return im2 isinstance(x, my_class) - checks if x is of type my_class
Note – Use np.int_() on pixels in order to get the “usually or inherits from it.
expected” sum of two pixel values