11.Arrays
11.Arrays
to work with arrays in Python you will have to import a library, like the NumPy library.
It also has functions for working in domain of linear algebra, fourier transform, and matrices.
NumPy was created in 2005 by Travis Oliphant. It is an open source project and you can use it freely.
#NumPy is used to work with arrays. The array object in NumPy is called ndarray.
#We can create a NumPy ndarray object by using the array() function.Create an Numpy array
import numpy as np
a = np.array([1, 2, 3, 4, 5,6,7,8])
print(a)
print(type(a))
print(len(a))
[1 2 3 4 5 6 7 8]
<class 'numpy.ndarray'>
8
NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.
This is the main reason why NumPy is faster than lists. Also it is optimized to work with latest CPU architectures.
NumPy is a Python library and is written partially in Python, but most of the parts that require fast computation are written in C or C++.
The source code for NumPy is located at this github repository https://github.com/numpy/numpy
#o create an ndarray, we can pass a list, tuple or any array-like object into the array() method,
#and it will be converted into an ndarray:
#Use a tuple to create a NumPy array:
import numpy as np
a = np.array((1, 2, 3, 4, 5))
print(a)
print(type(a))
[1 2 3 4 5]
<class 'numpy.ndarray'>
42
<class 'numpy.ndarray'>
[1 2 3 4 5 6 7 8 9]
9
<class 'numpy.ndarray'>
ND Arrays
An array that has 1-D arrays as its elements is called a 2-D array.
[[1 2 3 7]
[4 5 6 9]]
2
#Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6:
import numpy as np
a = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(a)
print(len(a))
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]
2
print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
0
1
2
3
[[[[[1 2 3 4]]]]]
number of dimensions : 5
The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
Length of array
4
1
2
3
#Get third and fourth elements from the following array and add them.
import numpy as np
a = np.array([1, 2, 3, 4])
print("Sum Of Arrays")
print(a[0] + a[1])
print(a[0] + a[2])
print(a[0] + a[3])
print(a[1] + a[2])
print(a[1] + a[3])
print(a[2] + a[3])
Sum Of Arrays
3
4
5
5
6
7
import numpy as np
a = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print(a[0,0])
print('2nd element on 1st row: ', a[0, 1])
print(a[0,2])
print(a[0,3])
print(a[0,4])
print(a[1,0])
print(a[1,1])
print(a[1,2])
print(a[1,3])
print(a[1,4])
print(a)
1
2nd element on 1st row: 2
3
4
5
6
7
8
9
10
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
# 3D array
import numpy as np
a = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(a[0,0,0])
print(a[0,1,0])
print(a[0,1,2])
print(a[1,0,0])
print(a[1,1,1])
1
4
6
7
11
The first number represents the first dimension, which contains two arrays:
and:
The second number represents the second dimension, which also contains two arrays:
[1, 2, 3]
and:
[4, 5, 6]
[4, 5, 6]
The third number represents the third dimension, which contains three values:
import numpy as np
a = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('Last element from 2nd dim: ', a[1, -1])
Slicing arrays
Slicing in python means taking elements from one given index to another given index.
#2D array
a = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(a)
print(a[1, 1:4])
print(a[0:2, 2])
#From both elements, slice index 1 to index 4 (not included), this will return a 2-D array:
print(a[0:2, 1:4])
[2 3 4 5]
[1 2 3 4 5 6]
[2 3 4 5 6]
[4 5 6 7]
[5 6 7]
[3 4 5 6 7]
[1 2 3 4]
[1 2 3 4 5]
[5 6]
[2 4]
[1 3 5 7]
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
[7 8 9]
[3 8]
[[2 3 4]
[7 8 9]]
strings - used to represent text data, the text is given under quote marks. e.g. "ABCD"
complex - used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j
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
int64
<U6
#Change data type from float to integer by using 'i' as parameter value:
import numpy as np
arr = np.array([1.1, 2.1, 3.1])
print(arr)
newarr = arr.astype('i')
print(newarr)
print(newarr.dtype)
#Change data type from float to integer by using int as parameter value:
import numpy as np
arr = np.array([1.1, 2.1, 3.1])
newarr = arr.astype(int)
print(newarr)
print(newarr.dtype)
[1 2 3]
int64
The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a view of the original array.
The copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original array will not
affect the copy.
#Make a copy, change the original array, and display both arrays:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 42
print(arr)
print(x)
[42 2 3 4 5]
[1 2 3 4 5]
#Make a view, change the original array, and display both arrays:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr[0] = 42
print(arr)
print(x)
[42 2 3 4 5]
[42 2 3 4 5]
[31 2 3 4 5]
[31 2 3 4 5]
copies owns the data, and views does not own the data, but how can we check this?
Every NumPy array has the attribute base that returns None if the array owns the data.
#Print the value of the base attribute to check if an array owns it's data or not:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
x = arr.copy()
print(x)
y = arr.view()
print(y)
print(x.base)
print(y.base)
[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]
None
[1 2 3 4 5]
#NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements.
#Print the shape of a 2-D array:
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)
(2, 4)
#Create an array with 5 dimensions using ndmin using a vector with values 1,2,3,4
#and verify that last dimension has value 4:
import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr)
print('shape of array :', arr.shape)
[[[[[1 2 3 4]]]]]
shape of array : (1, 1, 1, 1, 4)
Reshaping arrays
By reshaping we can add or remove dimensions or change number of elements in each dimension.
#Convert the following 1-D array with 12 elements into a 2-D array.
#The outermost dimension will have 4 arrays, each with 3 elements:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)
newarr = arr.reshape(3, 4)
print(newarr)
newarr = arr.reshape(6, 2)
print(newarr)
newarr = arr.reshape(2, 6)
print(newarr)
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]]
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
#Convert the following 1-D array with 12 elements into a 3-D array.
#The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(2, 3, 2)
print(newarr)
newarr = arr.reshape(2, 2, 3)
print(newarr)
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
1
2
3
[1 2 3]
[4 5 6]
1
2
3
4
5
6
[[1 2 3]
[4 5 6]]
[[ 7 8 9]
[10 11 12]]
#iterate down to the scalars:
import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for x in arr:
for y in x:
for z in y:
print(z)
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
[1 2 3 4 5 6]
[[1 2 5 6]
[3 4 7 8]]
#We pass a sequence of arrays that we want to join to the stack() method along with the axis.
#If axis is not explicitly passed it is taken as 0.
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
print(arr)
[[1 4]
[2 5]
[3 6]]
[1 2 3 4 5 6]
#Stacking along Columns
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.vstack((arr1, arr2))
print(arr)
[[1 2 3]
[4 5 6]]
[[[1 4]
[2 5]
[3 6]]]
#SPLITING ARRAYS
#Split the array in 3 parts:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr)
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)
print(newarr[0])
print(newarr[1])
print(newarr[2])
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
newarr = np.array_split(arr, 3)
print(newarr)
#Split the 2-D array into three 2-D arrays along rows.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.array_split(arr, 3, axis=1)
print(newarr)
#Use the hsplit() method to split the 2-D array into three 2-D arrays along rows.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18]])
newarr = np.hsplit(arr, 3)
print(newarr)
#Searching Arrays
#Find the indexes where the value is 4:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 4, 4])
x = np.where(arr == 4)
print(x)
#Find the indexes where the value 7 should be inserted, starting from the right:
import numpy as np
arr = np.array([6, 7, 8, 9])
x= np.searchsorted(arr, 7, side='right')
print(x)