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?
-
Why is the cash flow of a residential mortgage loan unknown?
-
What information should a lessee disclose about its capital leases in its financial statements and footnotes?
-
The compressive strength, in kilopascals, was measured for concrete blocks from five different batches of concrete, both three and six days after pouring. The data are as follows: Can you conclude...
-
A production line at V.J. Sugumarans machine shop has three stations. The first station can process a unit in 10 minutes. The second station has two identical machines, each of which can process a...
-
a) Make a list of all compensation-related legislation in Saint Vincent and the Grenadines Give a brief description of at least four pieces of legislation. Does the legislation adequately cover...
-
The focus of this project is to create a master budget for the Williams Company based on the companys industry outlook, recent company outcomes, and the companys business rules. You will create a...
-
Question 1 A 5 m chain with density 2 kg/m hangs off the roof of a building. How much work is required to pull the chain to the roof? Question 2 The trough below is full of water. Recall that water...
-
A raft loaded with rocks is floating in a swimming pool. We mark the level of water in the swimming pool and on the raft. If we drop the rocks into the pool, what will happen to the water level in...
-
Analyse the security procedures that BMW must implement to guarantee that inventory is recorded accurately with as few errors as feasible.
-
Marigold Company has sales of $500000, variable costs of $449500, and fixed costs of $28000. Ivanhoe Company has sales of $500000, variable costs of $198000, and fixed costs of $242000. Marigold's...
-
XYZ is a respectable midsized banking group in India that differentiates itself from its competitors through its attention to customer service and satisfaction. The bank also the lower interest rates...
-
Solve. log3 (p) = 5 p = Question Help: Video Message instructor Calculator Submit Question
-
Masthead Corporation was organized on January 1 and issued 725,000 shares of common stock on that date. On July 1, an additional 350,000 shares were issued for cash. Net income for the year was...
-
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?
-
When there is a concentration gradient in the system, show that the potential gradient is composed of two terms, (i) an Ohm's-law contribution and (ii) a diffusional contribution. State the equation...
-
Copper is deposited at a cathode from solution with a bulk concentration of \(0.5 \mathrm{M}\) at the rate of \(3.0 \mathrm{~g} / \mathrm{m}^{2} \cdot \mathrm{s}\). Find the surface concentration of...
-
Find the mobility of \(\mathrm{H}^{+}, \mathrm{OH}^{-}\), and other ions from the diffusivity data given in Table 22.1. Table 22.1. Diffusion coefficients of ions in water at 25 C. H+ Na+ K+ Ca+ OH-...
Study smarter with the SolutionInn App