0% found this document useful (0 votes)
7 views

16. Numpy

Uploaded by

vijay the stark
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)
7 views

16. Numpy

Uploaded by

vijay the stark
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
You are on page 1/ 11

16.

Numpy

November 24, 2021

1 Python Virtual Environment


• At its core, the main purpose of Python virtual environments is to create an isolated envi-
ronment for Python projects.
• This means that each project can have its own dependencies, regardless of what dependencies
every other project has.
• Recommended to create virtual environment and install packages in it.
• To create virtual environment > python -m venv name_for_venv
• Better to create virtual environment in your project directory/folder.
• So, run above command from your project directory.
• To start virtual environment, in terminal or command prompt, first navigate to the directory
where you have created virtual environment.
• Then, run following command
– On windows: > name_for_venv\Scripts\activate
– On Linux: > source name_for_venv/bin/activate
• To stop virtual environment: > deactiavte

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.

2.1 Why is NumPy Fast?


• Vectorization describes the absence of any explicit looping, indexing, etc., in the code - these
things are taking place, of course, just “behind the scenes” in optimized, pre-compiled C code.

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

[2]: arr = np.array([1, 2, 3, 4, 5]) #pass list or tuple

[3]: print(arr)

[1 2 3 4 5]

[4]: type(arr)

[4]: numpy.ndarray

[5]: len(arr)

[5]: 5

[6]: arr.shape

[6]: (5,)

[7]: arr2d = np.array([[1, 2, 3], [4, 5, 6]]) # 2d arrays

[8]: arr2d

[8]: array([[1, 2, 3],


[4, 5, 6]])

[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

[11]: array([[[1, 2, 3],


[4, 5, 6]],

[[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)

• Access array elements using indexing.


• Also slicing can be performed on arrays.

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

3.2 Access 2-D Arrays


[17]: arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

[18]: print('2nd element on 1st row: ', arr[0, 1])

2nd element on 1st row: 2

[19]: arr[0][1]

[19]: 2

3.3 Access 3-D Arrays


[20]: arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

[21]: arr[0, 1, 2]

[21]: 6

3
[22]: arr[0][1][2]

[22]: 6

3.4 Negative Indexing


[23]: arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

[24]: print('Last element from 2nd dim: ', arr[1, -1])

Last element from 2nd dim: 10

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]

[26]: array([2, 3, 4, 5])

4.1 Negative Slicing


• Use the minus operator to refer to an index from the end
[27]: arr = np.array([1, 2, 3, 4, 5, 6, 7])

[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]])

[32]: print(arr[1, 1:4])

[7 8 9]

[33]: arr[0:2, 2]

[33]: array([3, 8])

[34]: arr[0:2, 1:4]

[34]: array([[2, 3, 4],


[7, 8, 9]])

[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.

6.1 Reshape From 1-D to 2-D


[36]: arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

[37]: newarr = arr.reshape(4, 3)

[38]: print(newarr)

[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]

6.2 Reshape From 1-D to 3-D


[39]: newarr = arr.reshape(2, 3, 2)

[40]: newarr

5
[40]: array([[[ 1, 2],
[ 3, 4],
[ 5, 6]],

[[ 7, 8],
[ 9, 10],
[11, 12]]])

6.3 Can We Reshape Into any Shape?


• Yes, as long as the elements required for reshaping are equal in both shapes.
• We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot
reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9 elements.

6.4 Unknown Dimension


• You are allowed to have one “unknown” dimension.
• Meaning that you do not have to specify an exact number for one of the dimensions in the
reshape method.
• Pass -1 as the value, and NumPy will calculate this number for you.
[41]: newarr = arr.reshape(2, 2, -1)

[42]: print(newarr)

[[[ 1 2 3]
[ 4 5 6]]

[[ 7 8 9]
[10 11 12]]]

6.5 Flattening the arrays


• Flattening array means converting a multidimensional array into a 1D array.
• We can use reshape(-1) to do this.
[43]: newarr2 = newarr.reshape(-1)

[44]: newarr2

[44]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

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])

[46]: arr = np.concatenate((arr1, arr2))

[47]: arr

[47]: array([1, 2, 3, 4, 5, 6])

[48]: arr1 = np.array([[1, 2], [3, 4]])


arr2 = np.array([[5, 6], [7, 8]])

[49]: arr_a1 = np.concatenate((arr1, arr2), axis=1)

[50]: arr1

[50]: array([[1, 2],


[3, 4]])

[51]: arr2

[51]: array([[5, 6],


[7, 8]])

[52]: arr_a1

[52]: array([[1, 2, 5, 6],


[3, 4, 7, 8]])

[53]: arr_a0 = np.concatenate((arr1, arr2), axis=0)

[54]: arr_a0

[54]: array([[1, 2],


[3, 4],
[5, 6],
[7, 8]])

