0% found this document useful (0 votes)
6 views35 pages

NumPy vs. Python Lists Explained

Uploaded by

Leonel Messi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views35 pages

NumPy vs. Python Lists Explained

Uploaded by

Leonel Messi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introducing arrays

INTRODUCTION TO NUMPY

Izzy Weber
Core Curriculum Manager, DataCamp
NumPy and the Python ecosystem

INTRODUCTION TO NUMPY
NumPy arrays

INTRODUCTION TO NUMPY
Importing NumPy

import numpy as np

INTRODUCTION TO NUMPY
Creating 1D arrays from lists
python_list = [3, 2, 5, 8, 4, 9, 7, 6, 1]
array = [Link](python_list)
array

array([3, 2, 5, 8, 4, 9, 7, 6, 1])

type(array)

[Link]

INTRODUCTION TO NUMPY
Creating 2D arrays from lists
python_list_of_lists = [[3, 2, 5],
[9, 7, 1],
[4, 3, 6]]
[Link](python_list_of_lists)

array([[3, 2, 5],
[9, 7, 1],
[4, 3, 6]])

INTRODUCTION TO NUMPY
Python lists

Can contain many different data types

python_list = ["beep", False, 56, .945, [3, 2, 5]]

NumPy arrays

Can contain only a single data type

Use less space in memory

numpy_boolean_array = [[True, False], [True, True], [False, True]]

numpy_float_array = [1.9, 5.4, 8.8, 3.6, 3.2]

INTRODUCTION TO NUMPY
Creating arrays from scratch

There are many NumPy functions used to create arrays from scratch, including:

[Link]()

[Link]()

[Link]()

INTRODUCTION TO NUMPY
Creating arrays: [Link]()
[Link]((5, 3))

array([[0., 0., 0.],


[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])

INTRODUCTION TO NUMPY
Creating arrays: [Link]()
[Link]((2, 4))

array([[0.88524516, 0.85641352, 0.33463107, 0.53337117],


[0.69933362, 0.09295327, 0.93616428, 0.03601592]])

INTRODUCTION TO NUMPY
Creating arrays with [Link]()
[Link](-3, 4) from matplotlib import pyplot as plt
[Link]([Link](0, 7),

array([-3, -2, -1, 0, 1, 2, 3]) [Link](-3, 4))


[Link]()

[Link](4)

array([0, 1, 2, 3])

[Link](-3, 4, 3)

array([-3, 0, 3])

INTRODUCTION TO NUMPY
Let's practice!
INTRODUCTION TO NUMPY
Array dimensionality
INTRODUCTION TO NUMPY

Izzy Weber
Core Curriculum Manager, DataCamp
3D arrays
array_1_2D = [Link]([[1, 2], [5, 7]])
array_2_2D = [Link]([[8, 9], [5, 7]])
array_3_2D = [Link]([[1, 2], [5, 7]])
array_3D = [Link]([array_1_2D, array_2_2D, array_3_2D])

INTRODUCTION TO NUMPY
4D arrays
array_4D = [Link]([array_A_3D, array_B_3D, array_C_3D, array_D_3D, array_E_3D,
array_F_3D, array_G_3D, array_H_3D, array_I_3D])

INTRODUCTION TO NUMPY
Vector arrays

INTRODUCTION TO NUMPY
Matrix and tensor arrays

A matrix has two dimensions A tensor has three or more dimensions

INTRODUCTION TO NUMPY
Shapeshifting

Array attribute:

.shape

Array methods:

.flatten()

.reshape()

INTRODUCTION TO NUMPY
Finding an array's shape
array = [Link]((3, 5))
print(array)

array([[0., 0., 0., 0., 0.],


[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])

[Link]

(3, 5)

INTRODUCTION TO NUMPY
Rows and columns
In 2D arrays...

Rows are the first dimension

Columns are the second dimension

INTRODUCTION TO NUMPY
Flattening an array
array = [Link]([[1, 2], [5, 7], [6, 6]])
[Link]()

array([1, 2, 5, 7, 6, 6])

INTRODUCTION TO NUMPY
Reshaping an array
array = [Link]([[1, 2], [5, 7], [6, 6]])
[Link]((2, 3))

array([[1, 2, 5],
[7, 6, 6]])

[Link]((3, 3))

ValueError: cannot reshape array of size 6 into shape (3,3)

INTRODUCTION TO NUMPY
Let's practice!
INTRODUCTION TO NUMPY
NumPy data types
INTRODUCTION TO NUMPY

Izzy Weber
Core Curriculum Manager, DataCamp
NumPy vs. Python data types

Sample Python data types: Sample NumPy data types:

int np.int64

float np.int32

np.float64

np.float32

INTRODUCTION TO NUMPY
Bits and bytes
The number 10436 represented in binary is:

np.int32 can store 4,294,967,296 integers:

INTRODUCTION TO NUMPY
Bits and bytes
The number 10436 represented in binary is:

np.int32 can store 4,294,967,296 integers:

INTRODUCTION TO NUMPY
The .dtype attribute
[Link]([1.32, 5.78, 175.55]).dtype

dtype('float64')

INTRODUCTION TO NUMPY
Default data types
int_array = [Link]([[1, 2, 3], [4, 5, 6]])
int_array.dtype

dtype('int64')

INTRODUCTION TO NUMPY
String data
[Link](["Introduction", "to", "NumPy"]).dtype

dtype('<U12')

INTRODUCTION TO NUMPY
dtype as an argument
float32_array = [Link]([1.32, 5.78, 175.55], dtype=np.float32)
float32_array.dtype

dtype('float32')

INTRODUCTION TO NUMPY
Type conversion
boolean_array = [Link]([[True, False], [False, False]], dtype=np.bool_)
boolean_array.astype(np.int32)

array([[1, 0],
[0, 0]], dtype=int32)

INTRODUCTION TO NUMPY
Type coercion
[Link]([True, "Boop", 42, 42.42])

array(['True', 'Boop', '42', '42.42'], dtype='<U5')

INTRODUCTION TO NUMPY
Type coercion hierarchy
Adding a float to an array of integers will change all integers into floats:

[Link]([0, 42, 42.42]).dtype

dtype('float64')

Adding an integer to an array of booleans will change all booleans in to integers:

[Link]([True, False, 42]).dtype

dtype('int64')

INTRODUCTION TO NUMPY
Let's practice!
INTRODUCTION TO NUMPY

You might also like