diving into numpy arrays
play

Diving into NumPy arrays Justin Kiggins Product Manager DataCamp - PowerPoint PPT Presentation

DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Diving into NumPy arrays Justin Kiggins Product Manager DataCamp Python for MATLAB Users Calculations on NumPy arrays print(radius) Operation Operator [[1, 2, 3]] Addition +


  1. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Diving into NumPy arrays Justin Kiggins Product Manager

  2. DataCamp Python for MATLAB Users Calculations on NumPy arrays print(radius) Operation Operator [[1, 2, 3]] Addition + type(radius)) Subtraction - <class 'numpy.ndarray'> Multiplication * area = np.pi * (radius ** 2) Division / print(area) [3.14159265 12.56637061 Exponentiation ** 28.27433388]

  3. DataCamp Python for MATLAB Users Math between NumPy arrays type(revenue) <class 'numpy.ndarray'> print(revenue) [[100, 114, 96, 120]] type(cost) <class 'numpy.ndarray'> print(cost) [102. 104. 101. 102.] profit = revenue - cost print(profit) [-2. 10. -5. 18.]

  4. DataCamp Python for MATLAB Users Summarizing data in NumPy arrays import numpy as np np.mean(revenue) 107.5 Some useful NumPy functions: np.min() returns smallest value in array np.max() returns largest value in array np.sum() returns sum of all values in array

  5. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Let's practice!

  6. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Indexing NumPy arrays Justin Kiggins Product Manager

  7. DataCamp Python for MATLAB Users Indexing with ranges arr.shape (100,) slice = arr[10:15] slice.shape (5,)

  8. DataCamp Python for MATLAB Users Indexing multidimensional arrays image.shape (1920, 1200) window = image[100:150,1000:1100] window.shape (50,100)

  9. DataCamp Python for MATLAB Users Indexing with Boolean arrays data.shape (10000,) is_valid.dtype <np.bool> np.sum(is_valid) 8732 valid_data = data[is_valid] valid_data.shape (8732,)

  10. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Let's practice!

  11. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Lists Justin Kiggins Product Manager

  12. DataCamp Python for MATLAB Users What is a list? Simple Python structure for storing data Lists can hold anything Similar to MATLAB's cell array But only one-dimensional Indexing like NumPy arrays

  13. DataCamp Python for MATLAB Users Making lists my_list = [8, 6, 7, 5, 3, 0 ,9] print(my_list[2]) 7 print(my_list[-3:]) [3, 0, 9]

  14. DataCamp Python for MATLAB Users Making NumPy arrays from lists my_list = [8, 6, 7, 5, 3, 0 ,9] import numpy as np my_array = np.array(my_list) type(my_array) numpy.ndarray

  15. DataCamp Python for MATLAB Users Multidimensional NumPy arrays from lists of lists list_of_lists = [[2, 3], [9, 0], [1, 4]] import numpy as np arr = np.array(list_of_lists) print(arr) [[2, 3] [9, 0] [1, 4]] arr.shape (3, 2)

  16. DataCamp Python for MATLAB Users Differences between lists and NumPy arrays NumPy Arrays Lists All elements must be same type Can mix types (+) does element-wise addition (+) concatenates lists Multidimensional Single dimension Range & Boolean indexing Only Range indexing

  17. DataCamp Python for MATLAB Users When to use each Need to do math? NumPy array Storing complex structures? list Multidimensional data? NumPy array

  18. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Let's practice!

  19. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Customizing plots Justin Kiggins Product Manager

  20. DataCamp Python for MATLAB Users Custom colors import numpy as np import matplotlib.pyplot as plt # Generate data x = np.arange(0,2*np.pi,0.01) y = np.sin(x) # Plot data plt.plot(x,y,color='violet') plt.show()

  21. DataCamp Python for MATLAB Users Custom line styles import numpy as np import matplotlib.pyplot as plt # Generate data x = np.arange(0,2*np.pi,0.01) y = np.sin(x) y2 = np.cos(x) # Plot data plt.plot(x, y, color='violet') plt.plot(x, y2, color='indigo', linestyle=':') plt.show()

  22. DataCamp Python for MATLAB Users Adding a legend import numpy as np import matplotlib.pyplot as plt # Generate data x = np.arange(0,2*np.pi,0.01) y = np.sin(x) y2 = np.cos(x) # Plot data plt.plot(x, y, color='violet', label='sin(x)') plt.plot(x, y2, color='indigo', linestyle=':', label='cos(x)') # Add a legend plt.legend() plt.show()

  23. DataCamp Python for MATLAB Users Encoding data in marker color import numpy as np import matplotlib.pyplot as plt # Generate data x = np.random.randn(1000) y = np.random.randn(1000) z = x * y # Plot the data plt.scatter(x,y,c=z) plt.show()

  24. DataCamp Python for MATLAB Users Custom tick labels # Create the plot plt.plot(months,revenue) # Customize the ticks plt.yticks( [1,2,3], ['$1M','$2M','$3M']) plt.xticks( [0,1,2,3,4,5,6,7,8,9,10,11], list('JFMAMJJASOND'))

  25. DataCamp Python for MATLAB Users PYTHON FOR MATLAB USERS Let's plot some data!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend