<jupyter_start><jupyter_text>Numpy Random Package Numpy.Random is a Python-based package used for generating random numbers. - Numpy can be imported and used as folllows:<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>The numpy.random package can be split into **four** components: 1. Simple Random Data 2. Permutations 3. Distributions 4. Random Generator  Simple Random Data- Used for generating simple random data- Based on the Mersenne Twister sequence and is considered deterministic.- Not truly random, so the numbers generated using the package are pseudorandom numpy.random.rand()- Generates random numbers- Add params to specifiy the dimensions of the array to be populated with random numbers<jupyter_code>np.random.rand(3,5)<jupyter_output><empty_output><jupyter_text>- The rand function returns numbers from a uniform distribution over [0,1]    - i.e returns a number between 0(including) and 1(not including).- The following histogram shows the distribution of numbers using the rand function<jupyter_code>import matplotlib.pyplot as plt
x = np.random.rand(1000)
plt.hist(x)
plt.show()<jupyter_output><empty_output><jupyter_text>Permutations The np.random package also offers funtionality for randomizing existing arrays.  np.random.shuffle()- Modify an array in-place by shuffling its contents.<jupyter_code># takes the entries in an array and shuffles them. For example, to shuffle a deck of cards.
arr = np.arange(53)
np.random.shuffle(arr)
arr<jupyter_output><empty_output><jupyter_text>np.random.permutation()- makes a copy and shuffle the elements randomly<jupyter_code>np.random.permutation(np.arange(10))<jupyter_output><empty_output><jupyter_text>Distributions np.random.uniform ()- The uniform distribution is a probability distribution that has a constant probability- The numpy.random.rand() uses the uniform distribution - Takes 3 parameters    - np.random.uniform(low=0.0, high=1.0, size=None)    - all generated values will be >= low    - all generated values will < high<jupyter_code>x = np.random.uniform(0,10,1000)
plt.hist(x)<jupyter_output><empty_output><jupyter_text>np.random.normal()- Generates random numbers around a central value- The mean, median and mode are all equal<jupyter_code>x = np.random.normal(10,100,10000)
plt.hist(x, color = "red")<jupyter_output><empty_output><jupyter_text>- In this case, the central value is 0.0- As shown in the histogram above, 99% of the values generated are within 3 standard deviations of the mean Normal vs Unifrom- The Normal distribution is distinguishable from the uniform in the spread of numbers generated.- Normal distribution values form a bell shape, centered arounda single value. In contrast, the uniform creates an even shape indicating the even spread of random numbers.   numpy.random.binomial(n, p[, size])-  Used to determine the number of successes (p) in (n) repeated trials of a binomial experiment- The binomial distribution has only two outcomes, success and failure.    - e.g a coin toss- All binomial trials are independent from each other.- The probability of success doesn’t change from trial to trial<jupyter_code>n, p = 10, .5  # number of trials, probability of each trial
s = np.random.binomial(n, p, 1000)
plt.hist(s, color = "yellow", label= "Binomial Distribution, Coin Toss")
plt.legend()<jupyter_output><empty_output><jupyter_text>- The histogram above shows a symmetric distribution.- This occurs when the probability of success and failure is equal.   If the probability of success in a trial is greater than 0.5, the distribution will be negatively skewed.<jupyter_code>n, p = 10, .8  # number of trials, probability 
s = np.random.binomial(n, p, 1000) 
plt.hist(s, color = "purple", label="Binomial Distribution, negative skew")
plt.legend()<jupyter_output><empty_output><jupyter_text>- This figure shows the probabilities of success are greater for values above the expected value.- In contrast, if the probability of success in a trial is less than 0.5, the distribution will be positively skewed.<jupyter_code>n, p = 100, .2  # number of trials
s = np.random.binomial(n, p,1000 ) 
plt.hist(s, color = "pink", label="Binomial Distribution, positive skew")
plt.legend()<jupyter_output><empty_output><jupyter_text>numpy.random.poisson()- The probability of evevnts occuring at a specific time is Poisson Distribution.- i.e if you are aware of how often the event happened, Poisson Distribution can be used to predict how often that event will occur. - It provideds the likelihood of a given number of events occuring in a set period- For example, the average number of yearly accidents on the Tuam Road is 5.To determine the probability that there are exactly three accidents on the Tuam Road this year you could use a poisson experiment.<jupyter_code>lam= 5  # lambda is the mean value
size = 1000 # for example creates an array of 10000 elements, each drawn from a poissonian probability density function for a mean value of 05.
s = np.random.poisson(lam, size)
plt.hist(s, color = "green", label="Poisson Distribution")
plt.legend()<jupyter_output><empty_output><jupyter_text>numpy.random.beta()- The Beta distribution is a distribution for probabilities. - Beta distribution is a continuous distribution taking values from 0 to 1.- It is defined by two parameters alpha and beta, - Depending on the values of alpha and beta they can assume very different distributions.<jupyter_code># generate 10000, random numbers from Beta distribution with alpha = 1 and beta = 1.
s = np.random.beta(1,1, 1000)
plt.hist(s, color = "orange", label="Beta Distribution")
plt.legend()<jupyter_output><empty_output><jupyter_text>- As shown above, the histogram of Beta(1,1) is a uniform distribution.<jupyter_code># generate 10000, random numbers from Beta distribution with alpha = 10 and beta = 1.
s = np.random.beta(10,1, 1000)
plt.hist(s, color = "black", label="Beta Distribution, negative skew")
plt.legend()<jupyter_output><empty_output><jupyter_text>The histogram of Beta(10,1) is skewed towards right.<jupyter_code># generate 10000, random numbers from Beta distribution with alpha = 1 and beta = 10.
s = np.random.beta(1,10, 1000)
plt.hist(s, color = "magenta", label="Beta Distribution, netagive skew")
plt.legend()<jupyter_output><empty_output><jupyter_text>The histogram of Beta(1,10) is skewed towards left.<jupyter_code># generate 10000, random numbers from Beta distribution with alpha = 10 and beta = 10.
s = np.random.beta(10,10, 1000)
plt.hist(s, color = "purple", label="Beta Distribution")
plt.legend()<jupyter_output><empty_output><jupyter_text>The histogram of Beta(10,10) is symetric and looks like the normal distribution.  Random Generator  numpy.random.seed()- Using a seed makes the random numbers predictable/repeatable.- When the value is reset, the same numbers will appear every time. This can be useful for example, when writing unit tests or writing explpaining an example, such as in this notebook.- If you do not assign the seed, NumPy automatically selects a random seed value based on the system's random number generator device or on the clock.<jupyter_code>np.random.seed(20) 
print(np.random.rand(5))
       
print (np.random.rand(5))
np.random.seed(20) # reset the seed to get repeatable results
print (np.random.rand(5))
print (np.random.rand(5))<jupyter_output>[0.5881308  0.89771373 0.89153073 0.81583748 0.03588959]
[0.69175758 0.37868094 0.51851095 0.65795147 0.19385022]
{'fraction_non_alphanumeric': 0.08700322234156821, 'fraction_numerical': 0.03423737916219119, 'mean_word_length': 5.377568493150685, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '555671', 'license': 'MIT', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2125, 'n_words': 877, 'path': 'numpy_random.ipynb', 'repo_name': 'DaireNiC/Emerging-Technologies', 'size': '7448'}