inheritance is a vers u s has a
play

Inheritance : Is - a Vers u s Has - A W OR K IN G W ITH TH E C L - PowerPoint PPT Presentation

Inheritance : Is - a Vers u s Has - A 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 E x tending o u r DataShells Inheritance - A class that takes on a rib u tes from another , " parent "


  1. Inheritance : Is - a Vers u s Has - A 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. E x tending o u r DataShells Inheritance - A class that takes on a � rib u tes from another , " parent " class and adds some more of its o w n f u nctionalit y. WORKING WITH THE CLASS SYSTEM IN PYTHON

  3. T w o Dinosa u rs WORKING WITH THE CLASS SYSTEM IN PYTHON

  4. Is a and Has a Relationship WORKING WITH THE CLASS SYSTEM IN PYTHON

  5. Is a and Has a Relationship A Pterodact y l is - a Dinosa u r A T y rannosa u r u s is - a Dinosa u r Is a Pterodact y l a dinosa u r ? Yes , pterodact y l inherits from dinosa u r . Is a T y rannosa u r u s a pterodact y l ? No , b u t the y' re both dinosa u rs . Is a dinosa u r a pterodact y l ? No , so it doesn ' t w ork the other w a y, either . WORKING WITH THE CLASS SYSTEM IN PYTHON

  6. Inheriting a DataShell # new class which inherits from DataShell: StDevDataShell class StDevDataShell(DataShell): # now this class inherits all attributes and methods from DataShell pass WORKING WITH THE CLASS SYSTEM IN PYTHON

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

  8. Inheritance w ith DataShells W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON Vicki Bo y kis Sr . Data Scientist

  9. DataShell w ith Standard De v iation WORKING WITH THE CLASS SYSTEM IN PYTHON

  10. Changing the DataShell class DataShell: def __init__(self, filename): self.filename = filename def create_datashell(self): data_array = np.genfromtxt(self.filename, delimiter=',', dtype=None) self.array = data_array return self.array def show_shell(self): print(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 five figure summary(self col position): WORKING WITH THE CLASS SYSTEM IN PYTHON

  11. Allo w ing for a standard de v iation def get_stdev(self,col_position): column = self.array[1:,col_position].astype(np.float) stdev = np.ndarray.std(column,axis=0) return f"Standard Deviation of column {col_position}: {stdev}" WORKING WITH THE CLASS SYSTEM IN PYTHON

  12. Inheritance w ith DataShells class DataStDev(DataShell): def __init__(self,filename): DataShell.filename = filename def get_stdev(self,col_position): column = self.array[1:,col_position].astype(np.float) stdev = np.ndarray.std(column,axis=0) return f"Standard Deviation of column {col_position}: {stdev}" WORKING WITH THE CLASS SYSTEM IN PYTHON

  13. Calling o u r ne w DataShell Code to call it : car_data = 'mtcars.csv' my_st_dev_shell = DataStDev(car_data) my_st_dev_shell.create_datashell() my_st_dev_shell.get_stdev(1) 'Standard Deviation of column 1: 5.932029552301218' 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. Composition 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. Inheritance v ers u s Composition WORKING WITH THE CLASS SYSTEM IN PYTHON

  17. Composing w ith Animals WORKING WITH THE CLASS SYSTEM IN PYTHON

  18. Composition In a DataShell - 1 Fi v e - Fig u re S u mmar y Composition def five_figure_summary(self,col_position): statistics = stats.describe(self.array[1:,col_position].astype(np.float)) return f"Five-figure stats of column {col_position}: {statistics}" WORKING WITH THE CLASS SYSTEM IN PYTHON

  19. Composition In a DataShell - 2 Create DataShell Composition : def create_datashell(self): data_array = np.genfromtxt(self.filename, delimiter=',', dtype=None) self.array = data_array return self.array WORKING WITH THE CLASS SYSTEM IN PYTHON

  20. Composing w ith Pandas Create DataShell Composition : class DataShell: def __init__(self, filename): self.filename = filename def create_datashell(self): data_array = np.genfromtxt(self.filename, delimiter=',', dtype=None) self.array = data_array return self.array class DataShellComposed: def __init__(self, filename): self.filename = filename def create_datashell(self): self.df = pandas.read_csv() return self.df WORKING WITH THE CLASS SYSTEM IN PYTHON

  21. What does o u r ne w class look like ? car_data = 'mtcars.csv' my_data_shell = DataShellComposed(car_data) my_data_shell.create_datashell() print(type(my_data_shell.df)) <class 'pandas.core.frame.DataFrame'> WORKING WITH THE CLASS SYSTEM IN PYTHON

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

  23. Wrapping Up OOP 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

  24. Understanding Objects WORKING WITH THE CLASS SYSTEM IN PYTHON

  25. Understanding Classes and Instances of Classes . Classes : Made u p of methods and a � rib u tes Initiali z ed w ith an init constr u ctor method Has a self a � rib u te that ' s referring to the class ( or partic u lar instance of that class ) WORKING WITH THE CLASS SYSTEM IN PYTHON

  26. The DataShell WORKING WITH THE CLASS SYSTEM IN PYTHON

  27. The benefits of OOP WORKING WITH THE CLASS SYSTEM IN PYTHON

  28. Inheritance and Composition WORKING WITH THE CLASS SYSTEM IN PYTHON

  29. Ho w to get better at OOP 1) Read w ell - doc u mented codebases 2) Write y o u r o w n classes 3) Don ' t get disco u raged ! WORKING WITH THE CLASS SYSTEM IN PYTHON

  30. Final Steps W OR K IN G W ITH TH E C L ASS SYSTE M IN P YTH ON

Recommend


More recommend