Numpy
Numpy
NumPy
• In Python we have lists that serve the purpose of arrays, but they
are slow to process.
• NumPy aims to provide an array object that is up to 50x faster than
traditional Python lists.
• The array object in NumPy is called ndarray, it provides a lot of
supporting functions that make working with ndarray very easy.
• Arrays are very frequently used in data science, where speed and
resources are very important.
• Data Science: is a branch of computer science where we
study how to store, use and analyze data for deriving
information from it.
import numpy as np
print(arr)
Create a NumPy ndarray Object
print(arr)
print(type(arr))
• Use a tuple to create a NumPy array:
import numpy as np
print(arr)
Creating a 0-D array
import numpy as np
arr = [Link](42)
print(arr)
1-D Arrays
import numpy as np
print(arr)
2-D Arrays
import numpy as np
print(arr)
Check Number of Dimensions
import numpy as np
a = [Link](42)
b = [Link]([1, 2, 3, 4, 5])
c = [Link]([[1, 2, 3], [4, 5, 6]])
d = [Link]([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print([Link])
print([Link])
print([Link])
print([Link])
NumPy Array Indexing
Access Array Elements
• Array indexing is the same as accessing an array element.
• You can access an array element by referring to its index
number.
• The indexes in NumPy arrays start with 0, meaning that the
first element has index 0, and the second has index 1 etc.
import numpy as np
print(arr[0])
import numpy as np
print(arr[2] + arr[3])
Access 2-D Arrays
• To access elements from 2-D arrays we can use comma
separated integers representing the dimension and the index
of the element.
• Think of 2-D arrays like a table with rows and columns, where
the dimension represents the row and the index represents
the column.
• Access the element on the first row, second column:
import numpy as np
import numpy as np
Slicing arrays
• Slicing in python means taking elements from one given
index to another given index.
• We pass slice instead of index like this: [start:end].
• We can also define the step, like this: [start:end:step].
• If we don't pass start its considered 0
• If we don't pass end its considered length of array in that
dimension
• If we don't pass step its considered 1
Slice elements from index 1 to index 5 from the following
array:
• import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
• The result includes the start index, but excludes the end
index.
Slice elements from index 4 to the end of the array:
• import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[4:])
Slice elements from the beginning to index 4 (not included):
• import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[:4])
Negative Slicing
Use the minus operator to refer to an index from the end.
Slice from the index 3 from the end to index 1 from the end:
import numpy as np
print(arr[-3:-1])
Return every other element from index 1 to index 5:
• import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5:2])
Return every other element from the entire array:
• import numpy as np
arr = [Link]([1, 2, 3, 4, 5, 6, 7])
print(arr[::2])
NumPy Data Types
Data Types in Python
• strings - used to represent text data, the text is given under
quote marks. e.g. "ABCD"
• integer - used to represent integer numbers. e.g. -1, -2, -3
• float - used to represent real numbers. e.g. 1.2, 42.42
• boolean - used to represent True or False.
• complex - used to represent complex numbers. e.g. 1.0 +
2.0j, 1.5 + 2.5j
Data Types in NumPy
NumPy has some extra data types, and refer to data types with one
character, like i for integers, u for unsigned integers etc.
Below is a list of all data types in NumPy and the characters used to
represent them.
• i - integer
• b - boolean
• u - unsigned integer
• f - float
• c - complex float
• m - timedelta
• M - datetime
• O - object
• S - string
• U - unicode string
V - fixed chunk of memory for other type ( void )
Checking the Data Type of an Array
• The NumPy array object has a property called dtype that
returns the data type of the array:
print([Link])
• Get the data type of an array containing strings:
import numpy as np
print([Link])