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

diving into numpy arrays
SMART_READER_LITE
LIVE PREVIEW

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 +


slide-1
SLIDE 1

DataCamp Python for MATLAB Users

Diving into NumPy arrays

PYTHON FOR MATLAB USERS

Justin Kiggins

Product Manager

slide-2
SLIDE 2

DataCamp Python for MATLAB Users

Calculations on NumPy arrays

Operation Operator Addition

+

Subtraction

  • Multiplication

*

Division

/

Exponentiation

** print(radius) [[1, 2, 3]] type(radius)) <class 'numpy.ndarray'> area = np.pi * (radius ** 2) print(area) [3.14159265 12.56637061 28.27433388]

slide-3
SLIDE 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.]

slide-4
SLIDE 4

DataCamp Python for MATLAB Users

Summarizing data in NumPy arrays

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

import numpy as np np.mean(revenue) 107.5

slide-5
SLIDE 5

DataCamp Python for MATLAB Users

Let's practice!

PYTHON FOR MATLAB USERS

slide-6
SLIDE 6

DataCamp Python for MATLAB Users

Indexing NumPy arrays

PYTHON FOR MATLAB USERS

Justin Kiggins

Product Manager

slide-7
SLIDE 7

DataCamp Python for MATLAB Users

Indexing with ranges

arr.shape (100,) slice = arr[10:15] slice.shape (5,)

slide-8
SLIDE 8

DataCamp Python for MATLAB Users

Indexing multidimensional arrays

image.shape (1920, 1200) window = image[100:150,1000:1100] window.shape (50,100)

slide-9
SLIDE 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,)

slide-10
SLIDE 10

DataCamp Python for MATLAB Users

Let's practice!

PYTHON FOR MATLAB USERS

slide-11
SLIDE 11

DataCamp Python for MATLAB Users

Lists

PYTHON FOR MATLAB USERS

Justin Kiggins

Product Manager

slide-12
SLIDE 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

slide-13
SLIDE 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]

slide-14
SLIDE 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

slide-15
SLIDE 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)

slide-16
SLIDE 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

slide-17
SLIDE 17

DataCamp Python for MATLAB Users

When to use each

Need to do math? NumPy array Storing complex structures? list Multidimensional data? NumPy array

slide-18
SLIDE 18

DataCamp Python for MATLAB Users

Let's practice!

PYTHON FOR MATLAB USERS

slide-19
SLIDE 19

DataCamp Python for MATLAB Users

Customizing plots

PYTHON FOR MATLAB USERS

Justin Kiggins

Product Manager

slide-20
SLIDE 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()

slide-21
SLIDE 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()

slide-22
SLIDE 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()

slide-23
SLIDE 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()

slide-24
SLIDE 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'))

slide-25
SLIDE 25

DataCamp Python for MATLAB Users

Let's plot some data!

PYTHON FOR MATLAB USERS