Python Programming (6 pt) Which one of the following statements are true about t
ID: 3587310 • Letter: P
Question
Python Programming
(6 pt) Which one of the following statements are true about the Python “lambda” construction?
While anonymous functions are defined using the def keyword, in Python, normal functions are defined using the lambda keyword.
Lambda is a tool for building function objects.
Lambda functions can have multiple expressions.
Lambda functions can have only one argument.
ANS:
(18 pt) The following is a list of tuples:
student_list=[(‘Kyle’, ‘100203’, 3.55),
(‘Jerry’, ‘100126’, 3.00),
(‘Alex’, ‘100201’, 2.80),
(‘Harry’, ‘100138’, 3.78)]
What does each of these function calls return?
sorted (student_list)
ANS:
sorted (student_list, key=lambda s: s[2])
ANS:
sorted (student_list, key=lambda s: s[3])
ANS:
(10 pt) Given these two lists,
a = [‘m’, ‘r’, ‘y’]
b = [‘e’, ‘r’, ‘!’]
What does the lists look like that are returned from the list function calls?
ab = zip (a, b)
list (ab)
ANS:
x, y = zip (*zip (a, b))
list(x)
list(y)
ANS:
(7 pt) Create the list comprehension for the following code and print it,
xy_tups = []
for x in ['m', 't', 'b']:
for y in ['b', 't', 'e']:
if x != y:
xy_tups.append ((x, y))
ANS:
Display the list:
ANS:
(25 pt) Given that A = np.arange (8) + 3
What is the output of
B, C, D = np.split (A, [2, 4])
print (B)
print (C)
print (D)
ANS:
A.reshape (2,4)
print (A)
ANS:
F = np.random.randint (2, 4, size=(2,4))
print (F)
ANS:
G = np.vstack([E, F])
print (G)
ANS:
H = G – 2
print (H)
ANS:
(6 pt) Given the follow code
tests = [ ‘Test 1’, ‘Midterm’, ‘Test 2’, ’Final’]
student1 = Series ([80, 92, 95, 83], index = tests)
student2 = Series ([92, 88, 99, 80], index = tests)
student3 = Series ([87, 91, 85, 93], index = tests)
tests_df = DataFrame (‘S1’: student1, ‘S2’: student2,
‘S3’: student3)
What command will give the mean score of each of the exams using one DataFrame function call. Display the results
ANS:
Display the results
ANS:
(5 pt) The following correlation matrix was created from the Sleep- In-Mammals dataset. There are several strong or medium strength relationships between these features. In particular, look at Exposure, rated from 1-5 with 5 being most exposed. Gestation, the length of time a fetus stays inside the mother is positively correlated with Exposure and is negatively correlated with Total Sleep. Does this makes sense? Do animals that are most exposed, that is, ones that have no dens to sleep in, need to be sleeping less and do they need to protect their infants longer by keeping them safe inside their mother?
Yes
No
ANS:
(16 pt) The K-means clustering of the Iris Dataset in Lab 7: PartA produced the confusion matrix shown below. The flower species are Versicolor (0), Setosa (1), and Virginica (0).
Row 0: Actual Versicolor
Row 1: Actual Setosa
Row 2: Actual Virginica
Predicted Class
Versicolor
Setosa
Virginica
Total
Actual Class
Versicolor
48
0
2
C1: 50
Setosa
0
50
0
C2: 50
Virginica
14
0
36
C3: 50
Total
62
50
38
150
You may leave the answers as fractions
Please calculate the following stats:
TC1: True Versicolor =
TC2: True Setosa =
TC3: True Virginica =
FC1C2: Setosa Classified as Versicolor =
FC1C3: Virginica Classified as Versicolor =
FC2C1: Versicolor Classified as Setosa =
FC2C3: Virginica Classified as Setosa =
FC3C1: Versicolor Classified as Virginica =
FC3C2: Setosa Classified as Virginica =
Sensitivity or Recall: TPR
TC1R: True Versicolor Rate =
TC2R: True Setosa Rate =
TC3R: True Virginica Rate =
Precision: PPV
C1PV: Versicolor Predictive Value =
C2PV: Setosa Predictive Value =
C3PV: Virginica Predictive Value =
Accuracy: ACC =
(7 pt) Which Machine Learning algorithm uses the Expectation–Maximization (E–M) algorithm? List the high-level steps for this machine-learning algorithm. Just copy from textbook the algorithm on page 465 of the PDF file.
ANS:
Predicted Class
Versicolor
Setosa
Virginica
Total
Actual Class
Versicolor
48
0
2
C1: 50
Setosa
0
50
0
C2: 50
Virginica
14
0
36
C3: 50
Total
62
50
38
150
Explanation / Answer
(6 pt) Which one of the following statements are true about the Python “lambda” construction?
Ans: Lambda is a tool for building function objects.
Lambda function can have multiple arguments but single expressions. Normal functions are defined using def keyword.
(18 pt) The following is a list of tuples:
student_list=[(‘Kyle’, ‘100203’, 3.55),
(‘Jerry’, ‘100126’, 3.00),
(‘Alex’, ‘100201’, 2.80),
(‘Harry’, ‘100138’, 3.78)]
What does each of these function calls return?
sorted (student_list)
Ans: sorted will return new sorted student_list sorted on first element of tuple
Sorted list will look like
sorted (student_list, key=lambda s: s[2])
ANS: sorted will return new sorted student_list sorted on third element of tuple
Sorted list will look like
sorted (student_list, key=lambda s: s[3])
ANS: This will complain as tuple only has three fields.
(10 pt) Given these two lists,
a = [‘m’, ‘r’, ‘y’]
b = [‘e’, ‘r’, ‘!’]
What does the lists look like that are returned from the list function calls?
ab = zip (a, b) This will return a list of tuples where first element of tuple from a adn second from b (
list (ab) = This will not do anything as ab is already a list
x, y = zip (*zip (a, b))
list(x)
list(y)
ANS: x will be same as a and y will be same as b
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.