编程代写|ST2195 Programming for Data Science

Section A

(a) i[1]=4

(b) print(j+1)

(c) k[0]=1

(d) j[1]=4

(a) Not possible. A tuple cannot be changed.

(b) Not possible. Cannot perform numerical operation to a list.

(c) Not possible. A set is not indexed.

(d) Possible. A list can be changed.

(a) When we want to study the empirical density of a variable.

(b) When we want to compare frequencies of one variable across difffferent categories of another variable.

(c) When we want to monitor changes in the distribution of a variable across difffferent categories of another variable.

(d) When we want to explore the association between two continuous variables

(c) Appropriate. It provides the empirical density of the continuous variable across the categories of the other variable.

(a) When training a machine learning pipeline the main aim is to achieve a high training error.

(b) When training a machine learning pipeline the main aim is to achieve a moderate training error.

(c) When training a machine learning pipeline the main aim is to achieve a low training error.

(d) When training a machine learning pipeline achieving a low training error may not be the primary aim.

(d) The primary aim is to achieve a low test error ideally (but not necessarily) with a low training

as well.

(a) An IDE is an alternative operating system to Microsoft Windows.

(b) An IDE typically provides a source-code editor

(c) Some source-code editors provide auto-completion of code and syntax highlighting

(d) There are only 4 source-code editors for R and 3 for Python.

(e) A source-code editor for Python cannot be used for R

(f) An IDE is necessary for writing code.

(b), (c)

(a) Jupyter notebooks cannot handle Python code.

(b) R Markdown is an authoring framework that combines Markdown with R

(c) R Markdown fifiles cannot be opened without installing R fifirst.

(d) Jupyter Notebooks are open-source web-browser based applications.

(e) Jupyter notebooks were named after the fifirst names of its creators, Julia and Peter.

(f) R Markdown fifiles can be converted in a variety of formats including HTML, PDF, and Microsoft Word documents.

(b), (d), (f)

C1. vec = c(1, 4, 7)

C2. paste(“Hello”, “world”)

C3. import numpy

C4. library(“mlr”)

C5. phrase = “Hello world”; print(phrase.lower())”

C6. vec = (1, 4, 7)

C7. list(“a”, 5, 1:3)

C8. [“a”, 5, (1, 2, 3)]

if (mark >= 50)

print(“pass”)

if mark >= 50:

print(“pass”)

C11. plot(1:5, 2:6)

C12. df = pandas.read_csv(fdate + ‘.csv’)

C13. df = read.csv(paste0(fdate, “.csv”))

C14. head(fd)

C15. df.head()

C16. plt.subplots()

C17. ggplot(df, aes(x = x)) + geom_histogram()

C18. write.table(df, file = “df.csv”)

C19. apply(df, 2, sum)

C20. df[-c(1, 3, 4), ]

R: C1, C2, C4, C7, C9, C11, C13, C14, C17, C18, C19, C20

Python: C3, C5, C6, C8, C10, C12, C15, C16

Section B

(a) A list is also a data frame.

(b) A data frame is also a list.

(c) A data frame can contain data objects of difffferent types such as vectors and matrices.

(d) Data frames can contain lists.

(e) We can select the elements of both lists and data frames using their names

(a) Not necessarily, a list can contain vectors matrices, data frames even other lists. This is not the case for data frames.

(b) Correct. It can be viewed as a list of vectors of potentially difffferent type.

(c) Incorrect. It can only contain vectors of difffferent types.

(d) Incorrect. In fact, lists can contain data frames.

(e) Correct. This can be done using the $ sign.

(a) The rows of a table in a relational database represent records.

(b) An attribube in a relational database is a tuple of rows.

(c) SQLite uses a separate server process to operate.

(d) The SQL query

SELECT employee_id, salary, department

FROM employee

WHERE employee_id >= 102 AND salary >= 100

ORDER BY salary

returns all available records and attributes from the table employee that have employee_id greater or equal to 102 and salary greater or equal to 100, ordered in increasing salary.

(e) The following R code chunk

