-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample-projection.py
70 lines (56 loc) · 1.71 KB
/
sample-projection.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
68
69
70
"""
Projection demo.
Orthogonal projection.
"""
from __future__ import print_function
import projection
try:
import numpy as np
except ImportError as msg:
error = ", please install the following packages:\n"
error += " NumPy (http://www.numpy.org)\n"
raise ImportError(str(msg) + error)
class Ortho(projection.Projection):
"""
Orthodonal projection.
"""
def __init__(self, data, data_class, plane=[1, 2]):
"""
Class initialization.
"""
dim = data.shape[1]
assert plane[0] != plane[1] and 0 < plane[0] <= dim and \
0 < plane[1] <= dim, \
"*** ERROR (Ortho): plane projection is not well defined"
projection.Projection.__init__(self, data, data_class, 2)
self.plane = plane
def project(self):
"""
Project method.
Computes the projection itself.
"""
assert type(self.data) is np.ndarray, \
"*** ERROR (Ortho): project input must be of numpy.array type."
ninst = self.data_ninstances
proj = np.zeros((ninst, 2))
proj[:, 0] = self.data[:, self.plane[0] - 1]
proj[:, 1] = self.data[:, self.plane[1] - 1]
self.projection = proj
def run():
import time
import sys
print("Loading data set... ", end="")
sys.stdout.flush()
data = np.loadtxt("sample-data.data", delimiter=",")
n, dim = data.shape
data_class = np.ones(n)
print("Done.")
start_time = time.time()
print("Projecting... ", end="")
sys.stdout.flush()
ortho = Ortho(data, data_class, [1, 3])
ortho.project()
print("Done. (" + str(time.time() - start_time) + "s)")
ortho.plot()
if __name__ == "__main__":
run()