Practice Hub
Practice fundamental mathematics and programming concepts for Data Science at The George Washington University.
What is the cardinality of \lvert \mathcal{A} \times \mathcal{B} \times \mathcal{C} \rvert?
\mathcal{A} = \{a, b, c\}, \ \mathcal{B} = \{1, 2\}, \ \mathcal{C} = \{\text{I}, \text{II}, \text{III}\}
The cardinality of the Cartesian product \mathcal{A} \times \mathcal{B} \times \mathcal{C} is the product of the cardinalities of the individual sets:
\begin{align*} \lvert \mathcal{A} \times \mathcal{B} \times \mathcal{C} \rvert &= \lvert \mathcal{A} \rvert \cdot \lvert \mathcal{B} \rvert \cdot \lvert \mathcal{C} \rvert \\ &= 3 \cdot 2 \cdot 3 \\ &= 18 \end{align*}
What are the elements of \mathcal{A}^3?
\mathcal{A} = \{1,2\}
The correct answer is \{ (1,1,1), (1,1,2), (1,2,1), (1,2,2), (2,1,1), (2,1,2), (2,2,1), (2,2,2) \} because the cartesian product \mathcal{A}^3 consists of all ordered triples where each element of the triple is taken from the set \mathcal{A}.
Since \mathcal{A} has 2 elements, there are 2^3 = 8 possible ordered triples.
Suppose we have the following function f:\mathcal{A} \to \mathcal{B} defined as follows:
\mathcal{A} = \{ 0, 1, 2 \}, \ \mathcal{B} = \{ 2, 3, 4, 5 \}, \ f = \{ (0, 2), (1, 2), (2, 3) \}
Find f(2).
To find f(2), we need to look at the pairs in the function definition where the first element is 2. From the given function f, we see that the only pair with 2 as the first element is (2, 3). Therefore, f(2) = 3.
Suppose we have the following function f:\mathbb{N} \to \mathbb{Z}. What is the domain, codomain and range of this function?
The correct answer is domain: \mathbb{N}, codomain: \mathbb{Z}, range: \mathbb{N}. The function f is defined as:
f:\underbrace{\mathbb{N}}_{\text{domain}} \to \underbrace{\mathbb{Z}}_{\text{codomain}};
The range is the set of all actual outputs of the function. Since f maps natural numbers to natural numbers, the range is \mathbb{N}.
Suppose the following functions are defined:
f:\mathbb{N} \to \mathbb{Z}, \ \ g:\mathbb{R} \to \mathbb{Z}
Is f=g?
The answer is False, since the domains of f and g are different. Two functions are equal if they have the same domain, codomain, and output for every input in the domain.
Given the following function f:\mathcal{A} \to \mathcal{B} with:
\mathcal{A} = \{1, 2, 3\}, \ \ \mathcal{B} = \{a, b, c, d\}
Which of the following sets represents a function f that is neither injective nor surjective?
The correct answer is the second option f = \{(1, a), (2, a), (3, b)\} because it is not injective (both 1 and 2 map to ‘a’) and not surjective (elements ‘c’ and ‘d’ in \mathcal{B} are not mapped to by any element in \mathcal{A}).
Suppose the following functions:
f: \mathbb{R} \to \mathbb{Z}, \ \ g: \mathbb{Z} \to \mathbb{N};
What is the composite function g \circ f?
The correct answer is g \circ f: \mathbb{R} \to \mathbb{N} because the output of f (which is in \mathbb{Z}) becomes the input of g (which takes inputs from \mathbb{Z}), and the output of g is in \mathbb{N}. Therefore, the composite function g \circ f takes inputs from \mathbb{R} and produces outputs in \mathbb{N}.
The function f:\mathbb{Z} \times \mathbb{Z} \to \mathbb{Z} \times \mathbb{Z} defined by f(a,b) = (a + b, a + 2b) is bijective. Find f^{-1}(a,b)
The correct answer is f^{-1}(a,b) = (2a - b, b - a). This can be found by solving the following system of equations:
\begin{align*} x + y &= a \\ x + 2y &= b \end{align*}
Subtracting the first equation from the second gives:
\begin{align*} x + y &= a \\ y &= b - a \end{align*}
Substituting this value of y into the first equation gives:
\begin{align*} x &= 2a - b \\ y &= b - a \end{align*}
Thus, the inverse function is given by:
f^{-1}(a,b) = (2a - b, b - a)
You can confirm by checking the composite function f(f^{-1}(a,b)) = (a,b).
\begin{align*} f(f^{-1}(a,b)) &= f(2a - b, b - a) \\ &= ((2a - b) + (b - a), (2a - b) + 2(b - a)) \\ &= (a, b) \end{align*}
Which of the following represents the elements of this set?
\{x \in \mathbb{Z}: x^2 - 2 = 0\}
The \emptyset is the correct answer given that there are no integers x such that x^2 - 2 = 0. The solutions to the equation x^2 - 2 = 0 are x = \sqrt{2} and x = -\sqrt{2}, neither of which are integers. Therefore, the set contains no elements.
Which of the shaded regions matches the following expression?
(\mathcal{A} \cup \mathcal{B}) \cap \mathcal{C}
The correct answer is the third option. We first take the union of sets \mathcal{A} and \mathcal{B}, which includes all elements that are in either set. Then, we find the intersection with set \mathcal{C}, which includes only those elements that are also in set \mathcal{C}.
Find \bar{\mathcal{A}}, given:
\mathcal{A} = \{1,2,3\}, \ \ \mathcal{U} = \{0,1,2,3,4,5\}
The correct answer is \{0, 4, 5\}. This is the complement of set \mathcal{A} with respect to the universal set \mathcal{U}. The complement \bar{\mathcal{A}} includes all elements in \mathcal{U} that are not in \mathcal{A}.
Which of the following is not a set?
The set \mathcal{A} is not a set because it contains the element 3 twice. In set theory, sets cannot have duplicate elements; each element must be unique.
What is the cardinality of the following set:
\lvert \mathcal{A} \rvert = \{ \{ \} \}
The set \mathcal{A} contains one element, which is the empty set \{ \}. Therefore, the cardinality of the set \mathcal{A} is 1.
The following sets are equal:
\{ \ldots -2, -1, 0, 1, 2, \ldots \} = \{ 0, -1, 1, -2, 2, \ldots \}
The two sets are equal because they contain the same elements. The first set lists all integers, while the second set rearranges them. Therefore, the statement is true.
Does the following element belong to the set:
\{\} \in \{1, 2, 3, \ldots\}
The correct answer is False because the empty set \{\} is not one of the elements in the set \{1, 2, 3, \ldots\}. The elements of the set are the natural numbers starting from 1, and the empty set is not included among them.
Determine whether \pi is an element of the set of real numbers \mathbb{R}:
\pi \in \mathbb{R}
The expression \pi \in \mathbb{R} is True given that \pi (approximately 3.14159) is a real number. The set of real numbers, denoted by \mathbb{R}, includes all rational and irrational numbers, and since \pi is an irrational number, it belongs to the set of real numbers.
List the following elements of this set:
\{ \mathcal{X} : \mathcal{X} \subseteq \{\text{Python}, \text{R}, \text{SQL}\} \ \text{and} \ \lvert \mathcal{X} \rvert = 2 \}
The correct answer is \big\{\, \{\text{Python}, \text{R}\}, \{\text{Python}, \text{SQL}\}, \{\text{R}, \text{SQL}\} \,\big\} since these are the only subsets of the set \{\text{Python}, \text{R}, \text{SQL}\} that have exactly 2 elements.
Calculate the slope from these two points (2, 3) and (5, 11).
An event has probability P(A) = 0.34. Compute the probability of the complement using the prob_a variable:
P(A^c) = 1 - P(A)
Compute the mean of the data list.
Create a 3 \times 3 identity matrix I as a list of lists.
A student has a dictionary of short notes for three courses. Compute the total number of characters across all notes using the formula:
\text{Total Characters} = \sum (\text{length of each string})
Two points are represented as tuples u and v. Compute the Euclidean distance between the points using the formula:
\| \mathbf{u} - \mathbf{v} \|_2 = \sqrt{ \sum_{i=1}^n (u_i - v_i)^2 }
Compute the range of the data list.
Calculate the percentage change if price goes from 120\$ to 150\$ (answer should be in decimal format; i.e. 0.3 for 30\% )
A student has grades: 80 (weight 30\%), 90 (weight 50\%), 70 (weight 20\%). Calculate the grade of the student.
Two independent events A and B have probabilities P(A)=0.3 and P(B)=0.5. Calculate the probability that both events occur P(A \cap B).
A population of 1000 grows at 5\% per year, compounded annually. Calculate population after 3 years using:
P = P_{0}*(1 + r)^t
A person weighs 72 kg and is 1.78 meters tall. Calculate their Body Mass Index (BMI) using the weight and height variables:
\text{BMI} = \frac{\text{weight}}{\text{height}^2}
Convert 21^\circ C to Fahrenheit using the Celsius C variable:
F = C \times \frac{9}{5} + 32
Compute the z-score for a value x of 87, with mean mu of 75 and standard deviation sigma of 8:
z = \frac{x - \mu}{\sigma}
A dataset has a standard deviation sigma of 6.5. Compute the variance:
\sigma^2 = \sigma \times \sigma
You are given a list of exam_scores. Compute the median of this dataset using indexing.
Assign a class based on two features:
- “Class A” if
feature1is greater than 5 andfeature2is less than or equal to 3, - “Class B” if
feature1greater than 5 andfeature2greater than 3, - Otherwise “Class C”.
Compute the expected value \mathbb{E}[X] of a discrete random variable X with outcomes x_i and probabilities p_i:
\mathbb{E}[X] = \sum_i x_i \ p_i
Compute the variance of the dataset using the definition:
\mathrm{Var}(X) = \frac{1}{n}\sum_{i=1}^{n}(x_i - \bar{x})^2
Build all possible 2-character combinations from a list of strings.
Given data values x_1, x_2, \ldots, x_n and bins defined as intervals, compute the number of observations falling into each bin using a loop.
Bins used in this exercise:
- [0, 10) for index 0
- [10, 20) for index 1
- [20, 30) for index 2
Given values x_1, x_2, \ldots, x_n, their min–max scaled version is
x_i^{\text{scaled}} = \frac{x_i - x_{\min}}{x_{\max} - x_{\min}}
Use a loop to compute the scaled values for the dataset.
You are given a dictionary course. The “course_id” contains a long alphanumeric identifier.
Slice the 5th to 14th characters of “course_id”.
Sort the list of data below and return the number that sits 25\% of the way into the sorted list.
Given a 30 \times 30 matrix represented as a list of lists. Access the element a_{15,7}.
A product_code was entered with an extra character “#”. The “#” is an error and it is located at the 27th index of the string.
Create a cleaned version of the string where the “#” is removed.
You are given a category “A” or “B”. Create a one-hot encoded list of length 2, where:
- Category A - [1,0]
- Category B - [0,1]
Threshold the prediction of whether to buy or not by checking whether the probability is greater than 50\%:
Determine if the model prediction is correct by comparing predicted and actual values:
Compute the ReLU function of x by following the formula:
\text{ReLU}(x) = \begin{cases} x, & \text{if } x > 0 \\ 0, & \text{if } x \le 0 \end{cases}
The absolute error between a true value y and prediction \hat{y} is
\lvert y - \hat{y} \rvert
Compute the absolute error for each pair of values in the abs_error() function and return the error list.
Create a class Text() that stores a string and returns the first word. Modify the first_word() method to return the first word in a string.
Use the math module to compute the factorial of 15! using the factorial function.
Use the Counter() function from collections to count occurrences in a data list.
Use the random library to generate 5 random integers between 1 and 10 using the function randint().
Compute the dot product between a matrix and a vector using numpy with the function dot().
A\mathbf{x} = \mathbf{b}
Using pandas package find the mean of column “A” using the method mean() of the data DataFrame object.
In Data Science, we often normalize a list of values so they sum to 1:
p_i = \frac{x_i}{\sum_j x_j}.
Normalize the numbers using the normalize() function and return the probs list.
A Bernoulli random variable takes values 1 (success) or 0 (failure). Its probability mass function is
P(X = x) = \begin{cases} p & \text{if } x = 1, \\ 1-p & \text{if } x = 0. \end{cases}
Compute the probability of each outcome if p = 0.3 using the bernoulli() function and return the probs list.
The Fibonacci sequence is defined by
F_1 = 1,\quad F_2 = 1,\quad F_{n} = F_{n-1} + F_{n-2}.
Use the function fib() to generate the first 8 Fibonacci numbers and return the sequence list.
Consider the polynomial represented by f(x)
f(x) = x^3 + 2x^2 + 5x + 10.
Compute the polynomial function f() for each value in the list below and return the list ys.
Create a class Counter() that counts how many numbers in a dataset are above a given threshold. Modify the count_above() method to return the count of all values above the threshold.
Create a class Scaler() that multiplies all numbers in a dataset by a scaling factor. Modify the scale() method to return the list of scaled values.
Create a class Text() that stores a string and returns the number of words. Modify the word_count() method to return the total count of words.
Create a class Text() that stores a string and counts how many times a given character appears. Modify the count_char() method to return the total count of characters.