Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.

Chapter 15: Data Science

34 problems · 0 figures

Concepts in Practice 15.1.1 §15.1 source ↗

What is the first stage of any data science life cycle?
  1. a.
    data visualization
  2. b.
    data cleanup
  3. c.
    data acquisition

Answer

C. data acquisition
Data acquisition is the first stage of the data science life cycle. Data can be collected by the data scientist or gathered previously and provided to the data scientist.

Concepts in Practice 15.1.2 §15.1 source ↗

How many stages does the data science life cycle have?
  1. a.
    3
  2. b.
    4
  3. c.
    5

Answer

B. 4
The data science life cycle has four stages: 1) data acquisition, 2) data exploration, 3) data analysis, and 4) reporting.

Concepts in Practice 15.1.3 §15.1 source ↗

What does a data scientist do in the data exploration stage?
  1. a.
    document insights and visualization
  2. b.
    analyze data
  3. c.
    data cleaning and visualization

Answer

C. data cleaning and visualization
The data exploration stage includes data cleaning and visualization.

Concepts in Practice 15.1.4 §15.1 source ↗

Between Python, R, and Java, which is the most popular language in data science?
  1. a.
    Python
  2. b.
    R
  3. c.
    Java

Answer

A. Python
Python is the most popular programming language in data science.

Concepts in Practice 15.1.5 §15.1 source ↗

Which of the following is a data science-related library in Python?
  1. a.
    list
  2. b.
    NumPy
  3. c.
    array

Answer

B. NumPy
NumPy is a Python library used in numerical data analysis.

Concepts in Practice 15.1.6 §15.1 source ↗

Google Colaboratory can be used for reporting and sharing insights.
  1. a.
    true
  2. b.
    false

Answer

A. true
Google Colaboratory is a Google document that can be shared with other Google accounts for viewing or editing the content.

Concepts in Practice 15.2.1 §15.2 source ↗

Which of the following creates an ndarray object with one row and two columns?
  1. a.
    np.array([2, 3])
  2. b.
    np.zeros(1, 2)
  3. c.
    np.zeros((1, 2))

Answer

C. np.zeros((1, 2))
To create an m by n matrix of all zeros, np.zeros((m,n)) can be used.

Concepts in Practice 15.2.2 §15.2 source ↗

Which of the following is a NumPy data type?
  1. a.
    ndarray
  2. b.
    list
  3. c.
    array

Answer

A. ndarray
ndarray is a data type supported by NumPy. When printing the type of an ndarray object, <'numpy.ndarray'> is printed.

Concepts in Practice 15.2.3 §15.2 source ↗

What is the benefit of using anndarray object compared to a list?
  1. a.
    computational efficiency
  2. b.
    array-oriented computing
  3. c.
    memory efficiency
  4. d.
    all the above

Answer

D. all the above
NumPy array is optimized for computational and memory efficiency while also offering array-oriented computation.

Concepts in Practice 15.2.4 §15.2 source ↗

What is the output of the following code?
import numpy as np

arr = np.array([[1, 2], [3, 4]])
out = 2 * arr
print(out)
  1. a.
    [[2 4]
    [8 16]]
  2. b.
    [[2 4]
    [6 8]]
  3. c.
    [[1 2]
    [3 4]]

Answer

B.
[[2 4]
[6 8]]
2 * arr multiplies all elements by 2 resulting in:
[[2 4]
[6 8]]

Concepts in Practice 15.2.5 §15.2 source ↗

Which of the following results in a 2 by 3 ndarray?
  1. a.
    import numpy as np
    arr = np.array(2, 3)
  2. b.
    import numpy as np
    arr = np.array([[1, 2], [1, 2], [1, 2]])
  3. c.
    import numpy as np
    arr = np.array([[1, 2], [1, 2], [1, 2]]).T

Answer

C.
import numpy as np
arr = np.array([[1, 2], [1, 2], [1, 2]]).T
np.array([[1, 2], [1, 2], [1, 2]]) creates an ndarray of 3 by 2. The T operator transposes the array. The transpose operator takes an array of m by n and converts the array to an n by m array. The result of applying the transpose operator on the given array is a 2 by 3 array.

Concepts in Practice 15.2.6 §15.2 source ↗

The function np.multiply(arr1, arr2) receives two ndarray objects arr1 and arr2 with the same dimensions, and performs element-wise multiplication. What is the output of the following code?
import numpy as np

arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[1, 0], [0, 1]])
out = np.multiply(arr1, arr2)
print(out)
  1. a.
    [[1 0]
    [0 4]]
  2. b.
    [[1 2]
    [3 4]]
  3. c.
    [[2 2]
    [3 5]]

Answer

A.
[[1 0]
[0 4]]
The result of element-wise multiplication between arr1 and arr2 is:
[[1 0]
[0 4]]

