Exercise 1¶

In [ ]:
# EXERCISE 1: Try to compute the BMI of each subject, as well as the average BMI across subjects
# BMI = weight/(length/100)**2

subj_length = [180.0,165.0,190.0,172.0,156.0]
subj_weight = [75.0,60.0,83.0,85.0,62.0]
subj_bmi = []


Exercise 2¶

In [ ]:
# EXERCISE 2: Try to complete the program now!
# Hint: np.mean() computes the mean of an ndarray
# Note that unlike MATLAB, Python does not need the '.' before elementwise operators

import numpy as np
subj_length = np.array([180.0,165.0,190.0,172.0,156.0])
subj_weight = np.array([75.0,60.0,83.0,85.0,62.0])


Exercise 3¶

In [ ]:
# EXERCISE 3: Create a 2x3 array containing the column-wise and the row-wise means of the original matrix
# Do not use a for-loop, and also do not use the np.mean() function for now.

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


Exercise 4¶

In [ ]:
# EXERCISE 4: Create your own meshgrid3d function
# Like np.meshgrid(), it should take two vectors and replicate them; one into columns, the other into rows
# Unlike np.meshgrid(), it should return them as a single 3D array rather than 2D arrays
# ...do not use the np.meshgrid() function


Exercise 5¶

In [ ]:
# EXERCISE 5: Make a better version of Exercise 3 with what you've just learned
arr = np.array([[1,2,3],[4,5,6],[7,8,9]], dtype='float')

print np.array([(arr[:,0]+arr[:,1]+arr[:,2])/3,(arr[0,:]+arr[1,:]+arr[2,:])/3])


Exercise 6¶

In [ ]:
# EXERCISE 6: Create a Gabor patch of 100 by 100 pixels

import numpy as np
import matplotlib.pyplot as plt


Exercise 7¶

In [ ]:
# EXERCISE 7: Vectorize the above program

import numpy as np
throws = np.random.randint(1,7,(5000,2000))
one = (throws==1)
two = (throws==2)
three = (throws==3)


Exercise 8¶

In [ ]:
# EXERCISE 8: Visualize the difference between the PIL conversion to grayscale, and a simple average of RGB
# Display pixels where the average is LESS luminant in red, and where it is MORE luminant in shades green
# The luminance of these colors should correspond to the size of the difference
# Extra: Maximize the overall contrast in your image
# Extra2: Save as three PNG files, of different sizes (large, medium, small)

import numpy as np
from PIL import Image
im = Image.open('python.jpg')


Exercise 9¶

In [ ]:
# EXERCISE 9: Plot y=sin(x) and y=sin(x^2) in two separate subplots, one above the other
# Let x range from 0 to 2*pi

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches


Exercise 10¶

In [ ]:
# EXERCISE 10: Add regression lines

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.lines as lines

# Open image, convert to an array
im = Image.open('python.jpg')
im = im.resize((400,300))
arr = np.array(im, dtype='float')

# Split the RGB layers and flatten them
R,G,B = np.dsplit(arr,3)
R = R.flatten()
G = G.flatten()
B = B.flatten()

# Do the plotting
plt.figure(figsize=(5,5))
plt.plot(R, B, marker='x', linestyle='None', color=(0,0,0.6))
plt.plot(R, G, marker='.', linestyle='None', color=(0,0.35,0))

# Tweak the plot
plt.axis([0,255,0,255])
plt.xlabel('Red value')
plt.ylabel('Green/Blue value')