本次CS代写的主要涉及如下领域: Python代写,香港浸会大学代写,COMP1007代写,香港程序代写
COMP1007 Introduction to Python and Its Applications
Practical Test 2
Due: April 23, 2020 (before 11:15am)
What to submit?
Save all your source codes as a single Python file, name it “<yourSSOid>-ptest2.py”, and submit it through the course page at BU Moodle. That is, if your SSOid is 17709394, then you should name your python file, 17709394-ptest2.py
About your submission file
- For the first few lines of your file, use a docstring (as indicated below) and write down your name and your SSOid. You should make use of inline comments to better explain what your code is doing and what are the assumptions that you have made.
"""
--------------------------------------------------------------------------
Practical Test 2
Name : CHU Xiaowen
SSOid: 17709394
--------------------------------------------------------------------------
"""
- Start each question with the special comment string #%% Question X such that we know where your program or function starts and ends.
#%% Question 1
...
#%% Question 2
...
#%% Question 3
...
Answer ALL SEVEN questions.
You have 1.5 hours and you should work on your own until 11:00am.
Submit your answers in one .py file through the submission box at the Course Moodle Page.
1. Write a NumPy function to add a vector to each row of a given matrix. Assume the vector has the same dimension as the row of the given matrix. Use the following test case to verify whether your function is correct or not: the input vector is [2, 3, 4] and the input matrix is [8910567], then the output matrix should be [1012147911].
(10 marks)
2. Write a NumPy program that generates ten 100 ×100 arrays whose data follows standard normal distribution. Then output the min, max, mean, and standard deviation of the data for each array. Next, sum up the ten 100 ×100 arrays into a single 100 ×100 array. Output the min, max, mean, and standard deviation of this new array.
(15 marks)
3. Write a Pandas program that does the following tasks:
(1) Create a Pandas Series object s with 100 random integers in the range of [0, 1000].
(2) Create another two Pandas Series objects s1 and s2 out from s. s1 contains the 50 elements of s with index 0, 2, 4, …, 98, while s2 contains the other 50 elements of s with index 1, 3, 5, …, 99.
(3) Compare the mean value of s1 and s2, and print the larger one with 2 decimal places.
(15 marks)
4. Create a Pandas DataFrame object from the dictionary data = {'state':['Ohio', 'Ohio', 'Ohio', 'nevada', 'Nevada'], 'year':[2000, 2003, 2002, 2001, 2002], 'pop':[1.5, 1.7, 3.6, 2.4, 2.9]}. Then you should display the data in two different ways. Firstly, show the data row by row. For each row, print the row index followed by a colon, then print the data of that row in the same line. Secondly, show the data column by column. For each column, print the column label followed by a colon, and then print the data of that column in the same line.
A sample output is given below:
0 : ['Ohio' 2000 1.5] 1 : ['Ohio' 2003 1.7] 2 : ['Ohio' 2002 3.6] 3 : ['nevada' 2001 2.4] 4 : ['Nevada' 2002 2.9] state : ['Ohio' 'Ohio' 'Ohio' 'nevada' 'Nevada'] year : [2000 2003 2002 2001 2002] pop : [1.5 1.7 3.6 2.4 2.9] |
(15 marks)
5. The csv files 'yob1880.csv' and 'yob1881.csv' have three columns: 'name', 'sex', 'births'. The 'births' column contains the number of births with the 'name' in each row. Write a Python program that does the followings:
(1) Load the CSV file 'yob1880.csv' into a DataFrame;
(2) Show the first 10 rows and the last 10 rows;
(3) Sort the DataFrame by the 'births' column in ascending order and apply the change to the DataFrame;
(4) Print the name with the largest value of 'births';
(5) GroupBy the DataFrame according to the 'sex' column. Then apply the sum(), min(), max(), and count() functions on the 'births' column;
(6) Load the CSV file 'yob1881.csv' into another DataFrame;
(7) Add a column 'year' to the DataFrame of 'yob1880.csv' with value 1880. Add a column 'year' to the DataFrame of 'yob1881.csv' with value 1881. Then Concatenate the two DataFrames as a single one;
(8) Create a pivot table from the concatenated DataFrame to analyze the total number of male births and female births for each year. The output should look like below (the column order doesn't matter):
sex |
F |
M |
year |
|
|
1880 |
xxxx |
xxxx |
1881 |
xxxx |
xxxx |
(20 marks)
6. Use Matplotlib to draw the following scatter plot which includes 80 points, whose values are drawn from function y = cos(x), 0 ≤ x ≤ 20. The size and color of each point are randomly generated. The colormap is viridis. The title of the plot is "A Scatter Cosine Wave with Random Size and Color".
(10 marks)
7. Use matplotlib to plot the following figure with four subplots, which are corresponding to the following four functions: (1) y = sin(2πt); (2) y = sin(4πt); (3) y = sin(8πt); (4) y = sin(16πt). The value of t ranges from 0 to 4.0. In the title of each subplot, you can use "$\pi$" to show the character π.
(15 marks)
End of Practical Test