Question 5.2. In math, the word alternating is sometimes used to indicate that the signs of...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Question 5.2. In math, the word "alternating" is sometimes used to indicate that the signs of the elements of a sequence oscillate back and forth between positive and negative. Complete the implementation of the function alternating_sign_mean, which takes in an array of positive numbers, values, and computes the mean of every element in the original array with alternating signs, starting with a positive sign for the element at position 0, a negative sign for the element at position 1, and so on. Example behavior is shown below. >>>alternating_sign_mean (np. array ( [2, 3.5, 4.5])) 1.0 # comes from (2 + (-3.5) + 4.5) / 3 >>> alternating_sign_mean (np. array( [2, 3.5, 1, 1.5])) -0.5 # comes from (2 + (-3.5) + 1 + (-1.5)) / 4 Hint: If x is an integer, x % 2 evaluates to 0 when x is even and to 1 when x is odd. Use this to help you figure out the signs. In [7]: def alternating_sign_mean (values): # Feel free to change this input to make sure your function works correctly. alternating_sign_mean (np.array ( [2, 3.5, 4.5])) In [8]: grader.check("q5_2") Out [8]: q5_2 results: q5_2 1 result: - Question 5.1. Complete the implementation of the function alternating_mean, which takes in an array of numbers, values, and computes the mean of every other element in the original array starting with the element at position 0. >>> alternating_mean (np.array ([2, 3.5, 1, 1.5])) 1.5 # comes from (2 + 1) / 2 >>> alternating_mean (np.array ([2, 3.5, 1, 1.5, 4.5])) 2.5 # comes from (2 + 1 + 4.5) / 3 Note: Once you've written your function, you should test it out on several arrays yourself to make sure it works as intended. Hint: Compute the mean by finding the sum of every other element in the array and dividing it by the number of elements that were summed. In [75]: values = np.array ([2, 3.5, 1, 1.5]) count=0 sum_=0 def alternating_mean (values): for i in range(0, len(values), 2): sum_+=values [i] values+=1 return sum_/count # Feel free to change this input to make sure your function works correctly. alternating_mean (np.array([2, 3.5, 1, 1.5])) Unbound LocalError Traceback (most recent call last) /tmp/ipykernel_190/1125179186.py in 11 12 # Feel free to change this input to make sure your function works correctly. ---> 13 alternating_mean(np.array ( [2, 3.5, 1, 1.5])) /tmp/inykernel 199/1125179186 ny in alternating mean (values) Question 5.2. In math, the word "alternating" is sometimes used to indicate that the signs of the elements of a sequence oscillate back and forth between positive and negative. Complete the implementation of the function alternating_sign_mean, which takes in an array of positive numbers, values, and computes the mean of every element in the original array with alternating signs, starting with a positive sign for the element at position 0, a negative sign for the element at position 1, and so on. Example behavior is shown below. >>>alternating_sign_mean (np. array ( [2, 3.5, 4.5])) 1.0 # comes from (2 + (-3.5) + 4.5) / 3 >>> alternating_sign_mean (np. array( [2, 3.5, 1, 1.5])) -0.5 # comes from (2 + (-3.5) + 1 + (-1.5)) / 4 Hint: If x is an integer, x % 2 evaluates to 0 when x is even and to 1 when x is odd. Use this to help you figure out the signs. In [7]: def alternating_sign_mean (values): # Feel free to change this input to make sure your function works correctly. alternating_sign_mean (np.array ( [2, 3.5, 4.5])) In [8]: grader.check("q5_2") Out [8]: q5_2 results: q5_2 1 result: - Question 5.1. Complete the implementation of the function alternating_mean, which takes in an array of numbers, values, and computes the mean of every other element in the original array starting with the element at position 0. >>> alternating_mean (np.array ([2, 3.5, 1, 1.5])) 1.5 # comes from (2 + 1) / 2 >>> alternating_mean (np.array ([2, 3.5, 1, 1.5, 4.5])) 2.5 # comes from (2 + 1 + 4.5) / 3 Note: Once you've written your function, you should test it out on several arrays yourself to make sure it works as intended. Hint: Compute the mean by finding the sum of every other element in the array and dividing it by the number of elements that were summed. In [75]: values = np.array ([2, 3.5, 1, 1.5]) count=0 sum_=0 def alternating_mean (values): for i in range(0, len(values), 2): sum_+=values [i] values+=1 return sum_/count # Feel free to change this input to make sure your function works correctly. alternating_mean (np.array([2, 3.5, 1, 1.5])) Unbound LocalError Traceback (most recent call last) /tmp/ipykernel_190/1125179186.py in 11 12 # Feel free to change this input to make sure your function works correctly. ---> 13 alternating_mean(np.array ( [2, 3.5, 1, 1.5])) /tmp/inykernel 199/1125179186 ny in alternating mean (values)
Expert Answer:
Answer rating: 100% (QA)
1 To implement the alternating sign mean function you can use the hint provided about the modulo ope... View the full answer
Related Book For
Probability And Statistics
ISBN: 9780321500465
4th Edition
Authors: Morris H. DeGroot, Mark J. Schervish
Posted Date:
Students also viewed these programming questions
-
.QUESTION .all the questions to be thoroughly explained Invocations of its type operations involve no writes to persistent memory and no output to clients. Concurrent processes may invoke the object....
-
Give Correct ANSWERS Human-Computer Interaction (a) If you had been one of the original inventors of the WIMP interface, and engineers on the technical team had been sceptical about the advantages...
-
All of the following predispose an individual to a urinary tract infection except 1. Renal calculi 2. Benign prostatic hypertrophy 3. Wilm's Tumor 4. Indwelling catheter Renal calculi is most...
-
Refer to the bond details in Problem 10-6A. Problem 10-6A Legacy issues $325,000 of 5%, four-year bonds dated January 1, 2013, that pays interest semiannually on June 30 and December 31. They are...
-
The unadjusted trial balance of Reliable Repairs at December 31, 2008, the end of the current year, is shown at the top of the next page. The data needed to determine year-end adjustments are as...
-
1. Multi-National Marriott Hotels Could Face Consumer Backlash and up to $1 Billion in Regulatory Fines and Litigation Costs for Massive Data Breach Marriott had sustained several cyberattacks prior...
-
Top managers of McDonough Products, Inc., have asked for your help in comparing the companys profit performance and financial position with the average for the industry. The accountant has given you...
-
a) Use X to define a NumPy array T, such that, 1) + (x 1) T = 1+2(x 1) + (x b) Find the difference between Y and T and print the result. E = Y-T c) Calculate and print the mean difference
-
A lot of 111 electronic products are inspected. A sample of 10 products is randomly selected. If 102 of the products are meeting the specifications, FIND the following: (a) The number of samples...
-
Define Requirements.
-
Which techniques are applied in software development process?
-
List any five inception artifacts.
-
Which of the following diagrams describe dynamic behavior of the software system? (a) Class diagram and object diagram (b) Use-case diagram and class diagram (c) Collaboration diagram and deployment...
-
What is Inception?
-
The following membrane lipid is a major component of the inner mitochondrial membrane: A. Lecithin B. Cephalin C. Cardiolipin D. Glycolipids E. Phsphatidyl-inositol
-
Eleni Cabinet Company sold 2,200 cabinets during 2011 at $160 per cabinet. Its beginning inventory on January 1 was 130 cabinets at $56. Purchases made during the year were as follows: February . 225...
-
Return to Example 6.2.7. a. Prove that the mins>0 (s) exp(snu) equals qn, where q is given in (6.2.16). b. Prove that 0 < q < 1. First, show that 0 < q <1 if u = 0. Next, let x = up + 1 p and show...
-
In the World Series of baseball, two teams A and B play a sequence of games against each other, and the first team that wins a total of four games becomes the winner of the World Series. If the...
-
Suppose that X1, . . . , Xn form a sequence of n Bernoulli trials for which the probability p of success on any given trial is unknown (0 p 1), and let T = Determine the form of the estimator...
-
Fill in the Blank. In a random process, the outcome of an experiment will be a function of some _________ such as time.
-
What is the covariance between two random variables \(X\) and \(Y\) ?
-
The joint density function of two random variables \(X\) and \(Y\) is given by \[p_{X, Y}(x, y)= \begin{cases}\frac{x y}{9}, & 0 \leq x \leq 2,0 \leq y \leq 3 \\ 0, & \text { elsewhere }\end{cases}\]...
Study smarter with the SolutionInn App