Now that we've learned about NumPy let's test your knowledge. We'll start off with a few...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Now that we've learned about NumPy let's test your knowledge. We'll start off with a few simple tasks, and then you'll be asked some more complicated questions. Import NumPy as np In [1]: import numpy as np Create an array of 10 zeros In [3]: np.zeros (10) Out [3]: array([0., e., e., e., e., e., e., e., e., e.]) Create an array of 10 ones In [4]: np.ones (10) Out[4]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) Create an array of 10 fives In [6]: np.full(10,5) Out [6]: array([5, 5, 5, 5, 5, 5, 5, 5, 5, 5]) Create an array of the integers from 10 to 50 In [7]: np.arange (10,51) Out [7]: array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, зе, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5e]) Create an array of all the even integers from 10 to 50 In [8]: np.arange (10,51,2) Out [8]: array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 3e, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]) Create an array of all the even integers from 10 to 50 In [8]: np.arange(10,51,2) Out [8]: array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]) Create a 3x3 matrix with values ranging from 0 to 8 In [10]: np.arange (0,9).reshape (3,3) Out[10]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) Create a 3x3 identity matrix In [11]: np.eye(3) Out [11]: array([[1., e., e.], [0., 1., е.], [0., e., 1.]]) Use NumPy to generate a random number between 0 and 1 In [12]: np.random.rand (1) Out[12]: array([0.04962707]) Use NumPy to generate an array of 25 random numbers sampled from a standard normal distribution In [13]: np.random.randn(25) Out[13]: array ([-0.63282827, -0.18902161, -0.51478523, -0.49530685, -0.61810597, , -0.57429369, 1.03630513, -0.24253243, -0.80656955, 1.552805 -0.13157291, -0.4952518, -0.78311892, 0.00591945, 0.38177906, -0.45719942, 0.43382632, -0.80893512, -0.25909398, 0.93741917, 1.27878852, -0.74325549, 0.14408309, 0.7018985 , 0.71068245]) Create the following matrix: In [39]: np.arange (1,101).reshape (10,10)/100 Out[39]: array ([[0.01, e.e2, 0.03, e.04, e.05, 0.06, 0.07, 0.08, e.09, e.1 ], [ө.11, е.12, е.13, е.14, е.15, в.16, в.17, в.18, е.19, е.2 1, [0.21, 0.22, e.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ], [0.31, 0.32, e.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ], [0.41, 0.42, 0.43, 0.44, e.45, 0.46, 0.47, e.48, 0.49, 0.5 ], [0.51, 0.52, 0.53, 0.54, e.55, 0.56, 0.57, 0.58, 0.59, 0.6 ], [0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ], [0.71, 0.72, e.73, e.74, e.75, 0.76, 0.77, 0.78, 0.79, 0.8 ], [0.81, 0.82, e.83, e.84, 0.85, 0.86, 0.87, 0.88, e.89, 0.9 ], [0.91, 0.92, e.93, 0.94, e.95, 0.96, 0.97, 0.98, 0.99, 1. ]]) Create an array of 20 linearly spaced points between 0 and 1: In [16]: np.linspace(0,1,20) Out[16]: array([0. , e.05263158, 0.10526316, e.15789474, 0.21052632, 0.26315789, e.31578947, 0.36842105, e.42105263, e.47368421, 0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211, 0.78947368, 0.84210526, 0.89473684, 0.94736842, 1. ]) Numpy Indexing and Selection Now you will be given a few matrices, and be asked to replicate the resulting matrix outputs: In [18]: mat = np.arange (1,26).reshape (5,5) mat Out[18]: array ([[ 1, 2, 3, 4, 5], 9, 10], [ 6, [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]) 7, 8, In [27]: # WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW # BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T # BE ABLE TO SEE THE OUTPUT ANY MORE mat[2:,1:] Out[27]: array([[12, 13, 14, 15], [17, 18, 19, 20], [22, 23, 24, 25]]) In [28]: # WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW # BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T # BE ABLE TO SEE THE OUTPUT ANY MORE mat[3][4] Out [28]: 20 In [30]: # WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW # BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T # BE ABLE TO SEE THE OUTPUT ANY MORE mat[0:3,1].reshape (3,1) Out[30]: array([[ 2], [ 7], [12]]) In [40]: mat[e:3,1:2] Out [40]: array ([[ 2], [ 7], [12]]) In [31]: # WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW # BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T # BE ABLE TO SEE THE OUTPUT ANY MORE mat[4] Out[31]: array([21, 22, 23, 24, 25]) In [33]: # WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW # BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON 'T # BE ABLE TO SEE THE OUTPUT ANY MORE mat[3:] Out [33]: array ([[16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]) Now do the following Get the sum of all the values in mat In [35]: np.sum(mat) Out[35]: 325 Get the standard deviation of the values in mat In [36]: np.std(mat) Out[36]: 7.211102550927978 Get the sum of all the columns in mat In [38]: mat.sum(axis=e) Out[38]: array([55, 60, 65, 70, 75]) Now that we've learned about NumPy let's test your knowledge. We'll start off with a few simple tasks, and then you'll be asked some more complicated questions. Import NumPy as np In [1]: import numpy as np Create an array of 10 zeros In [3]: np.zeros (10) Out [3]: array([0., e., e., e., e., e., e., e., e., e.]) Create an array of 10 ones In [4]: np.ones (10) Out[4]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]) Create an array of 10 fives In [6]: np.full(10,5) Out [6]: array([5, 5, 5, 5, 5, 5, 5, 5, 5, 5]) Create an array of the integers from 10 to 50 In [7]: np.arange (10,51) Out [7]: array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, зе, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5e]) Create an array of all the even integers from 10 to 50 In [8]: np.arange (10,51,2) Out [8]: array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 3e, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]) Create an array of all the even integers from 10 to 50 In [8]: np.arange(10,51,2) Out [8]: array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]) Create a 3x3 matrix with values ranging from 0 to 8 In [10]: np.arange (0,9).reshape (3,3) Out[10]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) Create a 3x3 identity matrix In [11]: np.eye(3) Out [11]: array([[1., e., e.], [0., 1., е.], [0., e., 1.]]) Use NumPy to generate a random number between 0 and 1 In [12]: np.random.rand (1) Out[12]: array([0.04962707]) Use NumPy to generate an array of 25 random numbers sampled from a standard normal distribution In [13]: np.random.randn(25) Out[13]: array ([-0.63282827, -0.18902161, -0.51478523, -0.49530685, -0.61810597, , -0.57429369, 1.03630513, -0.24253243, -0.80656955, 1.552805 -0.13157291, -0.4952518, -0.78311892, 0.00591945, 0.38177906, -0.45719942, 0.43382632, -0.80893512, -0.25909398, 0.93741917, 1.27878852, -0.74325549, 0.14408309, 0.7018985 , 0.71068245]) Create the following matrix: In [39]: np.arange (1,101).reshape (10,10)/100 Out[39]: array ([[0.01, e.e2, 0.03, e.04, e.05, 0.06, 0.07, 0.08, e.09, e.1 ], [ө.11, е.12, е.13, е.14, е.15, в.16, в.17, в.18, е.19, е.2 1, [0.21, 0.22, e.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ], [0.31, 0.32, e.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ], [0.41, 0.42, 0.43, 0.44, e.45, 0.46, 0.47, e.48, 0.49, 0.5 ], [0.51, 0.52, 0.53, 0.54, e.55, 0.56, 0.57, 0.58, 0.59, 0.6 ], [0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ], [0.71, 0.72, e.73, e.74, e.75, 0.76, 0.77, 0.78, 0.79, 0.8 ], [0.81, 0.82, e.83, e.84, 0.85, 0.86, 0.87, 0.88, e.89, 0.9 ], [0.91, 0.92, e.93, 0.94, e.95, 0.96, 0.97, 0.98, 0.99, 1. ]]) Create an array of 20 linearly spaced points between 0 and 1: In [16]: np.linspace(0,1,20) Out[16]: array([0. , e.05263158, 0.10526316, e.15789474, 0.21052632, 0.26315789, e.31578947, 0.36842105, e.42105263, e.47368421, 0.52631579, 0.57894737, 0.63157895, 0.68421053, 0.73684211, 0.78947368, 0.84210526, 0.89473684, 0.94736842, 1. ]) Numpy Indexing and Selection Now you will be given a few matrices, and be asked to replicate the resulting matrix outputs: In [18]: mat = np.arange (1,26).reshape (5,5) mat Out[18]: array ([[ 1, 2, 3, 4, 5], 9, 10], [ 6, [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]) 7, 8, In [27]: # WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW # BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T # BE ABLE TO SEE THE OUTPUT ANY MORE mat[2:,1:] Out[27]: array([[12, 13, 14, 15], [17, 18, 19, 20], [22, 23, 24, 25]]) In [28]: # WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW # BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T # BE ABLE TO SEE THE OUTPUT ANY MORE mat[3][4] Out [28]: 20 In [30]: # WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW # BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T # BE ABLE TO SEE THE OUTPUT ANY MORE mat[0:3,1].reshape (3,1) Out[30]: array([[ 2], [ 7], [12]]) In [40]: mat[e:3,1:2] Out [40]: array ([[ 2], [ 7], [12]]) In [31]: # WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW # BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T # BE ABLE TO SEE THE OUTPUT ANY MORE mat[4] Out[31]: array([21, 22, 23, 24, 25]) In [33]: # WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW # BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON 'T # BE ABLE TO SEE THE OUTPUT ANY MORE mat[3:] Out [33]: array ([[16, 17, 18, 19, 20], [21, 22, 23, 24, 25]]) Now do the following Get the sum of all the values in mat In [35]: np.sum(mat) Out[35]: 325 Get the standard deviation of the values in mat In [36]: np.std(mat) Out[36]: 7.211102550927978 Get the sum of all the columns in mat In [38]: mat.sum(axis=e) Out[38]: array([55, 60, 65, 70, 75])
Expert Answer:
Answer rating: 100% (QA)
Important Numpy as np In 1 import numpy as np create ... View the full answer
Related Book For
Posted Date:
Students also viewed these accounting questions
-
Create an array of 300 sequentially numbered elements from 1-300 Display these items in a matrix of 12 X 25 (rows to columns) Randomly choose number until it is found. Apply the binary search routine...
-
Create an array of size given by the user. Write a function to find a number taken as input from user. Your task is to print the index of the number using pointer to travel the array. Input Format...
-
Write an application that contains an array of 10 multiple-choice quiz questions related to your favorite hobby. Each question contains three answer choices. Also create an array that holds the...
-
The total cost of 2 chair and 1 table is 210 dollars. The total cost of 1 chair and 2 tables is 285 dollars. What is the cost of 1 chair?
-
A friend suggests that organizational behaviour courses are useful only to people who will enter management careers. Discuss the accuracy of your friends statement.
-
Your company sells customized computer systems to businesses large and small around the world. The Executive Committee needs to recommend someone to begin a three-year term as Manager of Eastern...
-
In question 1, identify the marketing as opposed to the business strategy. Data From Question 1: What is a business strategy? Do you agree with the definition proposed? Illustrate your answer with...
-
Sarah OHann enjoyed taking her first auditing course as part of her undergraduate accounting program. While at home during her semester break, she and her father discussed the class and it was clear...
-
State suitable case for each model Hierarchical Model Network data model Relational model Object-oriented data model
-
Allen Furniture is a manufacturer of hand-crafted furniture. At the start of January, Allen employs 20 trained craftspeople. They have forecasted their labor needs over the next 12 months as shown in...
-
List and explain 3 reasons why companies would take the risk of colluding by sharing confidential information, thereby depriving consumers of a competitive marketplace and facing federal charges.
-
What is a release?
-
Mila adopts religious ethical standards. These involve an element of a. compassion. b. cost-benefit analysis. c. discretion. d. utilitarianism.
-
Helen, an undercover police officer, pressures Pete to buy stolen goods. When he does so, he is arrested and charged with dealing in stolen goods. Pete will likely be a. acquitted because he was...
-
The president can hold acts of Congress and of the courts unconstitutional. (True/False)
-
What is the definition of white-collar crime?
-
The maintenance Work Order (WO) can be only scheduled once: Select one: The planner is assured that the request is performed. Planner is assured that the labor, parts, materials, and equipment are...
-
The baseball player A hits the ball from a height of 3.36 ft with an initial velocity of 34.8 ft/s. 0.14 seconds after the ball is hit, player B who is standing 15 ft away from home plate begins to...
-
Suppose the cost schedule for a perfectly competitive firm producing brooms is: If the market price is $5, how many brooms would the firm produce? Would the firm be making economic profit? How could...
-
Why do firms in oligopoly produce many brands of the same good?
-
What is the issue of corporate governance? To what extent does it affect shareholders?
-
At January 1, 2015, Cheng Company reported retained earnings of 20,000,000. In 2015, Cheng discovered that 2014 depreciation expense was understated by 4,000,000. In 2015, net income was 9,000,000...
-
Cherokee Construction Company changed from the cost-recovery to the percentage-of-completion method of accounting for long-term construction contracts during 2015. For tax purposes, the company...
-
In 2015, Bailey Corporation discovered that equipment purchased on January 1, 2013, for 50,000 was expensed at that time. The equipment should have been depreciated over 5 years, with no residual...
Study smarter with the SolutionInn App