You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

07 Class.py 9.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. # ---
  2. # jupyter:
  3. # jupytext_format_version: '1.2'
  4. # kernelspec:
  5. # display_name: Python 3
  6. # language: python
  7. # name: python3
  8. # language_info:
  9. # codemirror_mode:
  10. # name: ipython
  11. # version: 3
  12. # file_extension: .py
  13. # mimetype: text/x-python
  14. # name: python
  15. # nbconvert_exporter: python
  16. # pygments_lexer: ipython3
  17. # version: 3.5.2
  18. # ---
  19. # All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures
  20. # # Classes
  21. # Variables, Lists, Dictionaries etc in python is a object. Without getting into the theory part of Object Oriented Programming, explanation of the concepts will be done along this tutorial.
  22. # A class is declared as follows
  23. # class class_name:
  24. #
  25. # Functions
  26. class FirstClass:
  27. pass
  28. # **pass** in python means do nothing.
  29. # Above, a class object named "FirstClass" is declared now consider a "egclass" which has all the characteristics of "FirstClass". So all you have to do is, equate the "egclass" to "FirstClass". In python jargon this is called as creating an instance. "egclass" is the instance of "FirstClass"
  30. egclass = FirstClass()
  31. type(egclass)
  32. type(FirstClass)
  33. # Now let us add some "functionality" to the class. So that our "FirstClass" is defined in a better way. A function inside a class is called as a "Method" of that class
  34. # Most of the classes will have a function named "\_\_init\_\_". These are called as magic methods. In this method you basically initialize the variables of that class or any other initial algorithms which is applicable to all methods is specified in this method. A variable inside a class is called an attribute.
  35. # These helps simplify the process of initializing a instance. For example,
  36. #
  37. # Without the use of magic method or \_\_init\_\_ which is otherwise called as constructors. One had to define a **init( )** method and call the **init( )** function.
  38. eg0 = FirstClass()
  39. eg0.init()
  40. # But when the constructor is defined the \_\_init\_\_ is called thus intializing the instance created.
  41. # We will make our "FirstClass" to accept two variables name and symbol.
  42. #
  43. # I will be explaining about the "self" in a while.
  44. class FirstClass:
  45. def __init__(self,name,symbol):
  46. self.name = name
  47. self.symbol = symbol
  48. # Now that we have defined a function and added the \_\_init\_\_ method. We can create a instance of FirstClass which now accepts two arguments.
  49. eg1 = FirstClass('one',1)
  50. eg2 = FirstClass('two',2)
  51. print(eg1.name, eg1.symbol)
  52. print(eg2.name, eg2.symbol)
  53. # **dir( )** function comes very handy in looking into what the class contains and what all method it offers
  54. dir(FirstClass)
  55. # **dir( )** of an instance also shows it's defined attributes.
  56. dir(eg1)
  57. # Changing the FirstClass function a bit,
  58. class FirstClass:
  59. def __init__(self,name,symbol):
  60. self.n = name
  61. self.s = symbol
  62. # Changing self.name and self.symbol to self.n and self.s respectively will yield,
  63. eg1 = FirstClass('one',1)
  64. eg2 = FirstClass('two',2)
  65. print(eg1.name, eg1.symbol)
  66. print(eg2.name, eg2.symbol)
  67. # AttributeError, Remember variables are nothing but attributes inside a class? So this means we have not given the correct attribute for the instance.
  68. dir(eg1)
  69. print(eg1.n, eg1.s)
  70. print(eg2.n, eg2.s)
  71. # So now we have solved the error. Now let us compare the two examples that we saw.
  72. #
  73. # When I declared self.name and self.symbol, there was no attribute error for eg1.name and eg1.symbol and when I declared self.n and self.s, there was no attribute error for eg1.n and eg1.s
  74. #
  75. # From the above we can conclude that self is nothing but the instance itself.
  76. #
  77. # Remember, self is not predefined it is userdefined. You can make use of anything you are comfortable with. But it has become a common practice to use self.
  78. class FirstClass:
  79. def __init__(asdf1234,name,symbol):
  80. asdf1234.n = name
  81. asdf1234.s = symbol
  82. eg1 = FirstClass('one',1)
  83. eg2 = FirstClass('two',2)
  84. print(eg1.n, eg1.s)
  85. print(eg2.n, eg2.s)
  86. # Since eg1 and eg2 are instances of FirstClass it need not necessarily be limited to FirstClass itself. It might extend itself by declaring other attributes without having the attribute to be declared inside the FirstClass.
  87. eg1.cube = 1
  88. eg2.cube = 8
  89. dir(eg1)
  90. # Just like global and local variables as we saw earlier, even classes have it's own types of variables.
  91. #
  92. # Class Attribute : attributes defined outside the method and is applicable to all the instances.
  93. #
  94. # Instance Attribute : attributes defined inside a method and is applicable to only that method and is unique to each instance.
  95. class FirstClass:
  96. test = 'test'
  97. def __init__(self,name,symbol):
  98. self.name = name
  99. self.symbol = symbol
  100. # Here test is a class attribute and name is a instance attribute.
  101. eg3 = FirstClass('Three',3)
  102. print(eg3.test, eg3.name)
  103. # Let us add some more methods to FirstClass.
  104. class FirstClass:
  105. def __init__(self,name,symbol):
  106. self.name = name
  107. self.symbol = symbol
  108. def square(self):
  109. return self.symbol * self.symbol
  110. def cube(self):
  111. return self.symbol * self.symbol * self.symbol
  112. def multiply(self, x):
  113. return self.symbol * x
  114. eg4 = FirstClass('Five',5)
  115. print eg4.square()
  116. print eg4.cube()
  117. eg4.multiply(2)
  118. # The above can also be written as,
  119. FirstClass.multiply(eg4,2)
  120. # ## Inheritance
  121. # There might be cases where a new class would have all the previous characteristics of an already defined class. So the new class can "inherit" the previous class and add it's own methods to it. This is called as inheritance.
  122. # Consider class SoftwareEngineer which has a method salary.
  123. class SoftwareEngineer:
  124. def __init__(self,name,age):
  125. self.name = name
  126. self.age = age
  127. def salary(self, value):
  128. self.money = value
  129. print(self.name,"earns",self.money)
  130. a = SoftwareEngineer('Kartik',26)
  131. a.salary(40000)
  132. dir(SoftwareEngineer)
  133. # Now consider another class Artist which tells us about the amount of money an artist earns and his artform.
  134. class Artist:
  135. def __init__(self,name,age):
  136. self.name = name
  137. self.age = age
  138. def money(self,value):
  139. self.money = value
  140. print(self.name,"earns",self.money)
  141. def artform(self, job):
  142. self.job = job
  143. print(self.name,"is a", self.job)
  144. b = Artist('Nitin',20)
  145. b.money(50000)
  146. b.artform('Musician')
  147. dir(Artist)
  148. # money method and salary method are the same. So we can generalize the method to salary and inherit the SoftwareEngineer class to Artist class. Now the artist class becomes,
  149. class Artist(SoftwareEngineer):
  150. def artform(self, job):
  151. self.job = job
  152. print(self.name,"is a", self.job)
  153. c = Artist('Nishanth',21)
  154. dir(Artist)
  155. c.salary(60000)
  156. c.artform('Dancer')
  157. # Suppose say while inheriting a particular method is not suitable for the new class. One can override this method by defining again that method with the same name inside the new class.
  158. class Artist(SoftwareEngineer):
  159. def artform(self, job):
  160. self.job = job
  161. print(self.name,"is a", self.job)
  162. def salary(self, value):
  163. self.money = value
  164. print(self.name,"earns",self.money)
  165. print("I am overriding the SoftwareEngineer class's salary method")
  166. c = Artist('Nishanth',21)
  167. c.salary(60000)
  168. c.artform('Dancer')
  169. # If not sure how many times methods will be called it will become difficult to declare so many variables to carry each result hence it is better to declare a list and append the result.
  170. class emptylist:
  171. def __init__(self):
  172. self.data = []
  173. def one(self,x):
  174. self.data.append(x)
  175. def two(self, x ):
  176. self.data.append(x**2)
  177. def three(self, x):
  178. self.data.append(x**3)
  179. xc = emptylist()
  180. xc.one(1)
  181. print xc.data
  182. # Since xc.data is a list direct list operations can also be performed.
  183. xc.data.append(8)
  184. print xc.data
  185. xc.two(3)
  186. print xc.data
  187. # If the number of input arguments varies from instance to instance asterisk can be used as shown.
  188. class NotSure:
  189. def __init__(self, *args):
  190. self.data = ''.join(list(args))
  191. yz = NotSure('I', 'Do' , 'Not', 'Know', 'What', 'To','Type')
  192. yz.data
  193. # # Where to go from here?
  194. # Practice alone can help you get the hang of python. Give your self problem statements and solve them. You can also sign up to any competitive coding platform for problem statements. The more you code the more you discover and the more you start appreciating the language.
  195. #
  196. #
  197. # Now that you have been introduced to python, You can try out the different python libraries in the field of your interest. I highly recommend you to check out this curated list of Python frameworks, libraries and software http://awesome-python.com
  198. #
  199. #
  200. # The official python documentation : https://docs.python.org/2/
  201. #
  202. #
  203. # You can also check out Python practice programs written by my friend, Kartik Kannapur. Github Repo : https://github.com/rajathkumarmp/Python-Lectures
  204. #
  205. #
  206. # Enjoy solving problem statements because life is short, you need python!
  207. #
  208. #
  209. # Peace.
  210. #
  211. #
  212. # Rajath Kumar M.P ( rajathkumar dot exe at gmail dot com)

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。