Foundations of Data Science
What are the three different forms in which you can define a set?
1. Statement form = "all even numbers less than 10" 2. Set-Roster form = listed in {}, e.g. {1,2,3,4} 3. Set-Builder form = {x : x is even and x < 10}
How to define the size of a result matrix?
1. take the number of rows from the first matrix to find the first dimension 2. and the number of columns from the second matrix to find the second dimension.
What kind of error types exists?
Below are typically overcome with try / except , if else or assert statements 1. IndexError --> e.g access beyond limits of a list 2. TypeError --> e.g trying to convert inappropriate value, mix of data types 3. NameError --> e.g refering to non-existent variable 4. SyntaxError --> e.g forgetting quotation or parenthesis 5. ValueError --> correct type but an inappropriate value 6. ZeroDivisionError --> cant divide with 0 7. IOError --> eg file not foiund
What is the term for the set of all possible solutions for a linear system?
The solution set of the linear system
What are syntax and semantic errors?
The syntax error is an incorrect construction of the source code, whereas a semantic error is an error logic that produces the wrong result when executed.
What does A ∪ B mean?
The union of A and B, thus all the elements that are in A, B or Both.
What is the range of a relation in set theory?
The values in the co-domain that are related to the domain. In this example the range of f is {s , v}
When is a dictionary better than a list?
To pair key values with an associated input. E.g If you want to store fx name and age, it is better to use dictionaries as you can store them as key,value pair e.g. Niklas, 23, Marina, 24 etc. This is faster and more pythonic than saving these values in two separate lists.
What is a Python program?
a program is a sequence of definitions and commands (think input / output), there are commands executed by Python interpreter in a shell
x = {1} A = {1,2,3} Is x ⊆ A OR x ∈ A? x ⊆ A = x is a subset of A x ∈ A = x is an element of A
x ⊆ A, x is a subset of A
A = {1,2,3} B = {3,4,5} What is A∪B?
{1,2,3,4,5} = All the elements in both of the sets. Observe how "3" is not written out two times, the two sets will be merged with no duplicates. _æ--
What is the set cardinality of |A x B|? A = {1,2,3,4} B = {a,b,c}
|A x B| = 4 * 3 = 12
How do you denote the empty set {}?
Ø
What is the difference between a set, a function, and a relation?
- Sets: All these are sets - Relation: A relation between A and B is a subset of the Cartesian product A x B. Relations are a group of ordered pairs from one set of objects to another set of objects while - Function: . A function is a relation for which every element of the domain corresponds to (or maps to) exactly one element of the range. Functions are relations that connect one set of inputs to another set of outputs. So all functions are relations while all relations are not functions.
How to overcome logic error (hard)?
- Think before writting new code - Draw pictures, take a break - explain code to someone else
What does the following sentences mean? 1) for any (x,y) ∈ R x R 2) (x,y) ∈ C if x^2 + y^2 = 1
1) For any ordered pair (x,y) in the two sets R and R 2) the ordered pair (x,y) belongs to the relation C if they fulfill the condition: x^2 + y^2 = 1 Example (x,y) = (1,0) = 1^2 + 0^2 = 1 True. They belong to C
How does Python work with indexing and what rules apply?
1) Index always starts at 0 2) you use square brackets to perform indexing and get the value from an index / position. 3) you can access the last positions writing [-1]. Note that here it is not -0.
What are the conditions for a function in set theory?
1) That every element in X is a part of a pair 2) That no ordered pairs have the same first element, thus each element in the domain corresponds to only one element of the co-domain. 3) A relation from a set X to a set Y is called a function if each element of X is related to exactly one element in Y.
What are the three types of mathematical statements?
1) Universal. "For all" - a given condition is true for all elements in a set. Symbol: ∀ e.g. in a calender year it is universal that number of days is > 365 2) Conditional. "If p then q" - If condition A is true then condition B must also be true. E.g. if leap year, then 366 days in calender year. Note, universal conditional that feb is < 30 days 3) Existential. "For some" - e.g there is a month with < 30 days. Symbol: ∃, universal existential across all years that a month < 30 days
What is considered good programming?
1) more code is not necessarily a good thing 2) measure good programmers by the amount of functionality 3) introduce functions 4) mechanism to achieve decomposition (breaking problems into smaller and digestible parts) and abstraction (filtering out unnecessary things to focus on things that matter)
How to use exceptions and assertions?
1. An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions. Example: raise exceptions if users supply bad data input. 2. Assertions are statements that you can use to set sanity checks during the development process. Assertions allow you to test the correctness of your code by checking if some specific conditions remain true, which can come in handy while you're debugging code. Use assertions as / to - Supplement to testing - check types of argument - check constraint - check for violations
What types of error handling exist?
1. Try / Except: The try block lets you test a block of code for errors. If an error occurs, the except block lets you handle the error e.g insert a NameError and a print statement 2. If else: 3. Assert: Checks if criteria are fulfilled. If not, then an error message will be printed. 4. Raise statements:
What are different classes of tests?
1. Unit testing: Testing each function separately 2. Regression testing: catch reintroduced errors that were previously fixed, often performed right after each update or commit to the code base to identify new bugs 3. Integration testing: does the overall program work?
How to find a linear dependence relation?
1. Validate that it is linearly dependent 2. Reduce augmented matrix to reduced echelon form (complete reduction) 3. Write system of equations
How to find the basis of a vector space?
1. Write augmented matrix 2. Reduce to echelon form 3. Setup and solve system of linear equations 4. Write the span of vectors for the solved vector 5. Conclude on dependency and dimensionality --> if linearly independent then it forms a basis (remember vector space is linearly independent)
What are the advantages of OOP
1. bundle data into packages (see picture) together with procedures that work on them through well-defined interfaces 2. divide-and-conquer development • implement and test behavior of each class separately • increased modularity reduces complexity 3. classes make it easy to reuse code • many Python modules define new classes • each class has a separate environment (no collision on function names) • inheritance allows subclasses to redefine or extend a selected subset of a superclass' behavior
How to find the inverse? and proof it? (REVISE)
2 ways 1. Using formula: For a 2 x 2 matrix swap a and d position, and add a negative sign to b and c. Multiply with 1 / ( ad-bc). 2. Algorithm: Augment the matrix with an identity matrix, row reduce so that the original matrix is in row echelon form. The now converted identity matrix shows the inverse matrix. Proof/validate: Dotproduct of matrix should equal [1, 0] [0,1] Note: If 2 x 2 matrix remembers to first check that the determinants are not equal to 0 to validate that the matrix is invertible. If ad - bc = 0, then the matrix is not invertible. Also, a matrix is invertible if the number 0 is not an eigenvalue of A.
How many ways can you compute the dot product of two matrices?
2 ways. 1. By definition, computed separately (split the matrix into vectors) 2. by row-column rule (what we typically use)
What is the basis of something in linear algebra?
A basis is a linearly independent spanning set (note this relates to the basis of respectively row, column, null and vector space) In mathematics, a set B of vectors in a vector space V is called a basis if every element of V may be written in a unique way as a finite linear combination of elements of B. The coefficients of this linear combination are referred to as components or coordinates of the vector with respect to B. Therefore null space of a matrix is not nessecearily
Which data objects are mutable and immutable?
A mutable object is an object whose state can be modified after it is defined. The opposite of a mutable object is an immutable object, whose state cannot be altered after it is initially defined. Examples of immutable objects in Python include integers, floats, strings, and tuples. Some of the mutable data types in Python are list, dictionary, set and user-defined classes.
What is a solution of a system of linear equations?
A solution is a list of x numbers (one for each variable) that makes each equation in the linear system true. EXAMPLE: 2x1 - x2 + 1.5x3 = 8 x1 = 5 x2 = 6.5 x3 = 3 The equation simplifies to 8 = 8 The solution to the linear equation is (5,6.5,3)
What is a vector sub space? and how many properties does it contain?
A subspace of a vector space V is a subset H of V which is itself a vector space with respect to the addition and scalar multiplication in V. A subspace of a vector space satisfies three conditions: 1. the zero vector is in H 2. H is closed under addition 3. H is closed under multiplication of scalars
What is a vector space?
A vector space is a nonempty set V of objects, called vectors, on which are defined two operations, called addition and multiplication by scalars (real numbers), subject to the ten axioms (or rules) listed in image. The axioms must hold for all vectors u, v, and w in V and for all scalars c and d.
What is a trivial solution?
Boring solutions = 0 are considered trivial (linearly independent) --> only exists one solution. No matter what the equation equals zero
x = {1,2} A = {1,2,{1,2}} Is x ⊆ A OR x ∈ A? x ⊆ A = x is a subset of A x ∈ A = x is an element of A
Both. x ⊆ A and x ∈ A
CLS vs Self
CLS refers to the class itself. (not instances) Self refers to an instance in the class e.g. student in school and the class attributes.
What is the difference between Interpreted and Compiled Programming languages?
Compiled language: the target machine directly translates the program. A different program aka the interpreter reads and executives the code. Compiled tend to be faster as the process of translating code in interpreter adds run time. interpreted language: (step by step executer) the source code is not directly translated by the target machine. It is often easier to debug programs written in languages that are designed to be interpreted, as interpreters can produce error messages that are easy to correlate with the source code. Example: Python
How can we predict the dimensionality of the span of some vectors?
Compute the rank of set of vectors Note some ranks can be observed without computation (row reducing etc) A= [1, 1, 0, 2] [-1,-1,0,-2] The rank = 1 as any pair of columns are linearly dependent. Yet, as there are nonzero columns, the rank is positive.
What is the difference between recursive and iterative algorithms?
Diff: recursive is cleaner code (simpler), but requires much more computational power. An Iterative algorithm will use looping statements such as for loop, while loop or do-while loop to repeat the same steps while a Recursive algorithm, a module (function) calls itself again and again till the base condition(stopping condition) is satisfied.
What is the difference between span and plane span?
Difference: Is very similar but can differ in dimensionality. A span can also include a single non-zero vector, which spans a line. Whereas a plane span has a dimension of two. If two vectors a,b are linear independent (both vectors non-zero and there is no real number t with a=bt), they span a plane.
In set theory: In a relation (A R B) we have A = {1,2} and B = {1,2,3} what is the domain and the co-domain of the relation?
Domain = A = {1,2} Co-domain = B {1,2,3}
Does and don'ts for writing a program?
Dont: - Write, test and debug entire program - Forget where bug or what change you made Do (GIT is a good enabler) - Write a function, test, and debug the same function - Integration testing - Backup code - Write potential bug in comment - Compare new version with old version
What defines a transitive relation?
E.g a co-worker. If a is related to b and b is related to c then a must be related to c. Thus; if a R b and b R c then a R c. In other words if x = y and y = z, then x = z
Define an onto function in set theory
Each element in Y (co-domain) is an image of at least one element in X (domain). Thus ALL elements of Y are bound to an x value.
What is a linear combination? And how to determine if two vectors are linear combinations?
Every time we scale two vectors (e.g. multiply, add them together) --> to dermine if it is a linear combination, we have to check if the system is consistent. Steps 1. Augment matrix A with vector B 2. Row reduce augmented matrix to echelon or triangular form 3. Check consistency
What is a Power set?
For the set {a,b,c}: The empty set {} is a subset of {a,b,c} And these are subsets: {a}, {b} and {c} And these are also subsets: {a,b}, {a,c} and {b,c} And {a,b,c} is a subset of {a,b,c} And altogether we get the Power Set of {a,b,c}: P(S) = { {}, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c} } Think of it as all the different ways we can select the items (the order of the items does not matter), including selecting none, or all.
There are two types of rational numbers: Integers and fractions - what is the difference?
Fractions: What is between integers. A part of a whole number, e.g. 1 1/2 or 0.5 Integers: Countable numbers. Two types: negative integers and whole numbers (0 and positive integers).
What does Function Equality mean in set theory? How does it differ from an equivalence relation?
Function equality: Two functions are equal if they have the same domain and codomain and their values are the same for all elements of the domain. If F and G are functions, then they are equal if all pairs are similar, thus if f(x) = g(x) for all x ∈ in X Equivalence relation: When x R y and x = y, then R is both reflexive, symmetric and transitive. Note these 3 terms are properties of equivalence relation. If X is the set of all cars, and ~ is the equivalence relation "has the same color as", then one particular equivalence class consists of all green cars. X/~ could be naturally identified with the set of all car colors.
What are functions in programming, how do you use them, and what are the characteristics?
Functions are used to write reusable pieces/chunks of code that performs a specific tasks. Functions are not run in a program until they are "called" or "invoked" in the program. Function characteristics: 1) has a name 2) has parameters (0 or more) 3) has a body 4) returns something
What is GF(2)?
Galois field and has only two elements: 0 and 1
How to find the image in a linear transformation and what is it?
How: Dotproduct of matrix and vector. What: The image of a linear transformation of the matrix is the span of the vectors of the given linear transformation. (Think of it as what vectors you can get from applying the linear transformation or multiplying the matrix by a vector.) --> it is thus a part of the vector space
What are the side effects of a list?
If you create a list names=["Niklas","Marina","Max"] and now create a new variable new_names=names and then append a new name --> new_names.append("Lars"). If you print this, both the "names" and "new_names" variables will have all FOUR names in the list - this is because a list is mutable.
Are the following vectors linearly independent? x=[1,0,0], y=[0,1,0] and z=[4,5,0]
No. Observe that z=4x+5y hence they are linearly dependent
What is a partition in set theory?
Sets are divided into disjoint pieces. Such division is called a partition. A collection of disjoint subsets of a given set. The union of the subsets must equal the entire original set. For example, one possible partition of {1, 2, 3, 4, 5, 6} is {1, 3}, {2}, {4, 5, 6}.
What does 2 ∈ {1,2,3} mean?
That 2 is an element of the set (belongs to)
What does A ⊂ B mean?
That A is a proper subset of B. Thus, all elements of A are in B, but at least one element in B is not in A.
What does A ⊆ B mean?
That A is a subset of B. Thus, all elements of A are in B (cannot outrule that it is a proper subset)
What does it mean that set A is a proper subset of set B?
That all elements of A are in B, but at least one element in B is not in A. Denoted: A ⊂ B
What defines a reflexive relation?
That each element of the relation is related to itself. Thus; for all x, x R x. In other words x = x
What defines a symmetric relation?
That if any element is related to another element, when the second element is related to the first. So if an arrow goes from a to b, then there must go an arrow back from b to a. Thus; if a R b then b R a. In order words, if x = y, then y = x
What does it mean that two sets are disjoint?
That they have no elements in common.
What is the dot operator used for in Python?
The . (dot) operator is used to access class, structure, or union members e.g variablename.sum or self.name
What is the cartesian product, A x B?
The Cartesian Product of sets A and B is defined as the set of all ordered pairs (x, y) such that x belongs to A and y belongs to B. For example, if A = {1, 2} and B = {3, 4, 5}, then the Cartesian Product of A and B is {(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5)}. Other example: A = {1,2} B = {a,b,c} A x B = { (1,a) , (1,b) , (1,c) , (2,a) , (2,b) , (2,c) } A x B = 6 elements (ordered pairs)
What is the basis of vector space?
The basis of a vector space is a set of linearly independent vectors that span the full space. In linear algebra, a basis for a vector space V is a set of vectors in V such that every vector in V can be written uniquely as a finite linear combination of vectors in the basis. One may think of the vectors in a basis as building blocks from which all other vectors in the space can be assembled.
What is set cardinality?
The cardinality of a set is a measure of a set's size, meaning the number of elements in the set. For instance, the set A = { 1 , 2 , 4 } has a cardinality of 3 for the three elements that are in it.
What is the difference between the cartesian product between two sets, and a relation between the two sets?
The cartesian product is all the ordered pairs between two sets, where a relation for instance is a subset of the cartesian product, which only contains the pairs that satisfy the relationship condition. Let A and B be two sets. The 'Cartesian product' of these sets is denoted by A×B and consists of all ordered pairs (a, b) with a∈A and b∈B. Any subset R⊆A×B is called a 'binary relation' between A and B. The case A=B is of particular interest; in this situation R is called a 'relation' on A.
What does Aᶜ mean?
The complement of set A is defined as a set that contains the elements present in the universal set but not in set A. For example, Set U = {2,4,6,8,10,12} and set A = {4,6,8}, then the complement of set A, so Aᶜ = {2,10,12}.
In set theory: what is a domain and co-domain in a relation?
The domain of the relation R is the set of arguments i.e. first members of each ordered pair and the codomain is the set of which the values i.e. second members of all the ordered pairs is a subset of. fx the relation A={(1,2),(3,4)} will be= Domain = (1,3) and co-domain = (2,4)
What is the inverse of a relation?
The inverse of a relation is a set of ordered pairs which are obtained by interchanging the first and second elements of the ordered pairs Example: R = { (1,2), (2,2), (2,3)} R^-1 = { (2,1), (2,2), (3,2)}
What is the span?
The set of all the linear combinations (all combinations through multiplying and adding vectors). Similar to asking wherever a vector equation has a solution - eg. in 2-dimension vector it includes all pairs of in the 2-d space. - when they line up, their span is all vectors whose tip sits on a certain lien - if 0 it will be stuck at origin
What are two linear systems called if they have the same solution set?
Then they are said to be equivalent
If A is a 3x4 matrix and B is a 4x2 matrix, what is the size of B*A?
There is no answer, as the number of columns in matrix B does not equal the number of rows in matrix A.
What is this: { (a,b), (c,d), (d,e) }?
This is a set of ordered pairs, in which the order matters. As opposed to regular sets {1,2,3,4} where the order doesn't matter. Given this, the domain will be the first element of each pair e.g. {a,c,d} and the range of the set will be the second element: {b,d,e}. Another example; If 'a' and 'b' are two elements, then the two different pairs are (a, b), (b, a). In an ordered pair (a, b), a is called the first element, and b is called the second element.
Tuple vs List vs Dictionary in Python?
Tuple: is a collection of Python objects separated by commas and written in ( ). In some way, a tuple is similar to a list in terms of indexing, nested objects, and repetition but a tuple is immutable, unlike lists that are mutable. Therefore tuples are good to store results. Dictionary: one data structure opposed to lists. An unordered and mutable Python container that stores mappings of unique keys to values. Dictionaries are written with curly brackets ({}). List: a container that stores a collection of elements that are arranged in a linear or sequential order, can be accessed by index, whereas dictionary is matched by key. Mutable and can contain mixed types (not common)
For vs while loops
While loop: When you don't know how many iterations is needed For loop: When you have a pre-defined number of iterations, such as the length of a list etc. Both loops can end via break.
Are { 1 , 2 , 0.5 , b } and { 2 , 1 , 1 , b , b , 0.5} equal sets?
Yes, they are equal because they contain the same elements. The number of times a given element occurs doesn't matter.
We have the sets: A = {1,2,3} B = {2,3,4} Lets say we have the relation x R y where (x,y) ∈ R if x+y = 4 Which pairs are a part of R?
R = { (1,3), (2,2)} because x + y = 4
What does 'Union' mean in set theory?
A ∪ B = All the elements that are in A, B or both.
x = {1} A = {{1},{2},{3}} Is x ⊆ A OR x ∈ A? x ⊆ A = x is a subset of A x ∈ A = x is an element of A
x ∈ A, x is an element of A
Does Z^+ contain 0?
No, it only contains numbers > 0
What is a statement and an expression in Python?
Statements represent an action or command e.g print statements, assignment statements. Expression is a combination of variables, operations, and values that yields a result value.
What is control flow in Python? And what three types of control structures?
- A program's control flow is the order in which the program's code executes. - The control flow of a Python program is regulated by conditional statements, loops, and function calls. Python has three types of control structures: 1. Sequential - default mode eg. line-by-line execution 2. Selection - used for decisions and branching eg.. if statements 3. Repetition - used for looping, i.e., repeating a piece of code multiple times.
what are the tools for debugging?
- Built in to Anaconda, Google Collab - Python Tutor - Print statement (good to test hypothesis) - Use brain, and be systematic in hunt
What are binding variables and values?
An equal sign is an assignment of a value to a variable name e.g. name="Max". Here, name is the variable and "Max" is the value. You can also overwrite a variable such that name="Max"+"Marina" will overwrite the previous name variable, this is called "changing bindings".
What does inverse function mean?
An inverse function is a function that undoes the action of the another function. A function g is the inverse of a function f if whenever y=f(x) then x=g(y). In other words, applying f and then g is the same thing as doing nothing. We can write this in terms of the composition of f and g as g(f(x))=x. An example: Here we have the function f(x) = 2x+3. The Inverse Function goes the other way: So the inverse is f-1(y) = (y-3)/2 Steps for finding the inverse of a function f. 1. Replace f(x) by y in the equation describing the function. 2. Interchange x and y. In other words, replace every x by a y and vice versa. 3. Solve for y. 4. Replace y by f-1(x).
Define a one-to-one function in set theory
Any distinct element from A maps to a distinct element in B. Elements from A do not share any elements from B.
A = {1,2,3} B = {3,4,5} What is A∩B?
A∩B = 3 The intersection between A and B = All common elements
A = {1,2,3} B = {3,4,5} What is B - A in set theory?
B - A = 4, 5 The difference. All the elements in B that are not in A.
How many solutions exists in a linear dependent system?
Infinitely many
What is decomposition in programming?
It involves breaking down a complex problem or system into smaller parts that are more manageable and easier to understand. The smaller parts can then be examined and solved, or designed individually, as they are simpler to work with. In programming, divide problems into modules.
What defines a system of linear equations?
It is a collection of one or more linear equations with the same variables (x1,x2,x3).
What are linear dependence and independence? How to check?
Linearly dependent Vector does not add to the span but goes on the same line as others methods to check: a) if we have a nontrivial solution (determinants = 0 or there exists any non-zero solutions b) If there are free variables (then we know system has a nontrivial solution) c) if at least one of the vectors is a multiple of the other Linear dependence by inspection / theorems: d) Theorem:. If a set contains more vectors than entries (e.g. three vectors, but only two entries in each vector) e) Theorem: if a vector within the set only contains 0's Linearly independent If each vector adds to the span (set of linear combinations) -Columns of a Matrix A are linearly independent if and only if the equation Ax = 0 has only the trivial solution (all solutions x = 0) - No free variables
What is list comprehension?
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. It is a very concise way to create a new list by performing an operation on each item in the existing list. List comprehension is considerably faster than processing a list using the for loop. Note: Every list comprehension can be rewritten as a for loop but not every for loop can be rewritten as a list comprehension.
List comprehension vs for loop
List comprehension: Less computational power (less code) to create lists. For loop: More code, yet more easy to understand for the user. For loop can be used for multiple other things, whereas list comprehension is limited to lists and loops.
What is a local variable vs global variable?
Local Variable is defined as a type of variable declared within programming block or subroutines. It can only be used inside the subroutine or code block in which it is declared. The local variable exists until the block of the function is under execution. After that, it will be destroyed automatically. A Global Variable in the program is a variable defined outside the subroutine or function. It has a global scope means it holds its value throughout the lifetime of the program. Hence, it can be accessed throughout the program by any function defined within the program, unless it is shadowed.
How many vectors does the Span {} contain?
Only one vector, the zero vector
What is an operator in Python?
Operators are special symbols in Python that carry out arithmetic or logical computation. Fx 5+5, here the operator is "+". A sequence of operands and operators, like a + b - 5 , is called an expression.
What is a data object in Python (including examples)?
Programs manipulate data types. Objects have a type that defines the kind of things that programs can do to them. All data in a Python program is represented by objects or by relations between objects. Examples are Integers, Float, Boolean, List, String etc. (scalar objects)
What is R, Z, Q and N?
R = set of all real numbers (ALL numbers, both integers, fractions etc.) Z = set of all integers (all countable numbers, both negative and positive) Q = set of all rational numbers (both fractions and integers) N = Natural numbers are every integer equal to or above 0.
What does it mean that set A is a subset of set B?
that all elements of A are in B. Denoted: A ⊆ B