tuomasvaltanen

Untitled

Feb 24th, 2021 (edited)
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.98 KB | None | 0 0
  1. # lecture 2, numpy-module
  2.  
  3. # little Python warmup:
  4.  
  5. # pep8 is the standard usually followed
  6. # when it comes to Python coding style
  7. numbers = [3, 5, 6, 7, 8, 6, 5, 4, 6, 7]
  8.  
  9. print(numbers)
  10.  
  11. print(numbers[5])
  12.  
  13. print(numbers[0:3])
  14.  
  15. for n in numbers:
  16.     print(n)
  17.    
  18.  
  19. # a list of lists, or in numpy => Matrix
  20.  
  21. day_1 = [20, 24, 18, 23]
  22. day_2 = [15, 13, 12, 10]
  23. day_3 = [30, 32, 27, 28]
  24.  
  25. temperatures = [day_1, day_2, day_3]
  26.  
  27. print()
  28. print(temperatures[1][0])
  29. print()
  30.  
  31. for day in temperatures:
  32.     print("NEW DAY!")
  33.     for t in day:
  34.         print(t)
  35.  
  36. # NEW FILE!
  37.  
  38. import numpy as np
  39.  
  40. # if you have existing Python lists, or lists of lists
  41. # you can easily convert these into numpy arrays!
  42. numbers = [1, 2, 3, 400, 3]
  43.  
  44. data = np.array(numbers)
  45.  
  46. list1 = [1, 2, 3, 4]
  47. list2 = [8, 9, 10, 11]
  48. list3 = [15, 16, 17, 18]
  49.  
  50. combined = [list1, list2, list3]
  51.  
  52. matrix = np.array(combined)
  53.  
  54. # NEW FILE!
  55.  
  56. import numpy as np
  57.  
  58. # first parameter is included in the list
  59. # second parameter is excluded in the list
  60. # so last number is 20 - 1 = 19
  61. data = np.arange(0, 20)
  62.  
  63. data = np.arange(5, 101, 5)
  64.  
  65. ones = np.ones(10)
  66. zeroes = np.zeros(15)
  67.  
  68. # matrix of ones, 5 by 5
  69. one_matrix = np.ones((5, 5))
  70.  
  71. # 10 by 10 matrix = 10 * 10 = 100
  72. zero_matrix = np.zeros((10, 10))
  73.  
  74. # if you ever need an identity matrix quickly,
  75. # this is the way!
  76. id_matrix = np.eye(8)
  77.  
  78. # a list of evenly distributed values
  79. lin_data = np.linspace(0, 150, 11)
  80.  
  81. # NEW FILE!
  82.  
  83. import numpy as np
  84.  
  85. # vector, 30 values
  86. data = np.random.rand(30)
  87.  
  88. # 10 by 10 = 100 values
  89. data = np.random.rand(10, 10)
  90.  
  91. standard = np.random.randn(10)
  92.  
  93. number = np.random.randint(0, 100)
  94. numbers = np.random.randint(1, 1000, 50)
  95.  
  96. # reshape-function!
  97.  
  98. data = np.arange(1, 501)
  99. data = data.reshape(50, 10)
  100.  
  101. # the recommended way, chained functions.
  102. # first create a large vector
  103. # and immediately reshape it to matrix
  104. data = np.arange(1, 101).reshape(10, 10)
  105.  
  106. numbers = np.random.randint(1, 1000, 50).reshape(10, 5)
  107.  
  108. # if you ever need to know the dimensions of the data,
  109. # use the shape
  110. print(numbers.shape)
  111.  
  112. # these work mostly on vectors
  113. max_val = numbers.max()
  114. min_val = numbers.min()
  115.  
  116. max_val_pos = numbers.argmax()
  117. min_val_pos = numbers.argmin()
  118.  
  119. # NEW FILE!
  120.  
  121. import numpy as np
  122.  
  123. # basic index selections are identical to plain Python
  124. data = np.arange(50, 100)
  125.  
  126. single_value = data[15]
  127.  
  128. sublist1 = data[1:10]
  129.  
  130. sublist2 = data[:25]
  131.  
  132. sublist3 = data[25:]
  133.  
  134. matrix = np.arange(5, 46, 5).reshape(3, 3)
  135.  
  136. # first row and column
  137. value = matrix[0][0]
  138.  
  139. # second row, third column
  140. value = matrix[1,2]
  141.  
  142. # third row
  143. row = matrix[2]
  144.  
  145. # everything BEFORE second row,
  146. # everything AFTER second column
  147. section = matrix[:1, 1:]
  148.  
  149. matrix = np.arange(1, 26).reshape(5,5)
  150.  
  151. section = matrix[:3, 2:]
  152.  
  153. # taking the fifth column
  154. column = matrix[:, 4]
  155.  
  156. # NEW FILE!
  157.  
  158. import numpy as np
  159.  
  160. numbers = np.random.randint(1, 1000, 20)
  161.  
  162. big_numbers = numbers[numbers >= 500]
  163. odd_numbers = numbers[numbers % 2 != 0]
  164.  
  165. # broadcasting
  166. # remember to make a copy of your data
  167. # if you don't want operations to
  168. # affect original data as well!
  169. # this example shows what happens:
  170. # compare data and data_copy
  171. data = np.arange(1, 101)
  172. data_copy = data[0:10]
  173. data_copy[:] = -1
  174.  
  175. # version that uses copy()
  176. data = np.arange(1, 101)
  177. data_copy = data[0:10].copy()
  178. data_copy[:] = -1
  179.  
  180. # arithmetic operations
  181. data = np.arange(1, 101)
  182. combined = data + data
  183. reduced = data - data
  184. multiplied = data * data
  185.  
  186. increased = data + 100
  187. data = np.ones(10) * 25
  188.  
  189. # NEW FILE!
  190.  
  191. import numpy as np
  192.  
  193. data = np.arange(0, 11)
  194.  
  195. # 0/0 => NaN => Not a Number
  196. result = data / data
  197.  
  198. # 1 / 0 => inf => infinite
  199. other = 1 / data
  200.  
  201. # sum and standard deviation
  202. numbers = np.random.randint(1, 1000, 5)
  203. random_total = np.sum(numbers)
  204.  
  205. # low standard deviation means the values are close to the average value
  206. # high standard deviation menas the values are far apart from each other
  207. random_deviation = np.std(numbers)
  208.  
Add Comment
Please, Sign In to add comment