import numpy as np
import matplotlib.pyplot as plt
Generating arrays - random order generator. Make an NP array list that includes the names of your immediate family (you can make up the names if you want) as well as uncles and aunts and cousins. Now pretend that you are all staying at a small cabin with a single bathroom and shower and you need a fair way of randomly ordering who will get to use the bathroom/shower during the morning. So you offer to create a random ordering. Write a program that creates a random ordering of the names in the array. (Hint: you could probably use the function np.random.choice()
# I will use the basic list, but you could easily also do this with np.array
family = ["Johannes", "Siri", "Bo", "Odin", "Olav"]
np.random.choice
<function RandomState.choice>
np.random.choice(family,5, replace=False)
array(['Bo', 'Olav', 'Siri', 'Johannes', 'Odin'], dtype='<U8')
Generating random values
a. Generate 100 random values from a uniform distribution (you can choose the range yourself, 0-1, 0-10, whatever). Plot the values in a histogram
b. Now generate a 100x100 2-d array of random values from a uniform distribution.
c. Take the mean across each column and store this in a 1-d array. Plot a histogram of this array. Does it look like the array in a?
# I use the np.random.uniform command to generate 100 random values from a uniform distribution
rvals = np.random.uniform(0,10,100)
plt.hist(rvals)
(array([12., 7., 10., 11., 7., 10., 8., 12., 11., 12.]), array([6.56093224e-04, 9.99823459e-01, 1.99899082e+00, 2.99815819e+00, 3.99732556e+00, 4.99649292e+00, 5.99566029e+00, 6.99482765e+00, 7.99399502e+00, 8.99316239e+00, 9.99232975e+00]), <BarContainer object of 10 artists>)
# To create a 100 x 100 array, I start by generating 10.000 numbers (100x100)
rvals2 = np.random.uniform(0,10,10000)
# Then I change the shape of the array
rvals2 = rvals2.reshape(100,100)
rvals2.shape
(100, 100)
rvals
array([5.30281094e+00, 9.65198851e-01, 3.17893470e-01, 7.77609614e+00, 8.08541216e+00, 8.18512315e+00, 1.15680932e+00, 2.20136959e-01, 8.86960116e+00, 1.98393151e+00, 4.93579583e+00, 5.47603294e+00, 3.62132876e+00, 7.53559890e+00, 6.18211422e+00, 5.53471836e+00, 7.92208384e+00, 5.65222032e-01, 6.67872730e+00, 8.03962361e-01, 2.36311719e+00, 4.73083279e+00, 1.64357983e+00, 5.53153012e-02, 3.86642102e+00, 3.59625915e+00, 9.82656393e+00, 7.95315669e+00, 9.43733900e+00, 8.93178425e+00, 2.48574028e+00, 9.93085664e+00, 2.67637215e+00, 2.05276835e+00, 9.29427209e+00, 7.51855984e+00, 6.32130525e-01, 5.41905385e+00, 9.17563136e+00, 3.49626224e+00, 1.91126810e+00, 7.60983583e+00, 5.63191752e+00, 8.32364571e+00, 2.77449163e+00, 3.70961525e+00, 4.02092117e+00, 7.56473509e+00, 6.85155826e+00, 8.20982603e+00, 9.23125387e+00, 5.95827213e+00, 2.02775010e+00, 6.56093224e-04, 6.75333824e+00, 6.23176044e+00, 3.60115812e-01, 4.29412547e+00, 8.31837819e+00, 5.97269386e+00, 4.46508992e+00, 1.67548278e+00, 9.59850259e+00, 7.52574988e+00, 2.93549942e+00, 3.05260474e+00, 7.71472538e+00, 9.84595525e+00, 9.99232975e+00, 7.23201097e+00, 4.09886598e+00, 9.70228938e+00, 2.73961626e+00, 3.03630924e+00, 3.37839019e+00, 3.50856537e+00, 3.96981490e+00, 1.83733401e+00, 8.74972050e+00, 5.85272277e+00, 8.08396668e+00, 9.10388014e+00, 4.46671112e+00, 6.38738293e+00, 6.85707016e+00, 8.55876272e+00, 3.25500649e+00, 2.80416672e+00, 2.30687039e-01, 2.67198510e+00, 2.46485189e-01, 5.98298508e+00, 9.62386279e-01, 5.56311216e+00, 6.85659863e+00, 7.58435146e+00, 8.75080601e+00, 9.33866765e+00, 7.52791371e+00, 1.48116771e+00])
rvals2_means = rvals2.mean(axis=1)
plt.hist(rvals2_means)
(array([ 3., 11., 16., 10., 19., 11., 11., 8., 5., 6.]), array([4.51336873, 4.61989459, 4.72642044, 4.83294629, 4.93947215, 5.045998 , 5.15252385, 5.25904971, 5.36557556, 5.47210141, 5.57862726]), <BarContainer object of 10 artists>)
We notice that we don't get a uniform distribution. Instead, this looks more normal. This is not accident - this is one of the most central results in statistics: that the mean (or sum) of any distribution of numbers end up converging towards a normal (gaussian) distribution!
Working with a small data set
a. Find a small data set on a website somewhere and import it as a numpy array. You can either copy and paste the data directly into Jupyter, or copy it into a txt document then read it in as we did above. (For those who really want a challenge, you could experiment with using a web-scraping package like [BeautifulSoup](https://beautiful-soup-4.readthedocs.io/en/latest/) to import data directly from a website.)
b. Create a simple plot of the data - it doesn't need to be pretty.
See the example of electricity prices in the lab