0% found this document useful (0 votes)
10 views

Python Formula Sheet

This document serves as a comprehensive Python formula sheet covering various libraries such as Numpy and Pandas, along with built-in methods, data manipulation techniques, and examples of file I/O and image processing. It includes details on array creation, data analysis, string manipulation, and object-oriented programming concepts. The document is structured to provide quick references for common operations and functions within Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Python Formula Sheet

This document serves as a comprehensive Python formula sheet covering various libraries such as Numpy and Pandas, along with built-in methods, data manipulation techniques, and examples of file I/O and image processing. It includes details on array creation, data analysis, string manipulation, and object-oriented programming concepts. The document is structured to provide quick references for common operations and functions within Python programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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. np.array([[x],[y]]) - creates an array from a list of lists.
chr() - takes ASCII value, returns character np.arange(start, stop, step) - like range, but as np array.
ord(): returns ASCII value of char len() Returns the length of object np.linspace(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 np.zeros((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 np.random.rand() : array of random floats between 0–1
Pandas Data Analysis Example: np.random.rand()*n: array of random floats between 0–n
Strings(denoting string by ‘s’): Analysis for the Witcher’s quests np.random.randint(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 B.shape - 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 B.shape[0] and num of cols by B.shape[1]
def read_missions_file(file_name):
count() : returns count char (-1 if none)
try: B.sum(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 B.min(axis=0) \ B.max(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 np.mean(B) - get mean of the array (can use axis).
rfind():returns the last position of where it was found
Output: np.median(B) - get median of the array(can use axis).
Bounty and
split():Splits string at the specified separator, Expenses
returnsDuration
a list arr.astype(dtype) :Convert arr elements to type dtype
Kingdom arr.sort() :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 np.append(arr,values): Appends values to end of arr
title() Converts the first character Cintraof each
2500 word 2000to upper
3 np.insert(arr,2,values): Inserts vals into arr before index 2
Lists: #B Add a column of daily gain to the np.delete(arr,n,axis=0/1):Dels row/col on index n of arr
dataframe given as an i/p np.concatenate((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”]) np.argmin():Returns indices of the min vals along an axis
bounties["Daily
count() Returns the no. of elems with the specified value gain"] = np.argmax(): Returns indices of the max vals along an axis
d/(bounties[“Duration”]) np.percentile():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 np.cumsum():Returns cum-sum of elems along a given axis.
insert() Adds an element at the specified Bounty position
Expenses Duration np.bincount(im.flatten()): 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(bounties.sum(axis=0).iloc[0] -
update() Updates the dict with the specified key-value pairs
bounties.sum(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(df2.groupby(['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("data.csv") - read csv file into df.
Basic Operations:
df.set_index(): in order to change the row indices
df.head(i) - shows the first i rows.
df.tail(i) - shows the last i rows.
df.append(<dictionary/nparray>) -append to dataframe.
df.drop([’names’, ‘of’, ‘cols’]) - remove from dataframe.
df.mean() - get mean of values
df[df.Length > 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
df.iloc[row_index, col_index] - returns the value of the
dataframe at specific coordinates.
df.loc[row_name or index, col_name] - returns the value
of the dataframe at the specified row and column.
df.groupby(’col_name’) - combines repeating elements in
a column, use with mean() or max() usually.
Working With Multiple Files/Dataframes:
df.concat([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
pd.merge(df1,df2,on = ‘Column Name’ , how = ‘inner /outer’)
Or –‘ |’ , And – ‘&’
df.dot(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 = imageio.imread(img) # Opening the image file def load_crops(filename):
n1 = np.bincount(img1.flatten()) # 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(p.shape[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 = line.rstrip().split(',')
entropy += c # summing the entropy
return entropy row_headers.append(tokens[0])
B) Nearest Enlarge: table.append([int(x) for x in tokens[1:]])
def nearest_enlarge(img, a): except IOError:
img1 = imageio.imread(img) print("Failed parsing the file :(")
l = tuple([k*a for k in img1.shape()]) finally:
new_img = np.tile(0,l) # creating a new enlarged image if f != None :
for i in range(new_img.shape[0]):# going over each pixel f.close()
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 = im.copy()
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

You might also like