forked from Introtocs/Week9_Lec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_student.py
68 lines (53 loc) · 1.73 KB
/
sample_student.py
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
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 29 16:36:11 2015
@author: zhengzhang
"""
import util
import helper
# Sample class
class Sample(object):
def __init__(self, name, features, label = None):
#Assumes features is an array of numbers
self.name = name
self.features = features
self.label = label
def dimensionality(self):
return len(self.features)
def getFeatures(self):
return self.features[:]
def getLabel(self):
return self.label
def getName(self):
return self.name
def distance(self, other):
return util.minkowskiDist(self.features, other.getFeatures(), 2)
def __add__(self, other):
f = []
for i in range(self.dimensionality()):
f.append(self.getFeatures()[i] + other.getFeatures()[i])
return Sample(self.name + '+' + other.name, f)
def __truediv__(self, n):
f = []
for e in self.getFeatures():
f.append(e/float(n))
return Sample(self.name + '/' + str(n), f)
#### Implement an overwrite of the '-' operator here!
def __sub__(self, other):
''' replace the line below with you code
refer to the __add__ for ideas '''
return helper.__sub__(self, other)
def __mul__(self, other):
''' bonus: can you do vector multiplication?
this is two vectors element-wise multiplication '''
pass
def __str__(self):
return self.name +':'+ str(self.features) + ':' + str(self.label)
if __name__ == "__main__":
a = Sample('a', [1, 1])
b = Sample('b', [-1, -1])
print(a)
print(b)
print(a + b)
print(a - b)
print(a/2)