<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Finding Asteroids
Step1:  Figures
pl.subplots() returns the figure and an array of axes, in the shape you want to plot
Step2:  Q. Guesses for where this data came from?
Q. Why would this asteroid exercise be useful in practice?
Random Numbers (Text Ch. 8)
"Drawing" random numbers
Step3:  Q. So, if we didn't set the seed, how did we get different numbers each of the first 3 times I called random?
Q. What could the seed be based on that would appear random?
Q. If we need random numbers generated between 0 and 100 what could we do?
Step4:  Q. How could we visualize the distribution better?
Step5:  Q. Is the normalization about right?   That is, how many samples are there?
Step6:  Q. From these data, how would we compute the probability of a random number occuring in each bin?
Step7:  Q. What should the sum of all the numbers be (on average)?
Step8:  Q. When might you be inclined to provide the random number generator with a seed?
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as pl
Explanation: Finding Asteroids
End of explanation
img1 = np.loadtxt('../Tutorials/starfield_exp1.txt')
img2 = np.loadtxt('../Tutorials/starfield_exp2.txt')
# Side-by-side plots!
# Subplot values: (number of rows, number of columns, plot number)
fig, (ax1, ax2) = pl.subplots(ncols=2, figsize=(12,6))
#  Show each image
ax1.imshow(img1, origin='lower')
ax2.imshow(img2, origin='lower')
#  Get the coordinates of the middle of the image
xMid = img1.shape[0] / 2
yMid = img1.shape[1] / 2
# Overplot circle around starting point.
ax1.scatter(xMid, yMid, color='k', marker='o', s=100, facecolors='none') # Hollow points of size = 100
ax2.scatter(xMid, yMid, color='k', marker='o', s=100, facecolors='none')
#  Diff the images
img3 = img2 - img1
#  Get 
endpt = np.argwhere(img3 == img3.min())
ax2.scatter(endpt[0][0], endpt[0][1], color='r', marker='o', s=100, facecolors='none')
Explanation: Figures
pl.subplots() returns the figure and an array of axes, in the shape you want to plot:
End of explanation
import random
print(random.random())
print(random.random())
print(random.random())
random.seed(2)
print(random.random())
random.seed(2)
print(random.random())
random.seed(2)
print(random.random())
Explanation: Q. Guesses for where this data came from?
Q. Why would this asteroid exercise be useful in practice?
Random Numbers (Text Ch. 8)
"Drawing" random numbers
End of explanation
print(100 * random.random())
print(100 * random.random())
print(100 * random.random())
random.uniform(10, 20)
import random
# Pick a seed:
random.seed(5)
# Let's have 1000 samples:
N = 1000
x = range(N)
# List comprehension to generate random numbers in y between 0 and 100:
y = [random.uniform(0, 100) for _ in x]
pl.scatter(x, y)
Explanation: Q. So, if we didn't set the seed, how did we get different numbers each of the first 3 times I called random?
Q. What could the seed be based on that would appear random?
Q. If we need random numbers generated between 0 and 100 what could we do?
End of explanation
random.seed(5)
N = 1000      # 1000 samples
x = range(N)
y = [random.uniform(0, 100) for _ in x]
# Now, plot a histogram of the data
#
# hist() returns:
#    the number of values in each bin <array>
#    the bin x-coord values <array>
#    the patches <array>
#
# A Patch describes a bin geometrically
nArray, binArray, patchArray = pl.hist(y, bins=50, facecolor='g')
# Label the plot and add a grid for visualization:
pl.xlabel('Random Number')
pl.ylabel('Number of Occurances')
pl.grid(True)
#  Print each returned value from hist()
print(nArray)
print()
print(binArray)
print()
print(patchArray)
print()
print(patchArray[0])
Explanation: Q. How could we visualize the distribution better?
End of explanation
# Hint:  What is the average number of hits 
# in each bin and how many bins are there?
print(len(nArray))
Explanation: Q. Is the normalization about right?   That is, how many samples are there?
End of explanation
# Same as above
random.seed(5)
N = 1000
x = range(N)
y = [random.uniform(0, 100) for _ in x]
# Now, note "normed = True" to normalize to 1:
nArray, binArray, patchArray = pl.hist(y, bins=50, normed=True, facecolor='g')
# Then plot
pl.xlabel('Random Number')
pl.ylabel('Probability of Occurance')
pl.grid(True)
nArray
import numpy as np
randUniArray = np.random.uniform(0, 10, size=10000) # Range from 0 to 10
Explanation: Q. From these data, how would we compute the probability of a random number occuring in each bin?
End of explanation
sum(randUniArray)
import numpy as np
mu    = 100  # Average of 100
sigma = 20   # Standard deviation of 20
# randn = normal distribution (unity normalized by default)
randNormArray = mu + sigma * np.random.randn(10000)
# Q. What data type should x be?
type(randNormArray)
# Histogram of the data:
#nArray, binArray, patchArray = pl.hist(randNormArray, 100, normed=True, facecolor='g')
pl.hist(randNormArray, 100, normed=True, facecolor='g')
pl.xlabel('x', size='large')
pl.ylabel('Probability of x', size='large')
pl.title('Normal Distribution')
pl.text(40, 0.015, r'$\mu=100,\ \sigma=20$', size='x-large')
pl.grid(True)
mu    = 0
sigma = 20 
randNormArray = mu + sigma * np.random.randn(1000000) 
nArray, binArray, patchArray = pl.hist(randNormArray, 100, normed=1, facecolor='r')
pl.xlabel('x', size='large')
pl.ylabel('Probability of x', size='large')
pl.title('Normal Distribution')
pl.text(40, .015, r'$\mu=0,\ \sigma=20$', size='x-large')
pl.grid(True)
Explanation: Q. What should the sum of all the numbers be (on average)?
End of explanation
mu    = 100
sigma = 20
# Method #1
randNormArray1 = mu + sigma * np.random.randn(10000) 
# Method #2
randNormArray2 = np.random.normal(size=10000, loc=mu, scale=sigma)
# Compare
_, _, _ = pl.hist(randNormArray1, bins=100, color='b')
_, _, _ = pl.hist(randNormArray2, bins=100, color='g')
Explanation: Q. When might you be inclined to provide the random number generator with a seed?
End of explanation
<END_TASK>