Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 4 (A). Read about array broadcasting One particularly nice thing about numpy arrays is that you can (sometimes) add them, even if they are

image text in transcribed
Problem 4 (A). Read about array broadcasting One particularly nice thing about numpy arrays is that you can (sometimes) add them, even if they are different shapes. This is called array broadcasting. The three rules of broadcasting are: 1) If one array has fewer dimenensions than the other, is one with fewer dimensions is padded with ones on the left. Example: If a has shape 3 and b has shape 1 by 3 and you try to type a+b, it will automatically pad a 1 to the start of a so that both arrays have shape 1 by 3. 2) Suppose two arrays have the same number of dimensions, but different shapes. If one of shapes has length one along a given dimension, then that array is "stretched out" along that direction. Example: Suppose A=array([1,2]) (shape is 2) an B=array(([1,2],[3,4])). Numpy first converts A to an array with shape (1,2) (by rule 1) and then converts it to an array with shape (2,2) by stretching it out, so the result is array([[1,2],[1,2]]). It does this conversion, then performs the addition so the final answer is A+B=array([2,4],[4,6]) 3) Suppose two arrays have the same number of dimensions, but different shapes along a given dimension. If neither has length one, then there is nothing you can do and an error is raised. Example: lfA has shape (3,4) and B has shape(4,3) then A+B will yield an error. For more information, and many more examples read these notes from the Python Data Science Handbook. (B). - Review Unittest Review, if needed, the unittest module for constructing automated unit tests in Python. You may wish to refer to the required reading from that week, the lecture notes or the lecture notes/videos. (C). - Apply Unittest to Array Broadcasting Implement an automated test class called TestBroadcastingRules which tests the three rules of array broadcasting

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

1. What kind of brand is Body Shop Carrot Mois- ture Cream?

Answered: 1 week ago