7.1 Joining Arrays Using Stack Function


• Stacking is same as concatenation, the only difference is that stacking is done along a new
axis.
• We can concatenate two 1-D arrays along the second axis which would result in putting them
one over the other, ie. stacking.
• 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.

7
[55]: arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

[56]: arr = np.stack((arr1, arr2), axis=1)

[57]: print(arr)

[[1 4]
[2 5]
[3 6]]

7.1.1 Stacking Along Rows

[58]: arr = np.hstack((arr1, arr2))

[59]: print(arr)

[1 2 3 4 5 6]

7.1.2 Stacking Along Columns

[60]: arr = np.vstack((arr1, arr2))

[61]: arr

[61]: array([[1, 2, 3],


[4, 5, 6]])

7.1.3 Stacking Along Height (depth)

[62]: arr = np.dstack((arr1, arr2))

[63]: arr

[63]: array([[[1, 4],


[2, 5],
[3, 6]]])

8 Splitting NumPy Arrays


• We use array_split() for splitting arrays, we pass it the array we want to split and the
number of splits.
[64]: arr = np.array([1, 2, 3, 4, 5, 6])

[65]: newarr = np.array_split(arr, 3)

8
[66]: newarr

[66]: [array([1, 2]), array([3, 4]), array([5, 6])]

[67]: # If the array has less elements than required, it will adjust from the end␣
,→accordingly.

np.array_split(arr, 4)

[67]: [array([1, 2]), array([3, 4]), array([5]), array([6])]

8.1 Splitting 2-D Arrays


[68]: arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])

[69]: np.array_split(arr, 3)

[69]: [array([[1, 2],


[3, 4]]),
array([[5, 6],
[7, 8]]),
array([[ 9, 10],
[11, 12]])]

[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)

[71]: [array([[1, 2, 3],


[4, 5, 6]]),
array([[ 7, 8, 9],
[10, 11, 12]]),
array([[13, 14, 15],
[16, 17, 18]])]

[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]])

[73]: np.array_split(arr, 3, axis=1)

[73]: [array([[ 1],


[ 4],
[ 7],
[10],
[13],
[16]]),

9
array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]),
array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]

[74]: np.hsplit(arr, 3)

[74]: [array([[ 1],


[ 4],
[ 7],
[10],
[13],
[16]]),
array([[ 2],
[ 5],
[ 8],
[11],
[14],
[17]]),
array([[ 3],
[ 6],
[ 9],
[12],
[15],
[18]])]

9 Some functions in numpy


9.1 arange()
• Return evenly spaced values within a given interval.
[75]: arr = np.arange(10)

[76]: arr

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

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)

[77]: np.linspace(10,20, num=100)

[77]: array([10. , 10.1010101 , 10.2020202 , 10.3030303 , 10.4040404 ,


10.50505051, 10.60606061, 10.70707071, 10.80808081, 10.90909091,
11.01010101, 11.11111111, 11.21212121, 11.31313131, 11.41414141,
11.51515152, 11.61616162, 11.71717172, 11.81818182, 11.91919192,
12.02020202, 12.12121212, 12.22222222, 12.32323232, 12.42424242,
12.52525253, 12.62626263, 12.72727273, 12.82828283, 12.92929293,
13.03030303, 13.13131313, 13.23232323, 13.33333333, 13.43434343,
13.53535354, 13.63636364, 13.73737374, 13.83838384, 13.93939394,
14.04040404, 14.14141414, 14.24242424, 14.34343434, 14.44444444,
14.54545455, 14.64646465, 14.74747475, 14.84848485, 14.94949495,
15.05050505, 15.15151515, 15.25252525, 15.35353535, 15.45454545,
15.55555556, 15.65656566, 15.75757576, 15.85858586, 15.95959596,
16.06060606, 16.16161616, 16.26262626, 16.36363636, 16.46464646,
16.56565657, 16.66666667, 16.76767677, 16.86868687, 16.96969697,
17.07070707, 17.17171717, 17.27272727, 17.37373737, 17.47474747,
17.57575758, 17.67676768, 17.77777778, 17.87878788, 17.97979798,
18.08080808, 18.18181818, 18.28282828, 18.38383838, 18.48484848,
18.58585859, 18.68686869, 18.78787879, 18.88888889, 18.98989899,
19.09090909, 19.19191919, 19.29292929, 19.39393939, 19.49494949,
19.5959596 , 19.6969697 , 19.7979798 , 19.8989899 , 20. ])

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)

[78]: array([3, 3, 3, 3, 3])

9.4 random.randint()
• Returns random integers from the interval
[79]: np.random.randint(10,100, size=2)

[79]: array([62, 18])

There are many more functions available in numpy package. Explore them!!!
https://www.machinelearningplus.com/101-numpy-exercises-python/

11

You might also like