deep di v e on classes
play

Deep - Di v e on Classes W OR K IN G W ITH TH E C L ASS SYSTE M - PowerPoint PPT Presentation

Deep - Di v e on Classes W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON Vicki Bo y kis Senior Data Scientist Working w ith DataFrames WORKING WITH THE CLASS SYSTEM IN PYTHON Introd u cing the DataShell WORKING WITH THE CLASS SYSTEM IN


  1. Deep - Di v e on Classes W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON Vicki Bo y kis Senior Data Scientist

  2. Working w ith DataFrames WORKING WITH THE CLASS SYSTEM IN PYTHON

  3. Introd u cing the DataShell WORKING WITH THE CLASS SYSTEM IN PYTHON

  4. F u ll Class class DataShell: def __init__(self, filename): self.filename = filename def create_datashell(self): self.array = np.genfromtxt(self.filename, delimiter=',', dtype=None) return self.array def rename_column(self, old_colname, new_colname): for index, value in enumerate(self.array[0]): if value == old_colname.encode('UTF-8'): self.array[0][index] = new_colname return self.array def show_shell(self): print(self.array) def five_figure_summary(self, col_pos): statistics = stats.describe(self.array[1:,col_pos].astype(np.float)) return f"Five-figure stats of column {col_position}: {statistics}" WORKING WITH THE CLASS SYSTEM IN PYTHON

  5. Creating a Ne w Class class DataShell: pass WORKING WITH THE CLASS SYSTEM IN PYTHON

  6. Parts of a Class in Detail class DataShell: def __init__(self, filename): self.filename = filename # attribute def create_datashell(self): self.array = np.genfromtxt(self.filename, delimiter=',', dtype=None) return self.array def rename_column(self, old_colname, new_colname): for index, value in enumerate(self.array[0]): if value == old_colname.encode('UTF-8'): self.array[0][index] = new_colname return self.array WORKING WITH THE CLASS SYSTEM IN PYTHON

  7. Ho w to Call the Class class DataShell(): # some methods and attributes here our_data_shell = DataShell('mtcars.csv') our_data_shell <__main__.DataShell at 0x7f58df61de80> WORKING WITH THE CLASS SYSTEM IN PYTHON

  8. Let ' s practice ! W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON

  9. __ Init __ iali z ing a class W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON Vicki Bo y kis Senior Data Scientist

  10. Understanding Constr u ctors w ith Init Empt y Constr u ctor class Dinosaur: def __init__(self): pass Constr u ctor w ith A � rib u tes class Dinosaur: def __init__(self): self.tail = 'Yes' WORKING WITH THE CLASS SYSTEM IN PYTHON

  11. Init and O u r DataShell # Modeled on Pandas read_csv pandas.read_csv('mtcars.csv') Creating the DataShell w ith a Constr u ctor class DataShell: def __init__(self, filename): self.filename = filename WORKING WITH THE CLASS SYSTEM IN PYTHON

  12. Understanding Self class DataShell: def __init__(self, filename): self.filename = filename Initiali z ing the Car DataShell car_data_shell = DataShell('mtcars.csv') def __init__(car_data_shell, 'mtcars.csv'): self.filename = filename Initiali z ing the ForestFire DataShell forest_fires_data_shell = DataShell('forestfires.csv') def __init__(forest_fires_data_shell, 'forestfires.csv'): self.filename = filename WORKING WITH THE CLASS SYSTEM IN PYTHON

  13. Self is not a P y thon ke yw ord b u t w e u se it like one # Printing out the keyword list import keyword print(keyword.kwlist) ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'd 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] # Using this as an object referece def __init__(this, filename): this.filename = filename WORKING WITH THE CLASS SYSTEM IN PYTHON

  14. Let ' s practice ! W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON

  15. Class and Instance Variables W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON Vicki Bo y kis Senior Data Scientist

  16. Class Variables # Our Dinosaur Class class Dinosaur(): eyes = 2 def __init__(self, teeth): self.teeth = teeth # Building a Stegosaurus stegosaurus = Dinosaur(40) stegosaurus.teeth = 40 Stegosaurus.teeth 40 Stegosaurus.eyes 2 WORKING WITH THE CLASS SYSTEM IN PYTHON

  17. Instance Variables # Our Dinosaur Class class Dinosaur(): eyes = 2 def __init__(self, teeth): self.teeth = teeth # Building a Triceratops Triceratops = Dinosaur(5) Triceratops.teeth = 5 Triceratops.teeth 5 Triceratops.eyes 2 WORKING WITH THE CLASS SYSTEM IN PYTHON

  18. Passing in parameters to objects class DataShell(object): def __init__(self, filename): self.filename = filename Res u lts : my_data_shell = DataShell('mtcars.csv') print(my_data_shell.filename) 'mtcars.csv' WORKING WITH THE CLASS SYSTEM IN PYTHON

  19. Let ' s practice ! W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON

  20. Methods in Classes W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON Vicki Bo y kis Senior Data Scientist

  21. Methods class DataShell: # init method def __init__(self, filename): self.filename = filename # create_datashell method def create_datashell(self): self.array = np.genfromtxt(self.filename, delimiter=',', dtype=None) return self.array # rename_column method def rename_column(self, old_colname, new_colname): for index, value in enumerate(self.array[0]): if value == old_colname.encode('UTF-8'): self.array[0][index] = new_colname return self.array WORKING WITH THE CLASS SYSTEM IN PYTHON

  22. Initiali z ing Methods in Classes def create_datashell(self): self.array = np.genfromtxt(self.filename, delimiter=',', dtype=None) return self.array WORKING WITH THE CLASS SYSTEM IN PYTHON

  23. Methods w ith other parameters def rename_column(self, old_colname, new_colname): for index, value in enumerate(self.array[0]): if value == old_colname.encode('UTF-8'): self.array[0][index] = new_colname return self.array WORKING WITH THE CLASS SYSTEM IN PYTHON

  24. Ho w to call methods # Calling without passing in a parameters myDatashell.create_datashell() # Calling by passing in a parameter myDatashell.rename_column('cyl','cylinders') WORKING WITH THE CLASS SYSTEM IN PYTHON

  25. Let ' s practice ! W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON

Recommend


More recommend