Flattening arrays with flatten vs. ravel) Create a 2-by-3 array containing the first six powers of 2 beginning with 20. Flatten the array first with method flatten, then with ravel. In each case, display the result then display the original array to show that it was unmodified.
Question #2
a) Use vertical stacking to create the 4-by-2 array named array3 with array1 stacked on top of array2.
b) Use horizontal stacking to create the 2-by-4 array named array4 with array2 to the right of array1.
c) Use vertical stacking with two copies of array4 to create a 4-by-4 array5.
d) Use horizontal stacking with two copies of array3 to create a 4-by-4 array6.
Question #3 In this lecture, we discussed shallow vs. deep copies of arrays. Python’s built-in list and dictionary types have copy methods that perform shallow copies. Using the following dictionary dictionary = {‘Sophia’: [97, 88]}
demonstrate that a dictionary’s copy method indeed performs a shallow copy. To do so,call copy to make the shallow copy, modify the list stored in the original dictionary, then display both dictionaries to see that they have the same contents.
Next, use the copy module’s deepcopy function to create a deep copy of the dictionary.
Modify the list stored in the original dictionary, then display both dictionaries to
prove that each has its own data.
Question #4
In this chapter, we used %timeit to compare the average execution times of generating a list of 6,000,000 random die rolls vs. generating an array of 6,000,000 random die rolls. Though we saw approximately two orders of magnitude performance improvement with array, we generated the list and the array using two different random- number generators and different techniques for building each collection. If you use the same techniques we showed to generate a one-element list and a one-element array, creating the list is slightly faster. Repeat the %timeit operations for one-element collections. Then do it again for 10, 100, 1000, 10,000, 100,000, and 1,000,000 elements and compare the results on your system
fill the below table and discuss which is better.
Number of Values
List average execution time
array average execution time
1
10
100
1000
10,000
100,000
1,000,000
Question #5
(Perform the following tasks with pandas Series:
a) Create a Series from the list [7, 11, 13, 17].
Number of values List average execution time array average execution time
1 1.56 μs ± 25.2 ns 1.89 μs ± 24.4 ns
10 11.6 μs ± 59.6 ns 1.96 μs ± 27.6 ns
100 109 μs ± 1.61 μs 3 μs ± 147 ns
1000 1.09 ms ± 8.59 μs 12.3 μs ± 419 ns
10,000 11.1 ms ± 210 μs 102 μs ± 669 ns
100,000 111 ms ± 1.77 ms 1.02 ms ± 32.9 μs
1,000,000 1.1 s ± 8.47 ms 10.1 ms ± 250 μs
Exercises 279
b) Create a Series with five elements that are all 100.0.
c) Create a Series with 20 elements that are all random numbers in the range 0 to
100. Use method describe to produce the Series’ basic descriptive statistics.
d) Create a Series called temperatures of the floating-point values 98.6, 98.9,
100.2 and 97.9. Using the index keyword argument, specify the custom indices
‘Julie’, ‘Charlie’, ‘Sam’ and ‘Andrea’.
e) Form a dictionary from the names and values in Part (d), then use it to initialize
a Series.
Question # 6
(Pandas: DataFrames) Perform the following tasks with pandas DataFrames:
a) Create a DataFrame named temperatures from a dictionary of three temperature
readings each for ‘Maxine’, ‘James’ and ‘Amanda’.
b) Recreate the DataFrame temperatures in Part (a) with custom indices using
the index keyword argument and a list containing ‘Morning’, ‘Afternoon’
and ‘Evening’.
c) Select from temperatures the column of temperature readings for ‘Maxine’.
d) Select from temperatures the row of ‘Morning’ temperature readings.
e) Select from temperatures the rows for ‘Morning’ and ‘Evening’ temperature
readings.
f) Select from temperatures the columns of temperature readings for ‘Amanda’
and ‘Maxine’.
g) Select from temperatures the elements for ‘Amanda’ and ‘Maxine’ in the
‘Morning’ and ‘Afternoon’.
h) Use the describe method to produce temperatures’ descriptive statistics.
i) Transpose temperatures.
j) Sort temperatures so that its column names are in alphabetical order.
Last Completed Projects
topic title | academic level | Writer | delivered |
---|