<jupyter_start><jupyter_text>Defining Vector and its operations<jupyter_code>class Vector(object):
    def __init__(self, x, y):
        super(Vector, self).__init__()
        self.x = float(x)
        self.y = float(y)
        
    def magnitude(self):
        return math.sqrt(self.x ** 2 + self.y **2)
    
    def direction(self):
        """
        calculate direction of this vector and returns it as unit vector
        """
        magnitude = self.magnitude()
        return Vector(self.x / magnitude, self.y / magnitude)
    
    def __add__(self, v):
        return Vector(self.x + v.x, self.y + v.y)
    
    def __sub__(self, v):
        return Vector(self.x - v.x, self.y - v.y)
    
    def __mul__(self, v):
        print "mul"
        return (self.x * v.x) + (self.y * v.y)
    
    def __rmul__(self, v):
        return Vector(v * self.x, v * self.y)
    
    def __str__(self):
        return "vector({}, {})".format(self.x, self.y)<jupyter_output><empty_output><jupyter_text>Plotting vector<jupyter_code>def drawVector(v, s="b-"):
    if v.x < 0:
        x_axis = np.arange(v.x, 1)
    else:    
        x_axis = np.arange(0, v.x+1)
    
    #print x_axis
    plt.plot(x_axis, x_axis * float(v.y)/float(v.x), s)
    plt.xticks(range(-12, 12))
    plt.yticks(range(-6, 12))
    plt.rcParams["figure.figsize"] = [12, 9]<jupyter_output><empty_output><jupyter_text>Unit vector<jupyter_code>drawVector(Vector(3, 9), "b-")
drawVector(Vector(3, 9).direction(), "r-") # unit vector
plt.grid(True)
plt.title("the red one is the unit vector of the blue vector")
print Vector(3, 9).direction()
plt.show()<jupyter_output>vector(0.316227766017, 0.948683298051)<jupyter_text>Addition and subtraction<jupyter_code>v1 = Vector(8, 3)
v2 = Vector(2, 4)
a = v1 + v2
s1 = v1 - v2
s2 = v2 - v1

drawVector(v1, "y-")
drawVector(v2, "g-")
drawVector(a, "b--")
drawVector(s1, "r--")
drawVector(s2, "c--")

plt.grid()
v1 = Vector(3, 4)
v2 = Vector(2, -2)
a = v1 + v2
s1 = v1 - v2
s2 = v2 - v1

drawVector(v1, "y-")
drawVector(v2, "g-")
drawVector(a, "b--")
drawVector(s1, "r--")
drawVector(s2, "c--")

plt.grid()<jupyter_output><empty_output><jupyter_text>Dot product and projection<jupyter_code>v1 = Vector(7, 0)
v2 = Vector(8, 4)

u = v1.direction()
z = (u * v2) * u

drawVector(v1, "b-")
drawVector(v2, "g-")
drawVector(z, "r--")
plt.title("projection of te green vector on the blue vector")
plt.grid()
print v1* v2
v1 = Vector(8, 2)
v2 = Vector(3, 5)

u = v1.direction()
z = (u * v2) * u

drawVector(v1, "b-")
drawVector(v2, "g-")
drawVector(z, "r-")
drawVector(z - v2, "c--")
plt.title("projection of te blue vector on the green vector")
plt.show()
plt.grid()
v1 = Vector(2, 1)
v2 = Vector(-5, 7)

u = v1.direction()
p = (u * v2) * u
print (v1 * v2)
drawVector(v1, "b-")
drawVector(v2, "g-")
drawVector(p, "r--")

plt.grid()
x1 = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
y1 = [12.0, 18.0, 14.0, 8.0, 10.0, 6.0]

x2 = [4, 5, 7, 8]
y2 = [47, 37, 22, 24]
plt.plot(x1, y1, "o", color="yellow")
plt.plot(x2, y2, "x", color="green")

plt.plot([4], [40], "x", color="red")
plt.plot([2], [22], "o", color="red")

#plt.plot(range(9),[ i * - 4.6 + 54 for i in range(9)], ":", color="blue")
plt.plot(range(9),[ i * - 4.6 + 44 for i in range(9)], color="blue")
#plt.plot(range(8),[ i * - 4.6 + 34 for i in range(8)], ":", color="blue")
plt.title("Figure 3: maximum margin")
5, 5 * - 4.6 + 44
w = Vector(2, 2)
v1 = Vector(3, 4)

u = w.direction()
print u
p = (u * v1) * u
#print (v1 * v2)
drawVector(w, "b-")
drawVector(v1, "g-")
drawVector(p, "r--")
plt.plot(range(-7, 7), [-i * 1.  for i in range(-7, 7)], "k")
plt.grid()<jupyter_output>vector(0.707106781187, 0.707106781187)
mul
{'fraction_non_alphanumeric': 0.2050300710770913, 'fraction_numerical': 0.06506287588846364, 'mean_word_length': 3.191294387170676, 'pattern_counts': {'":': 2, '<': 20, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '581427', 'license': 'MIT', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1603, 'n_words': 385, 'path': 'Vectors.ipynb', 'repo_name': 'muatik/machine-learning-examples', 'size': '3658'}