#!/usr/bin/env python # coding: utf-8 # ## What is Numpy # NumPy is a python library used for working with arrays. # # 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 stands for Numerical Python. # # Numpy is also incredibly fast, as it has bindings to C libraries. For more info on why you would want to use Arrays instead of lists, check out this great # ## Installation Instructions # # **It is highly recommended you install Python using the Anaconda distribution to make sure all underlying dependencies (such as Linear Algebra libraries) all sync up with the use of a conda install. If you have Anaconda, install NumPy by going to your terminal or command prompt and typing:** # # conda install numpy # # **If you do not have Anaconda and can not install it, please refer to [Numpy's official documentation on various installation instructions.](http://docs.scipy.org/doc/numpy-1.10.1/user/install.html)** # In[2]: my_list= [9,8,7] my_list # In[3]: import numpy as np np.array(my_list) # In[4]: list1=[1,2,3,4,5] np.array(list1) # In[5]: matrix=[[1,2,3],[4,5,6],[6,7,7]] print(matrix) print ('\n') print (np.array(matrix)) # ## Built-in Methods # # There are lots of built-in ways to generate Arrays # #### NumPy is used to work with arrays. The array object in NumPy is called ndarray. # In[6]: arr = np.array([1, 2, 3, 4, 5]) print(arr) print(type(arr)) # In[7]: np.arange(0,20) # In[8]: np.arange(0,50,3) # ### linspace # Return evenly spaced numbers over a specified interval. # In[9]: np.linspace (0,50,6) # In[10]: np.linspace (0,10,40) # ### zeros and ones # # Generate arrays of zeros or ones # In[11]: np.zeros(9) # In[12]: np.zeros((3,4)) # In[13]: np.ones(4) # In[14]: np.ones((3,4)) # ## eye # # Creates an identity matrix # In[15]: np.eye(5) # In[16]: np.eye(4,5) # # Random # ### What is a Random Number? # Random number does NOT mean a different number every time. Random means something that can not be predicted logically. # ### rand # Create an array of the given shape and populate it with # random samples from a uniform distribution # over ``[0, 1)``. # In[17]: np.random.rand(3) # In[18]: np.random.rand(4,4) # ### randn # # Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform: # In[19]: np.random.randn(2) # In[20]: np.random.randn(2,3) # ### randint # Return random integers from `low` (inclusive) to `high` (exclusive). # In[21]: np.random.randint(1,7) # In[22]: np.random.randint(1,100,10) # In[23]: np.random.randint (1,111) # ### Some of the Array Attributes and methods # # Have a look some of the main attributes and methods or an array # In[25]: arr = np.arange(50) ranarr=np.random.randint (0,100,10) # In[26]: arr # In[27]: ranarr # ## Reshape # Covert existing array into new shape with same data. # In[29]: arr.reshape(5,10) # In[31]: arr.reshape (2,25) # In[34]: arr1= np.arange(24) # In[35]: arr1 # In[37]: arr1.reshape(3,2,4) # ### max,min,argmax,argmin # # These are useful methods for finding max or min values. Or to find their index locations using argmin or argmax # In[39]: arr # arr.min() # In[41]: arr.max() # In[42]: ranarr # In[43]: ranarr.max() # In[44]: ranarr.argmax() # In[45]: ranarr.argmin() # ## Shape # The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it # In[46]: # Shape is an attribute that array have # Vector arr.shape # In[48]: # Notice the the two set of array arr.reshape(10,5) # In[49]: arr.reshape (1,50) # In[51]: arr.reshape(50,1).shape # In[52]: # You Can find the datatype of object in the array arr.dtype # A data type object (an instance of numpy.dtype class) describes how the bytes in the fixed-size block of memory corresponding to an array item should be interpreted. It describes the following aspects of the data: # # Type of the data (integer, float, Python object, etc.) # Size of the data (how many bytes is in e.g. the integer) # Byte order of the data (little-endian or big-endian) # If the data type is structured, an aggregate of other data types, (e.g., describing an array item consisting of an integer and a float), # what are the names of the “fields” of the structure, by which they can be accessed, # what is the data-type of each field, and # which part of the memory block each field takes. # If the data type is a sub-array, what is its shape and data type. # # https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.dtypes.html # ### Numpy Indexing and Selection # We will explore how we are going to grab a prticular elements of an array # In[54]: arr # ## Bracket Indexing and Selection # The simplest way to pick one or some elements of an array looks very similar to python lists: # In[55]: # Get the value at the index 24 arr[24] # In[58]: # Get the value in the range print (arr[4:24]) print ('\n') print (arr[0:10]) # ## Broadcasting # # Here Numpy is different from normal List in python since it is having ability to broadcast # In[61]: # Setting the value with index range (Broadcasting) arr[0:10]=88 # Show arr # In[62]: arr=np.arange (0,21) arr # In[63]: # Slicing is Very important slice_of_arr=arr[0:16] #Show slice slice_of_arr # In[66]: # Change the value of Slice slice_of_arr[:]=50 slice_of_arr # #### Note : Now the change is also there in original array # In[67]: arr # Data is not copied, it's a view of the original array! This avoids memory problems! # In[69]: # To get a copy , need to be explicit arr_copy=arr.copy () arr_copy # ## Indexing & Selection for 2D Array(Matrix) # Two-dimensional (2D) arrays are indexed by two subscripts, one for the row and one for the column. Each element in the 2D array must by the same type, either a primitive type or object type. # # # The general format is arr_2d[row][col] or arr_2d[row,col]. I recommend usually using the comma notation for clarity. # In[2]: import numpy as np matrix= np.array(([1,2,3],[4,5,6],[7,8,9])) # Show the value matrix # In[4]: # Indexing Rows # Grab 3nd row matrix[2] # In[5]: #row[1][2] #row[1,2] # Indexing Column # Grab 2nd column matrix[0:,1:2] # In[6]: # Format is arr_2d[row][col] or arr_2d[row,col] # Getting individual element value matrix[1][0] # In[7]: matrix[1][2] # In[8]: # Getting individual element value matrix[1,0] # In[9]: matrix # In[10]: # Matrix slicing # Shape (2,2) from top left corner matrix[0:2,0:2] # In[11]: # Matrix slicing # Shape (2,2) from top right corner matrix[0:2,1:3] # In[12]: # Matrix slicing # Shape (2,2) from bottom right corner matrix[1:,1:] # In[13]: arr=np.arange(25) # In[14]: b=arr.reshape(5,5) # In[15]: b # In[16]: b[3:,3:] # In[17]: # Grab all the elements in 4th column b[0:,3:4] # ## Fancing Indexing # Fancy indexing is conceptually simple: it means passing an array of indices to access multiple array elements at once. # # lets see an example # In[18]: # Set up matrix matrix1 = np.zeros ((11,11)) # Length of matrix matrix1_length = matrix1.shape[1] # In[20]: for i in range (matrix1_length): matrix1[i]+=i matrix1 # In[21]: # Fancy Indexing allows The following matrix1[[2,3,4,5]] # In[22]: matrix1[[5,6,7,8]] # ## More Indexing Help # Indexing a 2d matrix can be a bit confusing at first, especially when you start to add in step size. Try google image searching NumPy indexing to fins useful images, like this one: # # # ## Selection # # Let's briefly go over how to use brackets for selection based off of comparison operators. # In[24]: arr = np.arange(1,22) arr # In[25]: arr>4 # In[26]: bool_arr= arr>5 # In[27]: bool_arr # In[28]: arr[bool_arr] # In[29]: arr[arr>3] # In[30]: x=6 arr[arr>x] # ## NumPy Operations # # ### Arithmetic # Arithmetic Operations. Input arrays for performing arithmetic operations such as add(), subtract(), multiply(), and divide() must be either of the same shape or should conform to array broadcasting rules. # # You Can easily perform array with array arithematic , or scalar with array arithematic # # lets see some examples # # In[31]: arr= np.arange (0,25) # In[32]: arr # In[34]: arr+arr # In[35]: arr*arr # In[36]: arr-arr # In[147]: # Warning on division by zero, but not an error! # Just replaced with nan arr/arr # In[148]: # Also warning, but not an error instead infinity 1/arr # In[149]: arr**3 # ## Universal Array Functions # # Numpy comes with many [universal array functions](http://docs.scipy.org/doc/numpy/reference/ufuncs.html), which are essentially just mathematical operations you can use to perform the operation across the array. Let's show some common ones: # In[150]: # Taking square Roots np.sqrt (arr) # In[151]: np.max(arr) # In[152]: # Calculating exponential (e^) np.exp(arr) # In[153]: np.max(arr) #same as arr.max() # In[154]: np.sin(arr) # In[155]: np.log(arr) # ## Some Valueble insight of numpy lot more to explore # https://numpy.org/ # In[ ]: