Python Numpy
Python Numpy
Python Numpy
DATA SCIENCE
AN OVERVIEW
Let's start at the top...
NumPy is a portmanteau from "Numerical Python"
>> [ 1, 2, 3 ] List
>> array( [ [ 1, 2, 3 ],
2 Dimensional Array
[ 4, 5, 6 ] ] )
Importing NumPy
To ensure you can access all of the amazing functionality
from within NumPy - you import it like so...
import numpy as np
Creating Arrays 1
Creating a 1-D array
np.array([1,2,3])
>> array([1, 2, 3])
np.array([[1,2,3],[4,5,6]])
>> array([[1, 2, 3],
[4, 5, 6]])
np.zeros(5)
>> array([0., 0., 0., 0., 0.])
np.ones(5)
>> array([1., 1., 1., 1., 1.])
Creating Arrays 2
Creating an array of evenly spaced values (start, stop, step)
np.arange(2,18,4)
>> array([ 2, 6, 10, 14])
np.random.random((2,3))
>> array([[0.99800537, 0.65104252, 0.15230364],
[0.25347108, 0.85345208, 0.44232692]])
np.full(4, 10)
>> array([10, 10, 10, 10]))
np.empty(2)
>> array([1.05116502e-311, 0.00000000e+000])
Creating Arrays 3
Creating an array of evenly spaced values (low, high, num-values)
np.linspace(1,3,5)
>> array([1. , 1.5, 2. , 2.5, 3. ])
np.random.randint(2,7,5)
>> array([3, 3, 4, 4, 6])
Summary Operations 1
Example 1-Dimensional Array
my_1d_array = np.random.randint(0,10,7)
>> array([7, 6, 4, 2, 9, 6, 7])
my_1d_array.max()
>> 9
my_1d_array.min()
>> 2
my_1d_array.mean()
>> 5.857142857142857
my_1d_array.sum()
>> 41
my_1d_array.std()
>> 2.099562636671296
Summary Operations 2
Example 2-Dimensional Array
my_2d_array = np.random.randint(0,10,(2,3))
>> array([[6, 1, 7],
[9, 0, 6]])
my_2d_array.max()
>> 9
my_2d_array.max(axis = 0)
>> array([9, 1, 7])
my_2d_array.max(axis = 1)
>> array([7, 9])
Math Operations 1
Example 1-Dimensional Array
a = np.array([1,2,3,4,5])
>> array([1, 2, 3, 4, 5])
a + 10
>> array([11, 12, 13, 14, 15])
a - 10
>> array([-9, -8, -7, -6, -5])
a * 10
>> array([10, 20, 30, 40, 50])
a / 10
>> array([0.1, 0.2, 0.3, 0.4, 0.5])
Math Operations 2
Example 1-Dimensional Array
a = np.array([-2,-1,0,1,2])
>> array([-2, -1, 0, 1, 2])
np.square(a)
>> array([4, 1, 0, 1, 4])
np.sqrt(a)
>> array([nan, nan, 0. , 1. , 1.41421356])
np.sin(a)
>> array([-0.9092, -0.8414, 0. , 0.8414, 0.9092])
np.cos(a)
>> array([-0.4161, 0.5403, 1. , 0.5403, -0.4161])
np.tan(a)
>> array([ 2.1850, -1.5574, 0. , 1.5574, -2.1850])
np.sign(a)
>> array([-1, -1, 0, 1, 1])
Math Operations 3
Example: Multiple Arrays
a = np.array([1,2,3])
b = np.array([4,5,6])
np.add(a,b) # or a + b
>> array([5, 7, 9])
np.subtract(a,b) # or a - b
>> array([-3, -3, -3])
np.multiply(a,b) # or a * b
>> array([ 4, 10, 18])
np.divide(a,b) # or a / b
>> array([0.25, 0.4 , 0.5 ])
np.dot(a,b)
>> 32
Array Comparison
Example: Multiple Arrays
a = np.array([1,2,3])
b = np.array([4,2,6])
a == b
>> array([False, True, False])
np.array_equal(a, b)
>> False
Accessing Elements 1
Example 1-Dimensional Array
a = np.array([1,2,3,4,5])
>> array([1, 2, 3, 4, 5])
a[0]
>> 1
Slicing
a[1:4]
>> array([2, 3, 4])
Last Element
a[-1]
>> 5
Accessing Elements 2
Example 2-Dimensional Array
a = np.array([[1,2,3],[4,5,6]])
>> array([[1, 2, 3],
[4, 5, 6]])
a[0]
>> array([1, 2, 3])
a[0][1]
>> 2
Re-shaping Arrays
Example 2-Dimensional Array
a = np.array([[1,2,3],[4,5,6]])
>> array([[1, 2, 3],
[4, 5, 6]])
a.reshape(3,2)
>> array([[1, 2],
[3, 4],
[5, 6]])
a = np.array([1,2,3])
>> array([1, 2, 3])
b = np.array([4,5,6])
>> array([4, 5, 6])
Horizontal Stack
np.hstack((a,b))
>> array([1, 2, 3, 4, 5, 6])
Vertical Stack
np.vstack((a,b))
>> array([[1, 2, 3],
[4, 5, 6]])
Array Features
Example 2-Dimensional Array
a = np.array([[1,2,3],[4,5,6]])
>> array([[1, 2, 3],
[4, 5, 6]])
a.shape
>> (2, 3)
Number of dimensions
a.ndim
>> 2
Number of elements
a.size
>> 6
A Planetary Example!
Here we are going to calculate the volumes of the eight planets
in our solar system - based upon their radius measurements.
After that - we're going to crank it up! Instead of just doing this
for eight planets, we will run this for one million made-up
planets.
Mercury 2439.7
Venus 6051.8
Earth 6371
Mars 3389.7
Jupiter 69911
Saturn 58232
Uranus 25362
Neptune 24622
>> array([ 2439.7, 6051.8, 6371. , 3389.7, 69911. , 58232. , 25362. , 24622. ])
Calculating the volumes!
Here we will create a new array called volumes and we will
apply the formula for calculating volume from radius to our
radii array!
radius
Formula
4 3
volume (sphere) = r
3
volumes = 4/3 * np.pi * radii**3
Because of the way NumPy works - it does this very, very quickly
Since we have over-written the radii array, we can run the same
volume calculation code below...
Before you hit run - how long do you think it will take to run this
calculation for one million planets?!