Explanation & Answer length: 2 pages2 attachmentsSlide 1 of 2

  • attachment_1attachment_1
  • attachment_2attachment_2

UNFORMATTED ATTACHMENT PREVIEW

Functions Definition using def Functions are the primary and most important method of code organization and reuse in Python. Functions are declared with the def keyword and returned from with the return keyword: In def my_function(x, y): return (x + y) In [1]: my_function(2,3) Out[1]: 5 Declaring a maximum function that determines and returns the largest of three values def maximum(value1, value2, value3): max_value= value1 if value2> max_value: max_value=value2 if value3>max_value: max_value= value3 return max_value maximum(1,2,3) Out[380]: 3 maximum(22,130,151.5) Out[381]: 151.5 We may call maximum with mixed types, such as int and float. A pure Python way to implement a single random generation using the built-in random module. randrange function truly produces integers at random, every number in its range has an equal probability (or chance or likelihood) of being returned each time we call it. Using the textbook roll a die 100 times, instead. Each die face should occur approximately 100 times. In [2]: import random In [3]: face =list(range(100)) In [4]: for i in range(100): face[i] =random.randrange(1,7) In [5]: print(face) [6, 3, 3, 5, 5, 1, 6, 6, 6, 1, 5, 5, 4, 4, 4, 5, 4, 3, 2, 3, 5, 4, 6, 4, 4, 6, 1, 2, 6, 5, 1, 3, 3, 1, 2, 2, 5, 4, 3, 1, 6, 5, 4, 6, 1, 4, 2, 3, 6, 3, 3, 1, 2, 4, 5, 5, 4, 1, 6, 4, 5, 2, 3, 1, 1, 1, 3, 3, 6, 2, 3, 1, 2, 2, 6, 5, 4, 1, 6, 5, 3, 2, 2, 6, 4, 2, 2, 4, 6, 2, 2, 5, 5, 2, 2, 3, 3, 4, 4, 1] plt.plot(face) Out[6]: freq6=face.count(6) freq5=face.count(5) freq4=face.count(4) freq3=face.count(3) freq2=face.count(2) freq1=face.count(1) freq1 Out[]: 15 freq2 Out[5]: 18 freq3 Out[6]: 17 freq4 Out[7]: 18 freq5 Out[8]: 16 freq6 Out[9]: 16 In [11]: max(face) – min(face) Out[11]: 5 Sorting a dataset Sorting by some criterion is another important built-in operation in Python. In[12]: sorted_face= sort(face) Out[12]: print(sorted_face) [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3333333333333444444444444444444555555 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6] Running some the common list statistics (like sum, mean, standard deviation etc.): unique(face) Out[13]: array([1, 2, 3, 4, 5, 6]) mean(face) Out[14]: 3.5 std(face) Out[368]: 1.6703293088490065 var(face) Out[15]: 2.79 median(face) Out[16]: 3.5 sum(face) Out[17]: 350 Anonymous (Lambda) Functions Python has support for so-called anonymous or lambda functions, which are a way of writing functions consisting of a single statement, the result of which is the return value. They are defined with the lambda keyword, which has no meaning other than “we are declaring an anonymous function”, rather than def function definition keyword: def short_function(x): return x * 2 This is equivalent to: equiv_anon = lambda x: x * 2 Lambda functions are especially convenient in data analysis because, as you’ll see, there are many cases where data transformation functions will take functions as arguments. It’s often less typing (and clearer) to pass a lambda function as opposed to writing a full-out function declaration or even assigning the lambda function to a local variable. For example, consider this silly example, suppose you wanted to sort a collection of strings by the number of distinct letters in each string: In [17]: strings = [‘foo’, ‘card’, ‘bar’, ‘aaaa’, ‘abab’] Here we could pass a lambda function to the list’s sort method: In [178]: strings.sort(key=lambda x: len(set(list(x)))) In [179]: strings Out[179]: [‘aaaa’, ‘foo’, ‘abab’, ‘bar’, ‘card’] One reason lambda functions are called anonymous functions is that unlike functions declared with the def keyword, the function object itself is never given an explicit name attribute. Mathematical and Statistical Methods A set of mathematical functions that compute statistics. We can use aggregations (often called reductions) like sum, mean, and std (standard deviation) either by calling the list instance method or using the top-level NumPy function as we will learn later. From your reading of Chapter 3 of our textbook’s, math and statistics calculations, share with the class examples of each the following basic statistical methods for detecting and filtering data. Organize your information so it is easy to follow and understand (use headings). Make sure all information is provided: Sum: Sum of all the elements in the array or along an axis; zero-length arrays have sum 0 Mean: Arithmetic mean; zero-length arrays have NaN mean std, var: Standard deviation and variance, respectively, with optional degrees of freedom adjustment (default denominator n) min, max: Minimum and maximum argmin, argmax: Indices of minimum and maximum elements, respectively cumsum: Cumulative sum of elements starting from 0 cumprod: Cumulative product of elements starting from 1 1 Descriptive Data Methods Student Name Department/Faculty Name, Institution Name Course Code: Course Title Professor Date 2 Descriptive Data Methods Sum For sum of all the elements along an axis, the following function returns the sum of array elements over the specified axis: Sum(arr, axis, dtype, out). The parameters for the function include: Arr as the input array Axis along which the sum value will be computed. Otherwise, arr will be regarded as flattened, meaning that all the axis will be considered. Where axis = 0, it means along the column. When axis = 1, the function will work along the row. Out stands for different array where the result will be placed. For this, array must be allocated the same dimensions as the anticipated output. Below is an example of sum function: # 1D array arr = [20, 2, .2, 10, 4] print(“\nSum of arr : “, np.sum(arr)) print(“Sum of arr(uint8) : “, np.sum(arr, dtype = np.uint8)) print(“Sum of arr(float32) : “, np.sum(arr, dtype = np.float32)) print (“\nIs np.sum(arr).dtype == np.uint : “, np.sum(arr).dtype == np.uint) print (“Is np.sum(arr).dtype == np.float : “, np.sum(arr).dtype == np.float) Output Sum of arr : 36.2 Sum of arr(uint8) : 36 Sum of arr(float32) : 36.2 3 Is np.sum(arr).dtype == np.uint : False Is np.sum(arr).dtype == np.uint : True Arithmetic Mean Function Arithmetic mean function is suitable for computing the average of a list of numbers. The function returns the mean data set, given as parameters. The average is given by dividing the sum of the numbers with the count of the numbers in the list. Set of numbers: [n10, n20, n30, n40, n50] Sum of data-set = (n10 + n20 + n30 + n40 + n50) Number of data generated = 5 Average or arithmetic mean = (n10 + n20 + n30 + n40 + n50) / 5 data1 = [1, 3, 4, 5, 7, 9, 2] x = statistics.mean(data1) # Printing the mean print(“Mean is :”, x) Output Mean is: 4.428571428571429 Std, var Standard Deviation, Variance Standard deviation indicates the measure of spread and variation of a data set. The function is given by stdev( [data-set], xbar ) sample = [1, 2, 3, 4, 5] # Prints standard deviation # xbar is set to default value of 1 print(“Standard Deviation of sample is % s ” % (statistics.stdev(sample))) Output: Standard Deviation of the sample is 1.5811388300841898 4 Min, Max Minimum, and Maximum Min, max Minimum and maximum function calculates the maximum and minimum of the values passed in the argument. For the maximum, the function is max(j,k,l,..,key,default) # Python code to illustrate the functioning of # max() # printing the maximum of 8,24,40,36,98 print(“Maximum of 8,24,40,36 and 98 is : “,end=””) print (max(8,24,40,36,98 ) ) Output: Maximum of 8,24,40,36 and 98 is 98. For the minimum, the function is expressed as min(j,k,l,..,key,default) # printing the minimum of 8,24,40,36,98 print(“Minimum of 8,24,40,36 and 98 is : “,end=””) print (min(8,24,40,36,98 ) ) Output: Maximum of 8,24,40,36 and 98 is 8. Argmin and Argmax Argmin, argmax returns indices of the minimum and maximum elements of an array in a given axis, expressed as argmax(array, axis = None, out = None) array = geek.arrange(12).reshape(3, 4) print(“INPUT ARRAY : \n”, array) # No axis mentioned, so works on entire array print(“\nMax element : “, geek.argmax(array)) # returning Indices of the max element 5 # as per the indices print(“\nIndices of Max element : “, geek.argmax(array, axis=0)) print(“\nIndices of Max element : “, geek.argmax(array, axis=1)) Output: Input array: [[ 10 11 12 13] [ 14 15 16 17] [ 18 19 20 21]] Minimum element: 10 Max element: 21 Indices of Max element : [2 2 2 2] Indices of Max element : [3 3 3] Cumsum Functions Cumsum function is applied when calculating cumulative sum of array elements over a specific axis, expressed as cumsum(arr, axis=None, dtype=None, out=None) in_arr = geek.array([[2, 4, 6], [1, 3, 5]]) print (“Input array : “, in_arr) out_sum = geek.cumsum(in_arr) print (“cumulative sum of array elements: “, out_sum) Output: Input array : [[2 4 6] [1 3 5]] Cumulative sum of array elements: [ 2 6 12 13 16 21] 6 References Deitel, P., & Deitel, H. (2020). Introduction to data science: Measures of central tendency – Mean, median and mode. In Intro to python for computer science and data science. Pearson Education. Sayantan, J. (2018). Cumsum () in Python. Retrieved from https://www.geeksforgeeks.org/numpy-cumsum-in-python/ Wedin, O. (2008). Data filtering methods. Retrieved from https://cordis.europa.eu/docs/projects/cnect/5/215455/080/deliverables/ROADIDEA-D31-Data-filtering-methods-V1-1.pdf

Do you similar assignment and would want someone to complete it for you? Click on the ORDER NOW option to get instant services at essayloop.com

Do you have a similar assignment and would want someone to complete it for you? Click on the ORDER NOW option to get instant services at essayloop.com. We assure you of a well written and plagiarism free papers delivered within your specified deadline.