inner_join(employee, company, by = “sector”) %>%

filter(department == “HR”)

Find all records in tables employee and company that have matching values of sector, and return only those records where department is “HR”.

(a) Correct.

(b) Incorrect. An attribute in a relational database is a column of a table.

(c) Incorrect. SQLite, unlike other RDBMS does not require a separate server process to operate.

(d) Incorrect. The statement will return all available records for the attributes employee_id, salary, department under the stated conditions.

(e) Correct.

(a) Git is a repository hosting service for GitHub.

(b) A Git repository cannot be accessed without an internet connection.

(c) The command git add is used to add another user in the repository.

(d) Structured data are stored in a local hard drive, while unstructured data are in the cloud.

(e) CSV fifiles are special instances of XML fifiles.

(f) y x returns y modulo x.

(g) A dictionary in Python is a collection that is ordered, unchangable and indexed.

(h) The command matrix(1:12, nrow = 3) in R will create a matrix with 3 rows and 1 column with elements 1, 2, 3.

(i) A data frame in R can only hold factors and numeric variables.

(j) Mutable objects in Python are objects whose value changes depending on the operations performed on them.

(k) ggplot2 is an R system for data wrangling.

(a) GitHub is a repository hosting service for Git.

(b) A Git repository can be setup and accessed in a local computer.

(c) git add can be used to add fifile contents to the index.

(d) Structured data is data that is organized according to a predetermined set of rules; unstructured data sets are data sets for which it is diffiffifficult to have a predetermined set of rules for organizing them.

(e) A CSV fifile (comma delimited fifile) is not a special instance of XML fifile. CSV can be used to represent structured data is in two-dimensional arrays; XML is a markup language that defifines a set of rules for encoding information in objects.

(f) y x returns y raised to the power of x

(g) A dictionary is a collection which is unordered, changeable and indexed.

(h) matrix(1:12, nrow = 3) creates a matrix with 3 rows and 4 columns.

(i) A data frame in R can hold multiple variable types.

(j) Mutable objects are objects whose calues can be changed (e.g. lists, sets and dictionaries).

(k) ggplot2 is an R packages for graphics.

C1. git status

C2. print(type(2.3))

C3. paste(“type”, “‘float’”)

C4. git checkout master

O1. “type ‘float’”

O2. <type ‘float’>

O3. Switched to branch ‘master’

O4. On branch master

C1 – O4, C2 – O2, C3 – O1, C4 – O3

(a) Describe what graphs you would produce to demonstrate how the variables balance, and income affffect the likelihood of the person clearing the credit card outstanding balance in time

(b) Suppose that when you look at frequencies students tend to clear their outstanding credit card balances in time less often than the rest of the population. But if you focus on people with high outstanding credit card balances, students are more likely to pay in time than the rest of the population. Describe why could this be the case and what graphics you will use to depict that.

(a) A scatter plot of balance and income can be used by labeling the points according to cleared.

Also, side-by-side boxplots or violin plots separately for categories of cleared.

(b) This could imply that the reason students tend to miss payment is because they tend to have higher balances, and not because they are students per se. When compared with other members of the population with high balances they are actually more reliable. To depict that, we can use a grouped barplot of the cleared proportions separately for cases with balance being below and above its median.

(a) Provide the type of the machine learning models.

(b) Discuss the process of training the machine learning models indicating how the missing values will be handled.

(c) Discuss what criteria you will use to choose between the trained machine learning models.

(a) supervised learning, regression.

(b) The date can be split into a training and test set. The test set will be left aside and not used at all during the training process. The missing values can handled by one of the relevant methods, e.g. by fifilling with the sample average of this variable from the cases without missing values. It is important that this is done through a machine learning pipeline so that the sample average is calculate by points in the training dataset only. In order to tune the parameters of the machine learning pipelines with ridge regression and random forests, cross-validation can be used

(c) The trained and tuned machine learning pipelines will be used to obtain predictions to the test set (that was left aside in the process of part (b)). The best model will be the one with better predictive performance according to the mean squared error criterion.