<jupyter_start><jupyter_text>The numpy.random package  Assignment 2018 for Programming for Data Analysis - Investigation into numpy.randomThere are four distinct tasks to be carried out in this investigation.Explain the overall purpose of the numpy.random package.Explain the use of the “Simple random data” and “Permutations” functions.Explain the use and purpose of at least five “Distributions” functions.Explain the use of seeds in generating pseudorandom numbers.1. Explanation of the overall purpose of the numpy.random packageNumpy.random is a sub package of the NumPy library for Python. NumPy, which is short for Numerical Python, is a library which contains large, multidimensional arrays and matrices as well as a collection of high-level mathematical routines to process these arrays. NumPy enriches Python and ensures efficient calculation. To attempt a NumPy function through core Python would require extensive amounts of custom code. Random numbers - and the ability to generate random numbers - are an important part of data analysis. The numpy.random sub package can generate random, or to be more accurate, pseudorandom numbers - pseudorandom because numpy.random arrays are determined by an initial value known as a seed. As a result, pseudorandom number generators can never be considered truely random, but that is more a theoretical point. In terms of statistics and probability, pseudorandom number generators are more than sufficient. The purpose of the numpy.random package is to provide us with a way to generate and analyze pseudorandom number arrays and matrices. 2. Explanation of the use of "Simple random data" and "Permutations" functions.Simple Random DataSimple random data is a collection of sub-routines within the numpy.random routine. At it's most basic, the simple random data routines are variations on the essential premise of numpy.random which is concerned with the creation of values from probabiliity distributions. Take the numpy.random.rand function as an example. numpy.random.rand is a convenience function that creates an array of specified shape and fills it with random values.<jupyter_code># Import Libraries
import numpy as np #import numpy
import matplotlib.pyplot as plt 
np.random.rand(4) #create a basic 1D array of 4 rows and 1 column
np.random.rand(4) #re-running same line of code gives a random result
np.random.rand(4,5) #to run 2d array of random values<jupyter_output><empty_output><jupyter_text>The second sub routine in the simple random data collection we can look at is numpy.random.sample - which returns random floats in the half open interval [0.0, 1.0).<jupyter_code>np.random.random_sample(4)<jupyter_output><empty_output><jupyter_text>Many of the sub-routines in numpy.random perform similar tasks, but random.random_sample takes it one step further and is just an alias for the functions random.random, random.ranf, and random.rand.<jupyter_code>np.random.random_sample is np.random.ranf is np.random.random<jupyter_output><empty_output><jupyter_text>The only difference between random.random_sample and random.rand is that the shapes of the output array in random.rand are separate arguments compared to a signle tupel in random.random_sample. Permutations FunctionsA permutation is an arrangement or ordering of a number of different objects. Numpy.random contains two permutation sub-functions namely "shuffle" and "permutation". Shuffle, as the name suggests, modifies a sequence by shuffling or re-arranging its contents. Permutation is similar and can be used to randonly permute a sequence or range. The difference between the two routines is subtle. The shuffle function requires a list or array to be input as a paramater. The permutation function, on the other hand, can be used with an integer or an array. The permutation function will randomly permute np.arange(x) if x=integer.<jupyter_code>m = np.array([1,2,3,4,5,6,7,8,9,10]) #define array
np.random.shuffle(m)
m
np.random.permutation(10) #function randomly permutes np.arrange(10) in this instance
%matplotlib inline
m = np.array([1,2,3,4,5,6,7,8,9,10]) #define array
np.random.shuffle(m)
m
plt.hist(m) #show array on histogram<jupyter_output><empty_output><jupyter_text>Explain the use and purpose of at least five “Distributions” functions.Distributions are a visual representation of all possible values that your random variable can be plus the probabilities for each of those values. Distributions can take the form of tables, graphs or charts.The numpy.random function contains a number of distribution sub-functions to generate and visualize random variables from specific probability distribution.The five distributions we will examine in this section are:beta(a, b[, size])	Draw samples from a Beta distribution.binomial(n, p[, size])	Draw samples from a binomial distribution.chisquare(df[, size])	Draw samples from a chi-square distribution.dirichlet(alpha[, size])	Draw samples from the Dirichlet distribution.exponential([scale, size])	Draw samples from an exponential distribution.Beta<jupyter_code>np.random.beta(4, 5, size=5)
x = np.random.beta(4, 5, size=5)#simple example
plt.hist(x)<jupyter_output><empty_output><jupyter_text>BinomialThis function lets us draw samples from a binomial distribution. Samples are drawn from a Binomial distribution with specified parameters. These paramaters are n=trials and p=probability of success where n an integer > 0 and p is in the interval [0,1].<jupyter_code>x = np.random.binomial(17, .5, size=1000)#17 samples, 50% probability of success, and 1000 experiments
plt.hist(x)<jupyter_output><empty_output><jupyter_text>In the example above the histogram shows that the majority of values are returned are between 8-10 which is consistent with our 50% probability of success when calculated out of n (n=17) ChisquareIn probability, a chi-squared distribution is widely used in hypotheses testing to determine if there is a difference between an expected distribution and actual distribution.This distribution uses the parameters df for degrees of freedom and size for the sample size.<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt 
x = np.random.chisquare(2,4)
plt.hist(x)<jupyter_output><empty_output><jupyter_text>DirichletThe Dirichlet distribution shows the probability distribution over a sample of numbers that add up to 1 also known as a probability simplex. Exponential An exponential distribution is closely related to the Poisson distribution - which allows us to answer question like the number of events that occur in an interval of time. Exponential, on the other hand, deals with the time taken between 2 events occuring.<jupyter_code>np.random.exponential(scale=1.0, size=None)<jupyter_output><empty_output><jupyter_text>4. Explain the use of seeds in generating pseudorandom numbersPseudorandom numbers have all the outward trappings of random numbers but they can never be truely random because they are set by a seed. A seed is the starting point from which software programs can generate pseudorandom numbers. Because of the presence of the seed, the generated numbers can not be considered truely random. A human who rolls a dice and records each result can create a truely random sequence because there is no set starting point. By seeding the numpy.random generator, you can make the generated numbers predictable.<jupyter_code>np.random.seed(1) ; np.random.rand(6)
np.random.seed(1) ; np.random.rand(6)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.05511811023622047, 'fraction_numerical': 0.011610836780995596, 'mean_word_length': 5.591029023746702, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 38, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '488549', 'license': 'MIT', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1878, 'n_words': 969, 'path': 'Numpy-random.ipynb', 'repo_name': 'cobhan/numpy-random', 'size': '7493'}