SciPy, NumPyscikit-learn, statsmodels, pytorchpandasmatplotlibMy own, personal, preferences:
R is good for
Python is good for
Work on the warmup activity (handout).
Recall our code from a previous class:
Here is the same code, written in Python
import numpy as np
M = 10 # number of people at the party
hats = np.arange(M) # numbered hats
nsim = 10000 # number of simulations
results = np.zeros(nsim) # to store the results
for i in range(nsim):
randomized_hats = np.random.choice(hats, M, replace = False)
results[i] = np.sum(randomized_hats == hats) > 0
np.mean(results)array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.int64(0)
np.int64(1)
hats is a 1-dimensional array (similar to a vector in R)hats[0]import numpy as np
M = 10 # number of people at the party
hats = np.arange(M) # numbered hats
randomized_hats = np.random.choice(hats, M, replace = False)
randomized_hatsarray([7, 6, 1, 0, 9, 2, 8, 5, 4, 3])
np.random.choice works like R’s sample functionTrue and False (as opposed to TRUE and FALSE, or T and F)import numpy as np
M = 10 # number of people at the party
hats = np.arange(M) # numbered hats
randomized_hats = np.random.choice(hats, M, replace = False)
randomized_hatsarray([0, 3, 1, 5, 8, 7, 6, 4, 9, 2])
array([ True, False, False, False, False, False, True, False, False,
False])
np.int64(2)
import numpy as np
M = 10 # number of people at the party
hats = np.arange(M) # numbered hats
nsim = 10000 # number of simulations
results = np.zeros(nsim) # to store the results
for i in range(nsim):
randomized_hats = np.random.choice(hats, M, replace = False)
results[i] = np.sum(randomized_hats == hats) > 0
np.mean(results)range(nsim) is similar to 1:nsim in R{ }. Instead we use whitespace (four spaces is standard, you just have to be consistent)Work on the class activity on the course website. You do not need to submit anything for this activity.