<jupyter_start><jupyter_text>Beginning NumPy Install NumPy with:conda install numpyor /npip install numpy numpy arrays come in two flavors1. Vectors - 1D array2. Matrices - 2D array<jupyter_code>my_list = [1, 2, 3]
my_list
# Import the library for numpy
import numpy as np
# Making an array
arr = np.array(my_list)
arr
# Making a matrix - list of lists
my_mat = [[1, 2, 3], [4, 5, 6], [7, 8 ,9]]
my_mat
# Making a matrix - NumPy argument for matrices
np.array(my_mat)
# NumPy argument for defining a range of values
np.arange(0, 11, 2)
# Numpy argument for an array (or matrix) of zeroes
np.zeros(3)
np.zeros((5,5)) # first number m, second number n, or (down, across)
np.zeros((2,3))
# NumPy argument for an array (or matrix) of ones
np.ones(5)
np.ones((3,3))
# NumPy argument for an array of values covering a space by a particular spacing between points
np.linspace(1, 11, 11)
np.linspace(0, 5, 10)
# NumPy argument for an identity matrix
np.eye(5)
 
# NumPy argument for random number arrays - radnom sample of uniform distribution from 0 to 1
np.random.rand(5)
# Random Matrix - note the lack of tuple and only the dimension
np.random.rand(5,5)
# NumPy argument for random number arrays - radnom sample of normal (Gaussian) distribution centered around zero
np.random.randn(2)
# Random matrix - again only comma separated
np.random.randn(3,3)
# NumPy argument for random arrays - random integeres from a low to a high number
np.random.randint(1, 101, 10)<jupyter_output><empty_output><jupyter_text>Attributes and methods of an array<jupyter_code>arr = np.arange(25)
arr
ranarr = np.random.randint(0, 50, 10)
ranarr
# Reshaping an array: nameOfArray.reshape(m,n)
arr.reshape(5,5)
# Dimensions must agree, no auto-inserting zeroes
arr.reshape(5,10)
# Return the maximum value of an array
ranarr.max()
# Return the minimum value of an array
ranarr.min()
# Return the location of the maximum value (index value)
ranarr.argmax()
# Return the location of the minimum value (index value)
ranarr.argmin()
# Notice the reshape didn't stick
arr
# Just reassign it
arr = arr.reshape(5,5)
arr
# Find the shape of the matrix
arr.shape
arr.dtype
# can also avoid having to type "np.BlahBlahBlah"
from numpy.random import randint
# see it in action
randint(2,10)<jupyter_output><empty_output><jupyter_text>End of Lecture 17  NumPy Indexing and Selection<jupyter_code>import numpy as np
arr = np.arange(0,11)
arr
arr[8]
# Using slice notation
arr[1:5]
arr[0:5]
# Return everything up to a certain value
arr[:6]
# Return everything after a certain index - note that value = 5 is at index 6 not index 5
arr[5:]
# NumPy arrays differ from python list because of their ability to broadcast
arr[0:5] = 100
arr
arr = np.arange(0,11)
arr
sliceOfArr = arr[0:6]
sliceOfArr
sliceOfArr[:] = 99
sliceOfArr
arr<jupyter_output><empty_output><jupyter_text>Note that changing sliceOfArr = arr[0:6] to sliceOfArr[:] = 99 adjusted arr in the segments where sliceOfArr was defined, particularly 0:6.    As arr was a NumPy array, it means that it differs from a Python list and therefore, when some variable is defined from it, that variable is only a view of the original array.<jupyter_code>arrCopy = arr.copy()
arrCopy
arr == arrCopy
arrCopy[:] = 100
arr == arrCopy
# Create a 2D array
arr2D = np.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
arr2D
arr2D.shape<jupyter_output><empty_output><jupyter_text>Two formats for grabbing values from an array1. Double bracket2. Single bracket with a comma<jupyter_code># Double bracket format
arr2D
# Grab digit 5, it's in the first row first column
arr2D[0][0]
# Grab the value 40
arr2D
arr2D[2][1]
# Return digit 30
arr2D[1][2]
# Return the second row
arr2D[1]
# Single bracket with a comma
arr2D[:,1]
arr2D[:,0]
arr2D
# Grabbing submatrices
# Top right corner
arr2D[:2,1:]
# Bottom left
arr2D[1:,0:2]
arr2D[(0,2),:]
arr2D[:,(0,2)]<jupyter_output><empty_output><jupyter_text>Lecture 19<jupyter_code>arr = np.arange(1,11)
arr
boolArr = arr > 5
boolArr
arr[boolArr]
# Only returns results where boolean results are true
arr[arr > 5]
arr2D = np.arange(50).reshape(5,10)
arr2D
arr2D[1:3,3:5]
arr2D[:,(0,2,4)]<jupyter_output><empty_output><jupyter_text>Operations in NumPy<jupyter_code>import numpy as np
arr = np.arange(0,11)
arr
add = arr + arr
subtract = arr - arr
multiply = arr * arr
print('Result of adding:',add)
print('Result of subtracting:',subtract)
print('Result of multiplying:',multiply)
# element by element basis
addEW = arr + 100
subtractEW = arr - 100
multiplyEW = arr * 100
print('Result of elementwise adding:',addEW)
print('Result of elementwise subtracting:',subtractEW)
print('Result of elementwise multiplying:',multiplyEW)
# Numpy may only issues a warning instead of outputting an error on certain operations
1/0
# NOTE THE ERROR
arr / arr
# NOTE THE 'nan' and lack of error
1 / arr
# NOTE THE 'inf' and lack of error
arr ** 2<jupyter_output><empty_output><jupyter_text>Universal array functions<jupyter_code># Square root
np.sqrt(arr)
# Exponential
np.exp(arr)
# maximum value
np.max(arr)
arr.max()
np.sin(arr)
np.log(arr)
# runtime warning, but still runs and gives -inf<jupyter_output>/Users/mbair/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in log
  """Entry point for launching an IPython kernel.<jupyter_text>A series of exercises in NumPy  Import NumPy as np<jupyter_code>import numpy as np
#### Create an array of 10 zeros 
arr = np.zeros(10)
arr
#### Create an array of 10 ones
arr = np.ones(10)
arr
#### Create an array of 10 fives
arr = np.ones(10) * 5
arr
#### Create an array of the integers from 10 to 50
arr = np.arange(10,51)
arr
np.max(arr)
np.min(arr)
#### Create an array of all the even integers from 10 to 50
arr = np.arange(10,51,2)
arr
#### Create a 3x3 matrix with values ranging from 0 to 8
mat = np.arange(0,9).reshape(3,3)
mat
#### Create a 3x3 identity matrix
ident = np.eye(3)
ident
#### Use NumPy to generate a random number between 0 and 1
randInt = np.random.rand(1)
randInt<jupyter_output><empty_output><jupyter_text>Use NumPy to generate an array of 25 random numbers sampled from a standard normal distribution<jupyter_code>arr = np.random.randn(25,)
print(arr)
print(np.shape(arr))
#### Create the following matrix: 10x10 with elements ranging from 0-1 by 0.01 step
mat = np.linspace(0.01,1,100).reshape(10,10)
mat
#### Create an array of 20 linearly spaced points between 0 and 1:
arr = np.linspace(0,1,20)
arr<jupyter_output><empty_output><jupyter_text>Numpy Indexing and SelectionNow you will be given a few matrices, and be asked to replicate the resulting matrix outputs:<jupyter_code># 5x5 matrix from 1-25
mat = np.arange(1,26).reshape(5,5)
mat<jupyter_output><empty_output><jupyter_text>Now do the followingGet the sum of all the values in mat<jupyter_code>added = mat.sum()<jupyter_output><empty_output><jupyter_text>Get the standard deviation of the values in mat<jupyter_code>standDev = np.std(mat)
standDev
#### Get the sum of all the columns in mat
colSums = mat.sum(axis = 0)
colSums<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10617214043035107, 'fraction_numerical': 0.04161947904869762, 'mean_word_length': 5.291184327693678, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '459796', 'license': 'Unlicense', 'n_tokens_mistral': 2640, 'n_tokens_neox': 2458, 'n_words': 874, 'path': 'Python/NumPyBasics.ipynb', 'repo_name': 'ChristopherDaigle/Tutorials', 'size': '7064'}