Summary

This document is a lecture on NumPy arrays, part of a Python course on machine learning at Halmstad University. It covers topics like universal functions, array methods, and array indexing.

Full Transcript

Python a Gateway to Machine Learning (DT8051) Lecture 11: NumPy Arrays Halmstad University October 2024 In this Session... Functions acting on NumPy arrays...

Python a Gateway to Machine Learning (DT8051) Lecture 11: NumPy Arrays Halmstad University October 2024 In this Session... Functions acting on NumPy arrays ndarray methods Array Views Boolean Arrays Array Indexing Functions acting on NumPy Arrays Functions that act element-wise on arrays are called universal functions In NumPy many universal functions are already defined which includes mathematical functions In : import numpy as np a = np.array([1.1,2.2,3.3,4.4,5.5]) print(f"a = {a}") print(f"np.cos(a) = {np.cos(a)}") #cosine print(f"np.tan(a) = {np.tan(a)}") #tangent print(f"np.exp(a) = {np.exp(a)}") #exponential function print(f"np.log(a) = {np.log(a)}") #logarithm print(f"np.ceil(a) = {np.ceil(a)}") #ceiling print(f"np.floor(a) = {np.floor(a)}") #floor print(f"np.round(a) = {np.round(a)}") #round a = [1.1 2.2 3.3 4.4 5.5] np.cos(a) = [ 0.45359612 -0.58850112 -0.98747977 -0.30733287 0.70866977] np.tan(a) = [ 1.96475966 -1.37382306 0.15974575 3.09632378 -0.99558405] np.exp(a) = [ 3.00416602 9.0250135 27.11263892 81.45086866 244.69193 226] np.log(a) = [0.09531018 0.78845736 1.19392247 1.48160454 1.70474809] np.ceil(a) = [2. 3. 4. 5. 6.] np.floor(a) = [1. 2. 3. 4. 5.] np.round(a) = [1. 2. 3. 4. 6.] Most common mathematical functions and constants (e.g. pi and e) exist in NumPy Creating Universal Functions If you have a function that acts on a single value in Python you can make a universal function from it You should use the vectorize higher order function to perform this Another possiblity is to use the vectorize decorator just before your function definition In : import numpy as np def normal_f(n): return n/2+1 vectorized_f = np.vectorize(normal_f) @np.vectorize #the vectorize decorator of the NumPy library def g(n): return n/2+1 a = [1,2,3,4,5] print(vectorized_f(a)) print(g(a)) [1.5 2. 2.5 3. 3.5] [1.5 2. 2.5 3. 3.5] Array Methods NumPy arrays (ndarray) have many useful methods These methods normally perform an operation along a specific dimension which is specified by the axis arugment If the axis argument is not given, these methods are applied to the whole array ndarray methods usually also have a function equivalent with the same name in the numpy module In : import numpy as np a = np.array([[1,2,3,4],[5,6,7,8]]) print(f"a = \n{a}") print(f"a.sum() = {a.sum()}") print(f"a.sum(axis=0) = {a.sum(axis=0)}") # takes the sum along the rows (first print(f"a.sum(axis=1) = {a.sum(axis=1)}") # takes the sum along the columns (se a = [[1 2 3 4] [5 6 7 8]] a.sum() = 36 a.sum(axis=0) = [ 6 8 10 12] a.sum(axis=1) = [10 26] Some Useful Array Methods Method Description min(a, Takes the minimum of the elements along the axis dimension if given axis=None) otherwise for the whole array max(a, Takes the maximum of the elements along the axis dimension if given axis=None) otherwise for the whole array mean(a, Takes the mean of the elements along the axis dimension if given axis=None) otherwise for the whole array std(a, Takes the standard deviation of the elements along the axis dimension if axis=None) given otherwise for the whole array prod(a, Calculates the product of the elements along the axis dimension if given axis=None) otherwise for the whole array sum(a, Calculates the sum of the elements along the axis dimension if given axis=None) otherwise for the whole array argmin(a, Gets the index of the minimum element along the axis dimension if given axis=None) otherwise for the whole array argmax(a, Gets the index of the maximum element along the axis dimension if axis=None) given otherwise for the whole array sort(a, Sorts the elements along the axis dimension (default is the last axis=-1) dimension) argsort(a, Gives the indices that sort the elements along the axis dimension axis=-1) (default is the last dimension) Linear Algebra Functions in NumPy NumPy has a submodule called linalg which is dedicated to linear algebra functions that you might need If you have a system of equations that could be define by an invertible square matrix $A$: $$ Ax = b$$ You can solve this system of equation using the solve functions In : import numpy as np A = np.array([[1,2,3],[6,5,4],[1,0,1]]) b = np.array([1,1,1]) print(f"A = \n{A}") print(f"b ={b}") print(f"x = {np.linalg.solve(A,b)}") A = [[1 2 3] [6 5 4] [1 0 1]] b =[1 1 1] x = [ 0.28571429 -0.71428571 0.71428571] Linear Algebra Functions in NumPy For non-invertible matrices you can use the lstsq function which gives the answer that minimizes the square error In : import numpy as np A = np.array([[1,3,5],[2,4,6],[7,8,9]]) b = np.array([3,2,1]) print(f"A = \n{A}") print(f"b ={b}") result = np.linalg.lstsq(A,b) x = result print(f"x = {x}") A = [[1 3 5] [2 4 6] [7 8 9]] b =[3 2 1] x = [-0.69453207 -0.01787592 0.65878023] In this function, $A$ could also be a rectangular matrix, and it works for all over-determined, under-determined, and well determined systems Linear Algebra Functions in NumPy Here is a list of useful functions in the linalg submodule |Function|Description| |:-------|:----------| |det(a)|Returns the determinant of the matrix a| |eig(a)|Returns the eigenvalues and eigen vectors of matrix a| |inv(a)|Retruns the inverse of matrix a| |pinv(a)|Retruns the pseudoinverse of matrix a| |norm(a)|Returns the norm of a matrix or a vector| |svd(a)|Performs singular value decomposition on the matrix a| |qr(a)|Performs the QR decomposition on matrix a| |cholesky(a)|Performs the Cholesky decomposition on matrix a| |solve(A,b)|Solves the system of equations dfined by A and b. A must be invertible.| |lstsq(A,b)|Solves the system of equations defined by A and b using the least squares method| To learn more about the linear algebra submodule of NumPy visit: https://numpy.org/doc/stable/reference/routines.linalg.html Array Views A view is an array that shares data with another equally large or bigger array The simplest example of a view is an array slice In : import numpy as np M = np.array([[1,2],[3,4]]) print(M) v = M[0,:] print(f"v = {v}") v = 5 print(f"v = {v}") print(M) [[1 2] [3 4]] v = [1 2] v = [5 2] [[5 2] [3 4]] By changing a value in the view array the value also changes in the original array This means that the memory is shared between the original array and and the view array Array Views You can check which array is view and which is the original by using the base attribute of arrays If base is equal to None then the array is original, otherwise base will point to the original array In : import numpy as np M = np.array([[1,2],[3,4]]) v = M print(f"M.base = {M.base}") print(f"v = {v}") print(f"v.base = \n{v.base}") M.base = None v = [3 4] v.base = [[1 2] [3 4]] You should be careful when slicing an array to keep in mind that chaning the view will change the original array Array Views Other operations that create view arrays are the reshape() method and the transpose operation In : import numpy as np M = np.array([[1,2],[3,4]]) t = M.T r = M.reshape(-1) print(f"t = \n{t}\n") print(f"t.base = \n{t.base}\n") print(f"r = {r}\n") print(f"r.base = \n{r.base}") t = [[1 3] [2 4]] t.base = [[1 2] [3 4]] r = [1 2 3 4] r.base = [[1 2] [3 4]] Array Views To avoid modifying the values in the original array you can make a copy of the view array to make it original You can use the copy method of ndarray to do so In : import numpy as np M = np.array([[1,2],[3,4]]) t = M.T.copy() print(f"M = \n{M}\n") print(f"t = \n{t}\n") print(f"t.base = {t.base}") M = [[1 2] [3 4]] t = [[1 3] [2 4]] t.base = None Comparison of Arrays The comparison operators ==, !=, , =, work for NumPy arrays These operators function for arrays of the same shape or if the arrays are broadcastable to a common shape (you will learn broadcasting later) This includes comparing an array to a single scalar In : a = np.array([[1,2],[3,4],[5,6]]) b = np.array([[1,2],[3,4],[7,8]]) print(f"a < b : \n{a < b}\n") print(f"a 2) & (b

Use Quizgecko on...
Browser
Browser