# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np

my_list1 = [1,2,3,4]

# First we imported numpy as np to save keystrokes.  We created a list and then assigned it to an array by using the 'np.array()' method to more easily use tab or shift tab for more info.

my_array1 = np.array(my_list1)

# Calling my_array1 then produces our list in parens.

my_array1

# We can combine lists in our array similarly as shown below.

my_list2 = [11,22,33,44]

my_lists =  [my_list1, my_list2]

my_array2 = np.array(my_lists)

my_array2

# .shape method is to see rows and columns of the arrays and dtype is data type.  It is important to note that when using .zeros method you should use just float or int without adding the 32 or 64 as the data type shows.  

my_array2.shape

my_array2.dtype

my_zeros = np.zeros((3,3),int)
my_zeros

my_zeros.dtype

np.eye(6)

np.arange(1,15,2)

5/2

arr1 = np.array([[1,2,3,4], [8,9,10,11]])

arr1 * arr1

#

import numpy as np

arr = np.arange(0,11)

arr

arr[8]

arr[1:5]

arr

slice_of_arr = arr[0:6]

slice_of_arr

# Important note about arrays.  There is only one copy unless you create another, so anything you do changes the original copy.
# To make a copy do the following:

arr_copy = arr.copy()

arr_copy

arr_2d = np.array(([5,10,15],[20,25,30],[35,40,45]))

arr_2d

arr_2d[1]

arr_2d[1][0]

# Slicing the 3x3 is also possible using slices divided by a comma shown below:  

arr_2d[:2,1:]

# The :2 takes the 0 - 1 lists of  |5, 10, 15,|and |20, 25, 30] and the 1: gives us the 1 and 2 elements of each.  Remember that everything starts with zero.

# Below we create another array reshaped into a matrix.  To transform the matrix (i.e. make the colomn size the row size and vice versa), we use the transform method 'T'.

arr = np.arange(50).reshape((10,5))

arr

arr.T

# We can do dot multiplication which is done with matrices in mathematics by using 'np.dot'.  In dot multiplication we multiply the first value in the first row of our first matrix by the first value in the first column of our second matrix and continue until the entire first row of our first matrix is multiplied by the entire first column of our second matrix.  Then we sum the products for our matrix product.  We then do the same for the 2nd row of the first matrix and the 2nd column of the 2nd matrix and so on until completion.  This gives us the standard mathematics formula of [mxn X nxp = mxp.]  This means a 1x3 X 3x1 gives us a 1x1, but a 3x1 X 1x3 gives us a 3x3.  Below is our original array multiplied by our transposed one.  Remember that the number of values in the row must equal the values in the column in order to do dot multiplication.  I'll need to get into more math studies while doing this course and that will slow me down.  I'll spend more time doing this to compensate.

np.dot(arr.T, arr)

# It is also possible to make 3D matrices by using the reshape method though it isn't used often.

arr3d = np.arange(50).reshape((5,5,2))

arr3d

# Transposing a 3d matrix uses .tranpose and the output must be specified.

arr3d.transpose((1,0,2))

arr = np.array([[1,2,3]])

arr

arr.swapaxes(0,1)

# Universal array functions

arr = np.arange(11)
arr

import matplotlib.pyplot as plt
# %matplotlib inline

# We imported matplotlib.pyplot to visualize and we set it to plt to save characters.  We have to include the %matplotlib inline to allow us to see everythin in Jupyter

points = np.arange(-5,5,.01)

dx,dy = np.meshgrid(points, points)

dx

dy

z = (np.sin(dx) + np.sin(dy))
z

plt.imshow(z)

# +
plt.imshow(z)
plt.colorbar()

plt.title('plot for sin of x plus y')
# -

# np.where gives you a shorthand for booleans in numpy.  

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

arr

arr.sum(0)

# sum will add the values of an array over a given axis.  In the case above, 0 would be the columns and 1 would be the rows.  We can also get the mean by using the method below.  We can do it by axis or leave empty paren to get the mean of all.

arr.mean()

arr.std()

arr.var()

arr = np.arange(5)

# Below is how we save and load arrays.  First we created one and saved it using np.save.  Note that you must pass a name in quotes and the array you want to save.

arr

np.save('myarray',arr)

# Then we created another array under the original name.  By using np.load and passing the array name we gave our first array ending in .npy, we can retrieve our original.

arr = np.arange(10)

arr

np.load('myarray.npy')

# We can also save multiple arrays under a single name by zipping them using np.savez and passing our created name ending in npz in quotes followed by the files we want saved separated by commas.

arr1 = np.load('myarray.npy')
arr2 = arr

np.savez('ziparrays.npz',x = arr1, y = arr2)

zipped = np.load('ziparrays.npz')

# To call them, you must use the name of the zipped variable in quotes and within square brackets as shown below:

zipped['x']

zipped['y']

# Below is how we can save a matrix as a txt file.

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

arr


np.savetxt('textarray.txt', arr, delimiter=',')

arr = np.loadtxt('textarray.txt', delimiter = ',')

arr




StarcoderdataJupyterScriptsDedupFiltered