Concepts in Practice 15.3.1 §15.3 source ↗

Which of the following is a Pandas data structure?
  1. a.
    Series
  2. b.
    dictionary
  3. c.
    list

Answer

A. Series
Series is a Pandas data structure representing one-dimensional labeled data.

Concepts in Practice 15.3.2 §15.3 source ↗

A DataFrame object can be considered as a collection of Series objects.
  1. a.
    true
  2. b.
    false

Answer

A. true
A DataFrame object can be considered a collection of one-dimensional labeled objects represented by Series objects.

Concepts in Practice 15.3.3 §15.3 source ↗

What are the benefits of Pandas over NumPy?
  1. a.
    Pandas provides integration with other libraries.
  2. b.
    Pandas supports heterogeneous data whereas NumPy supports homogenous numerical data.
  3. c.
    Pandas supports both one-dimensional and two-dimensional data structures.

Answer

B. Pandas supports heterogeneous data whereas NumPy supports homogenous numerical data.
The Pandas DataFrame can store columns of varying data types, while NumPy only supports numeric data types.

Concepts in Practice 15.3.4 §15.3 source ↗

Which of the following returns the top five rows of a DataFrame?
  1. a.
    df.head()
  2. b.
    df.head(5)
  3. c.
    both

Answer

C. both
The function head() returns a DataFrame's top rows. If an argument is not specified, the default number of returned rows is five.

Concepts in Practice 15.3.5 §15.3 source ↗

What does the unique() function do in a DataFrame when applied to a column?
  1. a.
    returns the number of unique columns
  2. b.
    returns the number of unique values in the given column
  3. c.
    returns the unique values in the given column

Answer

C. returns the unique values in the given column
The unique() function when applied to a column returns the unique values (rows) in the given column.

Concepts in Practice 15.3.6 §15.3 source ↗

Which function generates statistical information of columns with numerical data types?
  1. a.
    describe()
  2. b.
    info()
  3. c.
    unique()

Answer

A. describe()
When the function describe() is applied to a DataFrame, summary statistics of numerical columns will be generated.

Concepts in Practice 15.4.1 §15.4 source ↗

Given the following code, respond to the questions below.

import pandas as pd

# Create sample data
data = {
  "A": ["a", "b", "c", "d"],
  "B": [12, 20, 5, -10],
  "C": ["C", "C", "C", "C"]
}

df = pd.DataFrame(data)
What is the output of print(df.iloc[0, 0])?
  1. a.
    a
  2. b.
    b
  3. c.
    IndexError

Answer

A. a
The element in the first row and the first column is a.

Concepts in Practice 15.4.2 §15.4 source ↗

Given the following code, respond to the questions below.

import pandas as pd

# Create sample data
data = {
  "A": ["a", "b", "c", "d"],
  "B": [12, 20, 5, -10],
  "C": ["C", "C", "C", "C"]
}

df = pd.DataFrame(data)
What is the output of print(df.iloc[1, 1])?
  1. a.
    a
  2. b.
    b
  3. c.
    20

Answer

C. 20
The element in the second row and the second column is 20 .

Concepts in Practice 15.4.3 §15.4 source ↗

Given the following code, respond to the questions below.

import pandas as pd

# Create sample data
data = {
  "A": ["a", "b", "c", "d"],
  "B": [12, 20, 5, -10],
  "C": ["C", "C", "C", "C"]
}

df = pd.DataFrame(data)
What is the output of print(df.loc[2, 'A'])?
  1. a.
    c
  2. b.
    C
  3. c.
    b

Answer

A. c
The element at row label 2 and column label A is c.

Concepts in Practice 15.4.4 §15.4 source ↗

Given the following code, respond to the questions.

import pandas as pd

# Create sample data
data = {
  "A": [1, 2, 3, 4],
  "B": [5, 6, 7, 8],
  "C": [9, 10, 11, 12]
}

df = pd.DataFrame(data)
Which of the following returns the first three rows of column A?
  1. a.
    df.loc[1:3, "A"]
  2. b.
    df.loc[0:2, "A"]
  3. c.
    df.loc[0:3, "A"]

Answer

B. df.loc[0:2, "A"]
df.loc[0:2, "A"] returns rows 0 to 2 (inclusive) of column A.

Concepts in Practice 15.4.5 §15.4 source ↗

Given the following code, respond to the questions.

import pandas as pd

# Create sample data
data = {
  "A": [1, 2, 3, 4],
  "B": [5, 6, 7, 8],
  "C": [9, 10, 11, 12]
}

df = pd.DataFrame(data)
Which of the following returns the second row?
  1. a.
    df.iloc[2]
  2. b.
    df[2]
  3. c.
    df.loc[1]

Answer

