<jupyter_start><jupyter_text>Numpy Random  Introduction NumPy is the fundamental libray for scientific computing with Python. The random package found in the Numpy library and is mainly used for generating random data. The package is broken down into four sections:- Simple random data- Permutations- Distributions- Random generatorThis notebook will be divided into four sections based on these headings, showing and explaining examples of functions in eachof these sections. To use the numpy.random package, the library must be imported like so:<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Simple Random Data There are many functions in this part of the Numpy.random package. Such as:   - rand   - randn   - randint   - random_integers   - random_sample   - random   - ranf   - sample   - choice   - bytesThis notebook will contain examples of a few of these functions.  Example 1- numpy.ramdom.rand This creates an Array of the given shape and populates it with random samples.<jupyter_code># Import numpy for random package
import numpy as np
# Generate our random numbers
np.random.rand(2,7,4)<jupyter_output><empty_output><jupyter_text>Example 2- numpy.ramdom.randn This returns a sample/samples from the “standard normal” distribution.<jupyter_code># Import numpy for random package
import numpy as np

# Generate our random numbers
np.random.randn(2,7,4)<jupyter_output><empty_output><jupyter_text>Example 3- numpy.ramdom.randint This returns a random integer from low to high, where it is optional to state the size of the array returned, the default is 1.<jupyter_code># Generate a random integer between 3 and 9
np.random.randint(3,9)
# Generate an array of 4 elements with random integers between 3 and 18
np.random.randint(3,18,size=4)<jupyter_output><empty_output><jupyter_text>Example 4- numpy.ramdom.bytes<jupyter_code>This returns random bytes in the form of a string. The size of the string is determined by the integer within the given parameter.
# Import numpy for random package
import numpy as np

# Generate our random bytes
np.random.bytes(30)<jupyter_output><empty_output><jupyter_text>Permutations A permutation can be defined as a collection of members to a set where order is important. The process of re-odering its elements is know as permuting.This section of numpy.random only contains two functions:- random.shuffle- random.permutation I will look at these functions and give a brief explanation, along with examples.  Example 1- numpy.random.shuffle The function modifies a sequence by shuffling the contents into different indexes.<jupyter_code># First we declare and initiate an Array
shape_array = ["Red", "Blue", "Green", "Black", "White", "Yellow", "Orange"]

# Then we shuffle our array by calling the 'shuffle' function
np.random.shuffle(shape_array)

# Output array
shape_array<jupyter_output><empty_output><jupyter_text>In the above example, we can see that the array is no longer arranged sequentially, but instead has been shuffled. It is important to remember though, that if you are using a multiple dimension array, that this function only shuffles the array along the first axis of the array.<jupyter_code>shape_array = ([[ "Red", "Green", "Blue"],
       [ "Black", "White", "Yellow"],
       ["Orange", "Pink", "Grey", "Brown"]])
np.random.shuffle(shape_array)
shape_array
As you can see, it only shuffled the sub-arrays, not the arrays themselves.<jupyter_output><empty_output><jupyter_text>Example 2- numpy.random.permutation This works like numpy.random,shuffle, It will randomly permute a sequence, or return a permuted range.For example, it can create an array and randomize it:<jupyter_code># Generate our random numbers
np.random.permutation(10)
It can also randomize a multi-dimensional array:
# Declare and Create a 3 dimensional array of size 5
array = np.arange(15).reshape((3, 5))
# Call our permutation function
np.random.permutation(array)<jupyter_output><empty_output><jupyter_text>Distributions The distribution of a statistical data set (or a population) is a listing or function showing all the possible values (or intervals) of the data and how often they occur. When a distribution of categorical data is organized, you see the number or percentage of individuals in each group. When a distribution of numerical data is organized, they’re often ordered from smallest to largest, broken into reasonably sized groups (if appropriate), and then put into graphs and charts to examine the shape, center, and amount of variability in the data.In the numpy.random package there are 35 different distributions that can be used. In the below examples, we will talk about a couple of the more common ones.  Example 1- numpy.random.normal This function is used to draw graphs from random samples from a normal (Gaussian) distribution. In order to graph data we must import the matplotlib.pyplot package<jupyter_code>import matplotlib.pyplot as plt
# Draw Samples From Distribution
mu, sigma = 0, 0.1 # mean and standard deviation
s = np.random.normal(mu, sigma, 1000)
# Verify the mean and the variance
abs(mu - np.mean(s)) < 0.01
# abs(sigma - np.std(s, ddof=1)) < 0.01
abs(sigma - np.std(s, ddof=1)) < 0.01
# Display the histogram of the samples, along with the probability density function
count, bins, ignored = plt.hist(s, 30, density=True)
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (bins - mu)**2 / (2 * sigma**2) ), linewidth=2, color='r')
plt.show()<jupyter_output><empty_output><jupyter_text>Example 2- numpy.random.logistic This is used to draw graphs from a logistic distribution with specified parameters.<jupyter_code>s = np.random.logistic(50, 1, 1000)
count, bins, ignored = plt.hist(s, bins=50)<jupyter_output><empty_output><jupyter_text>Example 3- numpy.random.poisson This is typically used to model the number of time an event occurs in a time interval.<jupyter_code># Generate samples
s = np.random.poisson(5, 10000)
s
# Draw 100 values for lambda 100 and 500
s = np.random.poisson(lam=(100., 500.), size=(100, 2))
count, bins, ignored = plt.hist(s, 14, density=True)

