-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1. Linear_Analysis.py
More file actions
112 lines (94 loc) · 4.59 KB
/
1. Linear_Analysis.py
File metadata and controls
112 lines (94 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
@author: Carson Hanel
Note: These code snippets are derived from Data Science from Scratch First Principles by Joel Grus.
For right now, I'll be transferring the code from the book, explaining the functions, and creating
an API that can be utilized in further analysis. While some of these functions may be part of
the Python standard library or a package already created, I thought it would be useful to begin
creating my own data science toolbelt for the future, with self written commentary.
"""
import math
class Linear_Analysis():
"""
Vector addition:
Vector addition is simply pointwise addition of two lines, or vectors.
The function creates a tuple (v,w) of all pointwise pairs between the
two vectors, and iteratively adds them together in a pointwise fashion,
finally returning a list of generated points.
Noteworthy: You're only supposed to be able to add two vectors of equal length,
however, zip adapts to the dimensions of the shortest vector given
as per the library definitions. Perhaps this could be improved by
introducing error handling.
"""
def vector_add (v, w):
return [v_i + w_i for v_i, w_i in zip(v,w)]
"""
Vector subtraction:
Please refer to the notes written on vector addition.
"""
def vector_sub (v, w):
return [v_i - w_i for v_i, w_i in zip(v,w)]
"""
Vector summation:
Takes a list of vectors as an argument.
The 0'th vector is set as the accumulation point, rather, in order to effectively
create a sum across all vectors, all vectors must be added to some vector; namely
the 0'th. From then, the function iterates from the 1'st vector onward, adding all
subsequent vectors to 0'th, and finally returning the accumulated sum.
Some useful application from the book:
def vector_sum(vectors):
return reduce(vector_add, vectors)
Which is a higher order function that reduces the vector space to a singular vector
by utilizing the given function, "vector_add". It's not necessary, but is a good usage
of space if that's the type of best practice you're after.
"""
def vector_sum (vectors):
result = vectors[0]
for vector in vectors[1:]:
result = vector_add(result, vector)
return result
"""
Scalar multiplication:
Used for multiplying a vector by a scalar. Essentially vector_add with multiplication,
and rather than a second vector being zipped and summed, all points are simply multiplied
by a constant.
"""
def scalar_multiply(c, v):
return [c * v_i for v_i in v]
"""
Dot product:
Used for multiplying the pointwise information of two vectors into pointwise products.
Simply works like vector_sum, but rather than the sum, you're deriving the product of
the two vectors; a simple algorithm.
"""
def dot(v, w):
return sum(v_i * w_i for v_i, w_i in zip(v,w))
"""
Sum of squares:
The multiplication of a vector by itself. Utilized for determining the dispersion of a
vector which is representative of a set of data. Also utilized in determining the line
of best fit for a dataset.
"""
def sum_of_squares(v):
return dot(v, v)
"""
Magnitude:
The magnitude is to vector what length is to a line, therefore, we're essentially
calculating the length of the vector by calling the magnitude function.
"""
def magnitude(v):
return math.sqrt(sum_of_squares(v))
"""
Squared Distance:
An intermediary step in finding the distance between two vectors, wherein the vector,
w, is pointwise subtracted from v and subsequently squared. To complete the calculation,
you simply have to take the square root of all pointwise information.
"""
def squared_distance(v, w):
return sum_of_squares(vector_subtract(v, w))
"""
Distance:
Finally, the calculation of the distance of two vectors by taking the square root of the
squared distance.
"""
def distance(v, w):
return math.sqrt(squared_distance(v, w))