C. df.loc[1]
loc[1] returns the row with label 1 , which corresponds to the second row of the DataFrame.

Concepts in Practice 15.4.6 §15.4 source ↗

Given the following code, respond to the questions.

import pandas as pd

# Create sample data
data = {
  "A": [1, 2, 3, 4],
  "B": [5, 6, 7, 8],
  "C": [9, 10, 11, 12]
}

df = pd.DataFrame(data)
Which of the following returns the first column?
  1. a.
    df.loc[0, :]
  2. b.
    df.iloc[:, 0]
  3. c.
    df.loc["A"]

Answer

B. df.iloc[:, 0]
iloc[:, 0] selects all rows corresponding to the column index 0, which equals returning the first column.

Concepts in Practice 15.4.7 §15.4 source ↗

Given the following code, respond to the questions.

import pandas as pd

# Create sample data
data = {
  "A": [1, 2, 3, 4],
  "B": [5, 6, 7, 8],
  "C": [9, 10, 11, 12]
}

df = pd.DataFrame(data)
Which of the following results in selecting the second and fourth rows of the DataFrame?
  1. a.
    df[df.loc[:, "A"] % 2 == 0]
  2. b.
    df[df[:, "A"] % 2 == 0]
  3. c.
    df[df.loc["A"] % 2 == 0]

Answer

A. df[df.loc[:, "A"] % 2 == 0]
The condition returns all rows where the value in the column with label A is divisible by 2.

Concepts in Practice 15.4.8 §15.4 source ↗

Which of the following is used to check the DataFrame for Null values?
  1. a.
    isnan()
  2. b.
    isnull()
  3. c.
    isnone()

Answer

B. isnull()
isnull() returns a Boolean DataFrame representing whether data entries are Null or not.

Concepts in Practice 15.4.9 §15.4 source ↗

Assuming that a DataFrame df is given, which of the following replaces Null values with zeros?
  1. a.
    df.fillna(0)
  2. b.
    df.replacena(0)
  3. c.
    df.fill(0)

Answer

A. df.fillna(0)
fillna() replaces all Null values with the provided value passed as an argument.

Concepts in Practice 15.4.10 §15.4 source ↗

Assuming that a DataFrame df is given, what does the expression df.isnull().sum() do?
  1. a.
    Calculates sum of the non-Null values in each column
  2. b.
    Calculates the number of Null values in the DataFrame
  3. c.
    Calculates the number of Null values in each column

Answer

C. Calculates the number of Null values in each column
The function sum() is applied to each column separately and sums up values in columns. The result is the number of Null values in each column.

Concepts in Practice 15.5.1 §15.5 source ↗

Which of the following plot types is best suited for comparing the distribution of a continuous variable?
  1. a.
    scatter plot
  2. b.
    histogram plot
  3. c.
    line plot

Answer

B. histogram plot
A histogram is used to plot the distribution of continuous variables.

Concepts in Practice 15.5.2 §15.5 source ↗

Which of the following plot types is best suited for visualizing outliers and quartiles of a continuous variable?
  1. a.
    histogram plot
  2. b.
    bar plot
  3. c.
    box plot

Answer

C. box plot
A box plot is used to show the distribution of a continuous variable, along with visualizing outliers, minimum, maximum, and quartiles.

Concepts in Practice 15.5.3 §15.5 source ↗

Which of the following plot types is effective for displaying trends and changes over time?
  1. a.
    line plot
  2. b.
    bar plot
  3. c.
    histogram plot

Answer

A. line plot
A line plot is used to show trends and changes over time between two variables.

Concepts in Practice 15.5.4 §15.5 source ↗

Given the following code, which of the function calls is appropriate in showing association between x and y?
import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [10, 15, 12, 18, 20]
  1. a.
    plt.boxplot(x)
  2. b.
    plt.hist(y)
  3. c.
    plt.scatter(x, y)

Answer

C. plt.scatter(x, y)
The function call plt.scatter(x, y) creates a scatter plot based on the data stored in the variables x and y.

Concepts in Practice 15.5.5 §15.5 source ↗

What is being plotted using the code below?
import matplotlib.pyplot as plt

# Data
categories = ['A', 'B', 'C', 'D']
values = [10, 15, 12, 18]
plt.bar(categories, values)
  1. a.
    a histogram plot
  2. b.
    a bar plot
  3. c.
    a line plot

Answer

B. a bar plot
The given code plots a bar plot with four bars representing the given categories. The height of the bars corresponds to the values stored in the values variable.

Concepts in Practice 15.5.6 §15.5 source ↗

Which library in Python is more commonly used for creating interactive visualizations?
  1. a.
    Matplotlib
  2. b.
    Plotly
  3. c.
    Pandas

Answer

B. Plotly
Plotly is a library that creates interactive visualizations.

End of the book.

Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.