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 figuresConcepts in Practice 15.1.1 §15.1 source ↗
- a.data visualization
- b.data cleanup
- c.data acquisition
Answer
Concepts in Practice 15.1.2 §15.1 source ↗
- a.3
- b.4
- c.5
Answer
Concepts in Practice 15.1.3 §15.1 source ↗
- a.document insights and visualization
- b.analyze data
- c.data cleaning and visualization
Answer
Concepts in Practice 15.1.4 §15.1 source ↗
- a.Python
- b.R
- c.Java
Answer
Concepts in Practice 15.1.5 §15.1 source ↗
- a.list
- b.NumPy
- c.array
Answer
Concepts in Practice 15.1.6 §15.1 source ↗
- a.true
- b.false
Answer
Concepts in Practice 15.2.1 §15.2 source ↗
ndarray object with one row and two columns?- a.
np.array([2, 3]) - b.
np.zeros(1, 2) - c.
np.zeros((1, 2))
Answer
np.zeros((1, 2))m by n matrix of all zeros, np.zeros((m,n)) can be used.Concepts in Practice 15.2.2 §15.2 source ↗
- a.
ndarray - b.
list - c.
array
Answer
ndarrayndarray 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 ↗
ndarray object compared to a list?- a.computational efficiency
- b.array-oriented computing
- c.memory efficiency
- d.all the above
Answer
Concepts in Practice 15.2.4 §15.2 source ↗
import numpy as np arr = np.array([[1, 2], [3, 4]]) out = 2 * arr print(out)
- a.
[[2 4] [8 16]]
- b.
[[2 4] [6 8]]
- c.
[[1 2] [3 4]]
Answer
[[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 ↗
ndarray?- a.
import numpy as np arr = np.array(2, 3)
- b.
import numpy as np arr = np.array([[1, 2], [1, 2], [1, 2]])
- c.
import numpy as np arr = np.array([[1, 2], [1, 2], [1, 2]]).T
Answer
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 ↗
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)
- a.
[[1 0] [0 4]]
- b.
[[1 2] [3 4]]
- c.
[[2 2] [3 5]]
Answer
[[1 0] [0 4]]
arr1 and arr2 is: [[1 0] [0 4]]
Concepts in Practice 15.3.1 §15.3 source ↗
- a.Series
- b.dictionary
- c.list
Answer
Concepts in Practice 15.3.2 §15.3 source ↗
- a.true
- b.false
Answer
Concepts in Practice 15.3.3 §15.3 source ↗
- a.Pandas provides integration with other libraries.
- b.Pandas supports heterogeneous data whereas NumPy supports homogenous numerical data.
- c.Pandas supports both one-dimensional and two-dimensional data structures.
Answer
Concepts in Practice 15.3.4 §15.3 source ↗
- a.
df.head() - b.
df.head(5) - c.both
Answer
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 ↗
unique() function do in a DataFrame when applied to a column?- a.returns the number of unique columns
- b.returns the number of unique values in the given column
- c.returns the unique values in the given column
Answer
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 ↗
- a.
describe() - b.
info() - c.
unique()
Answer
describe()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])?- a.
a - b.
b - c.
IndexError
Answer
aa.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])?- a.
a - b.
b - c.
20
Answer
20 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'])?- a.
c - b.
C - c.
b
Answer
c 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?- a.
df.loc[1:3, "A"] - b.
df.loc[0:2, "A"] - c.
df.loc[0:3, "A"]
Answer
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?- a.
df.iloc[2] - b.
df[2] - c.
df.loc[1]
Answer
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?- a.
df.loc[0, :] - b.
df.iloc[:, 0] - c.
df.loc["A"]
Answer
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?- a.
df[df.loc[:, "A"] % 2 == 0] - b.
df[df[:, "A"] % 2 == 0] - c.
df[df.loc["A"] % 2 == 0]
Answer
df[df.loc[:, "A"] % 2 == 0]Concepts in Practice 15.4.8 §15.4 source ↗
Null values?- a.
isnan() - b.
isnull() - c.
isnone()
Answer
isnull()isnull() returns a Boolean DataFrame representing whether data entries are Null or not.Concepts in Practice 15.4.9 §15.4 source ↗
df is given, which of the following replaces Null values with zeros?- a.
df.fillna(0) - b.
df.replacena(0) - c.
df.fill(0)
Answer
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 ↗
df is given, what does the expression df.isnull().sum() do?- a.Calculates sum of the non-
Nullvalues in each column - b.Calculates the number of
Nullvalues in the DataFrame - c.Calculates the number of
Nullvalues in each column
Answer
Null values in each columnsum() 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 ↗
- a.scatter plot
- b.histogram plot
- c.line plot
Answer
Concepts in Practice 15.5.2 §15.5 source ↗
- a.histogram plot
- b.bar plot
- c.box plot
Answer
Concepts in Practice 15.5.3 §15.5 source ↗
- a.line plot
- b.bar plot
- c.histogram plot
Answer
Concepts in Practice 15.5.4 §15.5 source ↗
x and y? import matplotlib.pyplot as plt # Data x = [1, 2, 3, 4, 5] y = [10, 15, 12, 18, 20]
- a.
plt.boxplot(x) - b.
plt.hist(y) - c.
plt.scatter(x, y)
Answer
plt.scatter(x, y)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 ↗
import matplotlib.pyplot as plt # Data categories = ['A', 'B', 'C', 'D'] values = [10, 15, 12, 18] plt.bar(categories, values)
- a.a histogram plot
- b.a bar plot
- c.a line plot
Answer
values variable.Concepts in Practice 15.5.6 §15.5 source ↗
- a.Matplotlib
- b.Plotly
- c.Pandas
Answer
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.