16. Numpy
16. Numpy
Numpy
2 Introduction
• At the core of the NumPy package, is the ndarray object. This encapsulates n-dimensional
arrays of homogeneous data types, with many operations being performed in compiled code
for performance.
• NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically).
Changing the size of an ndarray will create a new array and delete the original.
• The elements in a NumPy array are all required to be of the same data type, and thus will
be the same size in memory.
• NumPy arrays facilitate advanced mathematical and other types of operations on large num-
bers of data. Typically, such operations are executed more efficiently and with less code than
is possible using Python’s built-in sequences.
1
Vectorized code has many advantages, among which are:
– vectorized code is more concise and easier to read
– fewer lines of code generally means fewer bugs
– the code more closely resembles standard mathematical notation (making it easier, typ-
ically, to correctly code mathematical constructs)
– vectorization results in more “Pythonic” code. Without vectorization, our code would
be littered with inefficient and difficult to read for loops.
[1]: import numpy as np
[3]: print(arr)
[1 2 3 4 5]
[4]: type(arr)
[4]: numpy.ndarray
[5]: len(arr)
[5]: 5
[6]: arr.shape
[6]: (5,)
[8]: arr2d
[9]: arr2d.shape # array has 2 dimensions, and each dimension has 3 elements.
[9]: (2, 3)
[10]: arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) # 3d arrays
[11]: arr3d
[[1, 2, 3],
2
[4, 5, 6]]])
[12]: arr3d.ndim
[12]: 3
[13]: arr3d.shape # The shape of an array is the number of elements in each dimension.
[13]: (2, 2, 3)
3 Array Indexing
3.1 Access 1-D Arrays
[14]: arr = np.array([1, 2, 3, 4])
[15]: print(arr[0])
[16]: arr[1]
[16]: 2
[19]: arr[0][1]
[19]: 2
[21]: arr[0, 1, 2]
[21]: 6
3
[22]: arr[0][1][2]
[22]: 6
4 Array Slicing
• 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].
[25]: arr = np.array([1, 2, 3, 4, 5, 6, 7])
[26]: arr[1:5]
[28]: print(arr[-3:-1])
[5 6]
5 Step
• Use the step value to determine the step of the slicing:
[29]: arr = np.array([1, 2, 3, 4, 5, 6, 7])
[30]: print(arr[1:5:2])
[2 4]
4
5.1 Slicing 2-D Arrays
[31]: arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
[7 8 9]
[33]: arr[0:2, 2]
[35]: arr.dtype
[35]: dtype('int64')
6 Reshaping arrays
• Reshaping means changing the shape of an array.
• The shape of an array is the number of elements in each dimension.
• By reshaping we can add or remove dimensions or change number of elements in each dimen-
sion.
[38]: print(newarr)
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[40]: newarr
5
[40]: array([[[ 1, 2],
[ 3, 4],
[ 5, 6]],
[[ 7, 8],
[ 9, 10],
[11, 12]]])
[42]: print(newarr)
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
[44]: newarr2
7 Joining Arrays
• Joining means putting contents of two or more arrays in a single array.
• In SQL we join tables based on a key, whereas in NumPy we join arrays by axes.
• We pass a sequence of arrays that we want to join to the concatenate() function, along with
the axis.
6
• If axis is not explicitly passed, it is taken as 0.
[45]: arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
[47]: arr
[50]: arr1
[51]: arr2
[52]: arr_a1
[54]: arr_a0
7
[55]: arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
[57]: print(arr)
[[1 4]
[2 5]
[3 6]]
[59]: print(arr)
[1 2 3 4 5 6]
[61]: arr
[63]: arr
8
[66]: newarr
[67]: # If the array has less elements than required, it will adjust from the end␣
,→accordingly.
np.array_split(arr, 4)
[69]: np.array_split(arr, 3)
[70]: arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15],␣
,→[16, 17, 18]])
[71]: np.array_split(arr, 3)
[72]: # Split the 2-D array into three 2-D arrays along rows.
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15],␣
,→[16, 17, 18]])
9
array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]),
array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]
[74]: np.hsplit(arr, 3)
[76]: arr
10
9.2 linspace()
• Function returns evenly spaced numbers over a specified interval defined by the first two
arguments of the function (start and stop)
9.3 repeat()
• function repeats the elements of an array. The number of repetitions is specified by the second
argument repeats.
[78]: np.repeat(3,5)
9.4 random.randint()
• Returns random integers from the interval
[79]: np.random.randint(10,100, size=2)
There are many more functions available in numpy package. Explore them!!!
https://www.machinelearningplus.com/101-numpy-exercises-python/
11