# Plot the results
plt.show()<jupyter_output><empty_output><jupyter_text>Example 4- numpy.random.uniform This is used to draw graphs from a uniform distribution. Samples are uniformly distributed over the half-open interval (low, high). This includes low but will exclude high. When using this function, you must include 3 parameters:- Lower boundary of the output interval- Upper boundary of the output interval- The size of the output shapeSee the following code snipped to see this functions use:<jupyter_code>s = np.random.uniform(-1,0,1000)
np.all(s >= -1)
np.all(s < 0)
count, bins, ignored = plt.hist(s, 15, density=True)
plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')
plt.show()<jupyter_output><empty_output><jupyter_text>Example 4- numpy.random.binomial Binomial distribution can be very useful when visualizing data where there are only two possible outcomes of a specific event. the perfect example to use for this distribution would be a coin flip scenario. The binomial distribution allows us to visualise a sample of random outcomes for when for example a coin is flipped.<jupyter_code>s = np.random.binomial(100, 0.5, 1000) ### 100 coin flips, with a 0.5 chance of getting a heads
s
plt.ylabel(" Frequency ")
plt.xlabel(" Values ")
plt.hist(s)
plt.show()<jupyter_output><empty_output><jupyter_text>Here, we can see that for most of the test cases, when the coin was flipped 100 times, it landed on heads almost 50 times.<jupyter_code>s = np.random.binomial(100, (1/6), 1000) ### 100 dice rolls with 1/6 chance of getting a 6, done 1000 times
plt.ylabel(" Frequency ")
plt.xlabel(" Values ")
plt.hist(s)
plt.show()<jupyter_output><empty_output><jupyter_text>This clearly shows, that when a dice is rolled 100 times, the chances are that it will land on a 6 are between 15-20 of those 100 rolls.  Random Generator In this final part of the notebook we will look at random number generation. Random number generators can be applied for  generating random numbers for the purposes of gambling or creating unpredictable results in a computer game.Random numbers are split into two categories:True Random NumbersPseudorandom Numbers   Example 1- numpy.random.seed This function is used in conjuction with RandomState function. It is used to change the seed given in RandomState.<jupyter_code># set the seed
np.random.seed(12)

# Generate random number
randomNumber = np.random.rand(2,5)
print(randomNumber)<jupyter_output>[[0.15416284 0.7400497  0.26331502 0.53373939 0.01457496]
 [0.91874701 0.90071485 0.03342143 0.95694934 0.13720932]]<jupyter_text>Example 2- numpy.random.RandomState This is a pseudo-random number generator. It exposes a number of methods for generating random numbers drawn from a variety of probability distributions. When using a seed with RandomState, it will always return the same number if the same seed is used. If a seed is not used, it will always return a new number.<jupyter_code># Generate random seed
randomSeed = np.random.RandomState(500)

# Use randomSeed to generate random number
print(randomSeed.rand())

# Generate random array
randomArray = np.random.rand(1,4,2)

print(randomArray)<jupyter_output>0.6936795270051179
[[[0.28382835 0.60608318]
  [0.94422514 0.85273554]
  [0.00225923 0.52122603]
  [0.55203763 0.48537741]]]<jupyter_text>Refrences<jupyter_code>https://docs.scipy.org/doc/numpy-1.15.1/reference/routines.random.html<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08088235294117647, 'fraction_numerical': 0.03771008403361344, 'mean_word_length': 5.202605863192183, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 68, 'https://': 1, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '592727', 'license': 'MIT', 'n_tokens_mistral': 3025, 'n_tokens_neox': 2725, 'n_words': 1169, 'path': 'numpy-random/numpy-random.ipynb', 'repo_name': '123kieran/EmergingTechnologies', 'size': '9520'}