Week 10

Quiz

  1. Describe what np.roll does.

  2. What does the interval argument of the animation code determine?

  3. Write a function that takes an array of integers as input and returns an arrays of 1s where there were $k$s and 0s everywhere else.

In [ ]:
anim = FuncAnimation(fig=fig, func=animate,  interval=100, blit=True, repeat=False)

Answers:

  1. Shifts the entries of the array by a specified amount(s) in a specified direction(s) with anything that goes over the edge looping around.
  2. Determines the frame rate by setting the number of milliseconds between frames.
In [1]:
def the_straightforward_way(arr,k):
    m,n = np.shape(arr)
    for i in range(m):
        for j in range(n):
            if arr[i,j] == k:
                arr[i,j] = 1
            else:
                arr[i,j] = 0
    return arr
In [2]:
def the_boolean_way(arr,k):
    arr = arr == k
    arr = np.array(arr,dtype = int)
    return arr

Notes

3D arrays

In [3]:
import numpy as np
In [6]:
arr1 = np.zeros((5,4,3),dtype = int)
print(arr1)
[[[0 0 0]
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  [0 0 0]]

 [[0 0 0]
  [0 0 0]
  [0 0 0]
  [0 0 0]]]
In [7]:
np.shape(arr1)
Out[7]:
(5, 4, 3)
In [8]:
arr2 = np.zeros((4,20),dtype=int)
arr2
Out[8]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
In [9]:
np.transpose(arr2)
Out[9]:
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
In [10]:
np.transpose(arr1)
Out[10]:
array([[[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]],

       [[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]],

       [[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]]])
In [15]:
np.shape(np.transpose(arr1,axes=(1,0,2)))
Out[15]:
(4, 5, 3)
In [12]:
np.shape(arr1)
Out[12]:
(5, 4, 3)
In [16]:
arr3 = np.ones((5,20),dtype=int)
arr3
Out[16]:
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
In [17]:
arr2
Out[17]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
In [18]:
np.concatenate([arr2,arr3])
Out[18]:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
In [20]:
np.concatenate([arr2,arr3],axis=1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [20], in <cell line: 1>()
----> 1 np.concatenate([arr2,arr3],axis=1)

File <__array_function__ internals>:5, in concatenate(*args, **kwargs)

ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 4 and the array at index 1 has size 5
In [23]:
np.concatenate([np.zeros((3,3)),np.ones((3,3))],axis=1)
Out[23]:
array([[0., 0., 0., 1., 1., 1.],
       [0., 0., 0., 1., 1., 1.],
       [0., 0., 0., 1., 1., 1.]])
In [25]:
np.concatenate([np.zeros((3,3,1)),np.ones((3,3,1))],axis=2)
Out[25]:
array([[[0., 1.],
        [0., 1.],
        [0., 1.]],

       [[0., 1.],
        [0., 1.],
        [0., 1.]],

       [[0., 1.],
        [0., 1.],
        [0., 1.]]])

RGB color arrays

In [26]:
red = np.ones((10,10),dtype=float)
In [27]:
import matplotlib.pyplot as plt
In [29]:
green = np.ones((10,10),dtype=float)
blue = np.ones((10,10),dtype=float)
In [36]:
arr = np.zeros((20,20,3),dtype=float)
arr[8:18,5:15,0] = red
arr[2:12,2:12,1] = green
arr[2:12,8:18,2] = blue
plt.imshow(arr)
plt.show()
In [46]:
arr = np.zeros((20,20,3),dtype=float)
arr[6:20,:,0] = np.random.random((14,20))
arr[:15,:14,1] = np.random.random((15,14))
arr[:15,6:20,2] = np.random.random((15,14))
plt.imshow(arr)
plt.show()

Strings and dictionaries

In [1]:
string1 = "Hello, World!"
string2 = 'How are you?'
In [3]:
string1[5:8]
Out[3]:
', W'
In [4]:
string2[-1]
Out[4]:
'?'
In [5]:
string1+' '+string2
Out[5]:
'Hello, World! How are you?'
In [6]:
string1.upper()
Out[6]:
'HELLO, WORLD!'
In [7]:
string2.lower()
Out[7]:
'how are you?'
In [8]:
string1
Out[8]:
'Hello, World!'
In [9]:
string2.strip(',.?!')
Out[9]:
'How are you'
In [10]:
string1.strip(',.?!')
Out[10]:
'Hello, World'
In [11]:
string2
Out[11]:
'How are you?'
In [13]:
string3 = string1+' '+string2
In [14]:
string3.split()
Out[14]:
['Hello,', 'World!', 'How', 'are', 'you?']
In [15]:
pattern = "B14 K6 B6 K6 B6 K32 OG32 K6 OG32 K32 B32 K6 B6 K6 B32 K32 OG32 K6 OG32 K32 B6 K6 B6 K6 B28"
In [16]:
pattern.split()
Out[16]:
['B14',
 'K6',
 'B6',
 'K6',
 'B6',
 'K32',
 'OG32',
 'K6',
 'OG32',
 'K32',
 'B32',
 'K6',
 'B6',
 'K6',
 'B32',
 'K32',
 'OG32',
 'K6',
 'OG32',
 'K32',
 'B6',
 'K6',
 'B6',
 'K6',
 'B28']
In [17]:
alphanum = "fhf9939jfq0jdk02ihnd9047tnao7h438vndaoi784"
In [21]:
alpha = ''
num = ''
for char in alphanum:
    if char.isdigit():
        num += char
    else:
        alpha += char
print(alpha)
print(int(num))
fhfjfqjdkihndtnaohvndaoi
993900290477438784
In [19]:
"25"+"67"
Out[19]:
'2567'
In [20]:
int("25")+int("67")
Out[20]:
92
In [22]:
colors = {"B" : [52, 80, 100], "K" : [16, 16, 16], "OG" : [92, 100, 40]}
In [23]:
colors.keys()
Out[23]:
dict_keys(['B', 'K', 'OG'])
In [24]:
colors.values()
Out[24]:
dict_values([[52, 80, 100], [16, 16, 16], [92, 100, 40]])
In [25]:
colors.items()
Out[25]:
dict_items([('B', [52, 80, 100]), ('K', [16, 16, 16]), ('OG', [92, 100, 40])])
In [26]:
colors["K"]
Out[26]:
[16, 16, 16]
In [27]:
colors_MacKusick = {"RB" : [0, 0, 140],"G" : [0, 120, 0],"DR" : [140, 0, 0],"K" : [0, 0, 0],"LN" : [255, 255, 255],"P" : [100, 0, 140]}

Exercise: Write a detailed plan for how you are going to generate the array of horizontal or vertical striples.

In [28]:
Gunn_pattern = "DG4 DB24 DG2 K24 DG24 R4"
Gunn_colors = {"DG" : [0,100,0],"DB" : [28,0,112],"K" : [16,16,16],"R" : [200,0,0]}
In [ ]: