Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Introduction to Statistical programming in R and Python

Python Assessment 1  Mock Past Assessment

This assessment will be on Moodle. Questions 1 ‐ 6 will be multiple choice (or submit a value) questions where you are allowed multiple attempts, where you will receive full marks (2) for the first attempt, half marks (1) for the second attempt, and no marks (0) after that. For Questions 7 ‐ 10, you would be asked to copy and paste your code into a text box.

Task 1.  Assume we have a list of integers (whole numbers) called vinnys_list.  Which of the following options would count

the number of 1s in the list?

(a) vinnys_list.count()

(b) vinnys_list.count(1)

(c) count(vinnys_list)

(d) count(vinnys_list).(1)

Task 2.  Please select the option which represents the correct Python implementation of the following R code

v i n n y s _ l i s t 2   <−   c ( )

f o r ( i    i n    1 : 1 0 ) {

v i n n y s _ l i s t 2   <−   c ( v i n n y s _ l i s t 2  ,    i ^2)

}

(a) vinnys_list2   =  [ i ^2 for  i   in  range(10)]

(b) vinnys_list2   =  [ i ^2 for  i   in  range(1,11)]

(c) vinnys_list2   =  [ i**2 for  i   in  range(10)]

(d) vinnys_list2   =  [ i**2 for  i   in  range(1,11)]

[2 mark]

Task 3.  Which of the following represents a list comprehension to generate a list of tuples that contains all combinations of

1, 2 and 3, e.g.  [(1, 1), (1, 2), (1, 3), (2, 1), ..., (3, 3)]. Assume that x  =  [1,2,3]

(a)  [( i , j )  for   i   in  x  for  j   in  x]

(b)  [( i , i )  for   i   in  x  for  j   in  x]

(c)  [( j , j )  for   i   in  x  for  j   in  x]

(d)  [( i , j )  for   i   in  x  for  j   in   i ]

(e)  [( i , i )  for   i   in  x  for  j   in   i ]

(f)  [( j , j )  for   i   in  x  for  j   in   i ]                                                                                                                                                       [2 mark]

Task 4.  Assume that we have vector, x, created as a  NumPy array.   Which of the following would calculate the standard

deviation of the array?

(a) x . stddev()

(b) stddev(x)

(c) x . std ()

(d) standard_deviation(x)                                                                                                                                                                       [2 mark]

Task 5.  Calculate the Mean Squared Error for a simple regression model where all the matrices are known and given below. The calculation will be the mean of (y − XB)2 , where XB is a matrix multiplication of X and B . Please submit your answer answer as an integer, e.g. 20

i m p o r t   numpy   a s   np

X   =   np . a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )

B   =   np . a r r a y ( [ 1 , 2 , 3 ] )

y   =   np . a r r a y ( [ 2 , 5 , 8 ] )

Task 6.  Which of the following would find the elements that are in one set, s1, but not in another, s2?

(a) s1.diff(s2)

(b) s2.diff(s1)

(c) s2.difference(s1)

(d) s1.difference(s2)

[2 mark]

Task 7.  Wordle came to prominance as a game in 2021.   For this question we will be writing a function to help with the implementation of the same game but with numbers.  You will be writing a function called number_score’ which takes two lists of integers of any length. The first list will be your guess and the second list the true list of numbers. Your function will do the following:

(i) Check if the lists are the same length.  If they arent then raise an exception or value error

(ii) For two lists that are the same length, you will need to compare the numbers from each list (e.g. the numbers in the same position). You will then return a list of colours, where for each position you will specify the following colours:

  if the numbers are the same: return green

•  if the numbers are not the same, but the number in the guess appears anywhere in the true word: return orange’ (Note: that this is slightly different to what wordle would have done)

•  if the numbers are not the same and the number in the guess does not appear anywhere in the true word: return ‘red’

For example, the following would be returned for the guess [1,2,3,4,5] and true list of numbers [1,2,5,6,7]

p r i n t ( number _ score ( [ 1 , 2 , 3 , 4 , 5 ] ,    [ 1 , 2 , 5 , 6 , 7 ] ) )

#    [ ' green '  ,  ' green '  ,  ' red '  ,  ' red '  ,  ' orange  ' ]

because of the following logic:

  greenbecause the first position is the same for each list

  greenbecause the second position is the same for each list

•  ‘redbecause 3 is not in the second list

•  ‘redbecause 4 is not in the second list

  orangebecause the position is different in the two lists, but 5 is in the second list

Task 8.  The single parameter Box Cox transformation is given by

yi  = {  ,

with the two parameter version given by

yi  = { ,,

(1)

(2)

(a) Please write a function to compute the single parameter Box Cox transformation.  Your function should take as inputs, a list y’ and a parameter value lambda1’. Your function should check that all values of y are positive (i.e. y > 0)

and if not, raise an exception or value error.  It should return a list or NumPy array of the transformed input list. Hint 1: turning y into a NumPy array might make this easier. You can then use np.log()

Hint 2: make sure to name λ as lambda1’ not lambda’, otherwise you will have issues with lambda values    [3 mark]

(b) Please write a new function to compute the two parameter Box Cox transformation. Your function should take an inputs, a list y’ and two parameter values: ‘lambda1’ and lambda2’. Your function should check that all values of y + lambda2 are positive (i.e.  y + λ2  > 0), and if not, raise an exception or value error.  It should return a list or NumPy array of the transformed input list.

Hint 3:  Use you answer to part (a) as the starting point for part (b), but making sure to create a new function (with a new name)                                                                                                                                                                                                   [3 mark]

Task 9.  Please create a class called Housewith the following specifications.

(i) Initialise the class with the following

  a build date (‘build_date’) which should be accessible at any time

  the number of rooms (‘rooms’) which should be accessible at any time

•  a list of sale prices (‘ sale_prices’) which will begin empty

  an owner (‘owner’) which will start as None

(ii) Implement a function called purchase’ within the class to allow someone to purchase the house. This function will have as inputs the new_owner’ and the purchase price’. The function will update the owner and add the sale price to the recorded list of sale prices.

(iii) Implement another function called get_last_sale_price which returns the last price that someone paid for the house

If correctly implemented, your class should return the following (where the returned values are given after #)

my_ house   =   House ( 2 0 1 2 , 4 )

my_ house . purchase ( ' Bob '  ,    100)

my_ house . purchase ( ' Amy '  ,    150)

my_ house . purchase ( ' Vinny '  ,    200)

p r i n t ( my_ house . s a l e _ p r i c e s )

#   [ 1 0 0 ,   150 ,   200]

p r i n t ( my_ house . owner )

#   Vinny

my_ house . g e t _ l a s t _ s a l e _ p r i c e  ( )

#   200

[6 mark]

Task 10.  You have been asked by a retailer to implement a simple stock tracking system for their shop using a Python class.

Your tasks are as follows:

(i) Implement a class called Shopthat represents a single shop.

(ii) The class should be initialised such that it has stock’.  ‘stock’ will be initialised in the class and set to None as a default.  The user should however be able to provide initial stock in the form of a dictionary should they wish, e.g. {‘item1’: Amount1, ‘item2’: Amount2}

(iii) The class should be initialised such that it has a list of purchases that have taken place in the shop (‘purchase_history’). This will take the form of an empty list, which will become a list of the purchase dictionaries once purchases have   taken place

(iv) The class should have have a function called get_stock’ which returns the current stock in the shop as a dictionary in the form {‘item1’: Amount1, ‘item2’: Amount2}.

(v) The class should have a function called restock’ which takes as input a dictionary of new stock in the form {‘item1’: Amount1, ‘item2’: Amount2} that the shop has received. The restock function should update the stock’ that the shop has in it, combining the existing stock with the new stock.

(vi) The class should have a function called purchase’ which takes as an input a shopping list from a possible customer (in the form {‘item1’: Amount1, ‘item2’: Amount2}). The purchase function should firstly check if the shop has enough stock.

•  If there is not enough stock, a suitable exception should be raised and purchase_history’ and stock’ should not be changed.

•  If there is enough stock, the items from the shopping  list should  be  removed from the stock’ and an entry (dictionary in the form of {‘item1’: Amount1, ‘item2’: Amount2}) should be added to the purchase_history’

If correctly implemented, your class should return the following (where the returned values are given after #

shop   =   Shop ( {  ' a p p l e  ' : 1 0 } )

p r i n t ( shop . g e t _ s t o c k ( ) )

#      { ' a p p l e s  ' : 1 0 }

shop . p u r c h a s e ( {  ' a p p l e  ' : 5 } )

p r i n t ( shop . g e t _ s t o c k ( ) )

#      { ' a p p l e s  ' : 5 }

shop . r e s t o c k ( {  ' pear ' : 5  ,     ' a p p l e  ' :    1 0 } )

p r i n t ( shop . g e t _ s t o c k ( ) )

#      { ' a p p l e s ' : 1 5 ,     ' p e a r :    1 0 }

shop . p u r c h a s e ( {  ' pear ' : 1 5 } )

#   S o r r y ,    t h e r e    i s    not   enough   s t o c k

p r i n t ( shop . g e t _ s t o c k ( ) )

#   { ' a p p l e  ' :    1 5 ,    ' pear  ' :    1 0 }

p r i n t ( shop . p u r c h a s e _ h i s t o r y )

#    [ {  ' a p p l e  ' :    5 } ]

[10 mark]