-
Notifications
You must be signed in to change notification settings - Fork 0
/
isomap.py
140 lines (114 loc) · 4.55 KB
/
isomap.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import pandas as pd
from scipy import misc
from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import matplotlib.pyplot as plt
# Look pretty...
matplotlib.style.use('ggplot')
#
# TODO: Start by creating a regular old, plain, "vanilla"
# python list. You can call it 'samples'.
#
# .. your code here ..
samples= []
#
# TODO: Write a for-loop that iterates over the images in the
# Module4/Datasets/ALOI/32/ folder, appending each of them to
# your list. Each .PNG image should first be loaded into a
# temporary NDArray, just as shown in the Feature
# Representation reading.
#
# Optional: Resample the image down by a factor of two if you
# have a slower computer. You can also convert the image from
# 0-255 to 0.0-1.0 if you'd like, but that will have no
# effect on the algorithm's results.
#
# .. your code here ..
import os
for ALOI32 in os.listdir('E:\DAT210x-master\DAT210x-master\Module4\Datasets\ALOI\32'):
a = os.path.join('E:\DAT210x-master\DAT210x-master\Module4\Datasets\ALOI\32', ALOI32)
img1 = misc.imread(a).reshape(-1)
samples.append(img1)
print (len(samples))
#
# TODO: Once you're done answering the first three questions,
# right before you converted your list to a dataframe, add in
# additional code which also appends to your list the images
# in the Module4/Datasets/ALOI/32_i directory. Re-run your
# assignment and answer the final question below.
#
# .. your code here ..
for ALOI32i in os.listdir('E:\DAT210x-master\DAT210x-master\Module4\Datasets\ALOI\32i'):
b = os.path.join('Datasets/ALOI/32i', ALOI32i)
img2 = misc.imread(b).reshape(-1)
samples.append(img2)
print (len(samples))
#create a colur python list where.
#Store a 'b' in it for each element you load from the /32/ directory,
#and an 'r' for each element you load from the '32_i' directory.
#Then pass this variable to your 2D and 3D scatter plots, as an optional parameter c=colors
# .. your code here ..
colours=[]
for i in os.listdir('E:\DAT210x-master\DAT210x-master\Module4\Datasets\ALOI\32'):
colours.append('b')
for j in os.listdir('E:\DAT210x-master\DAT210x-master\Module4\Datasets\ALOI\32i'):
colours.append('r')
#
# TODO: Convert the list to a dataframe
#
# .. your code here ..
df = pd.DataFrame(samples)
#
# TODO: Implement Isomap here. Reduce the dataframe df down
# to three components, using K=6 for your neighborhood size
#
# .. your code here ..
from sklearn import manifold
iso = manifold.Isomap(n_neighbors=1, n_components=3)
T=iso.fit_transform(df)
manifold.Isomap(eigen_solver='auto', max_iter=None, n_components=3, n_neighbors=1,
neighbors_algorithm='auto', path_method='auto', tol=0)
#def Plot3D(T, title, x, y, num_to_plot=40):
def Plot2D(T, title, x, y, num_to_plot=40):
#
# TODO: Create a 2D Scatter plot to graph your manifold. You
# can use either 'o' or '.' as your marker. Graph the first two
# isomap components
#
# .. your code here ..
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title(title)
ax.set_xlabel('Component: {0}'.format(x))
ax.set_ylabel('Component: {0}'.format(y))
x_size = (max(T[:,x]) - min(T[:,x])) * 0.08
y_size = (max(T[:,y]) - min(T[:,y])) * 0.08
ax.scatter(T[:,x],T[:,y], marker='.',alpha=0.7,c=colours)
Plot2D(T, 'Isomap 2D', 0, 1, num_to_plot=40)
#
# TODO: Create a 3D Scatter plot to graph your manifold. You
# can use either 'o' or '.' as your marker:
#
# .. your code here ..
def Plot3D(T, title, x, y, z):
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.set_title(title)
ax.set_xlabel('Component: {0}'.format(x))
ax.set_ylabel('Component: {0}'.format(y))
ax.set_zlabel('Component: {0}'.format(z))
x_size = (max(T[:,x]) - min(T[:,x])) * 0.08
y_size = (max(T[:,y]) - min(T[:,y])) * 0.08
z_size = (max(T[:,z]) - min(T[:,z])) * 0.08
ax.scatter(T[:,x],T[:,y],T[:,z], marker='.', alpha=0.7,c=colours)
Plot3D(T, "Isomap 3D", 0, 1, 2)
plt.show()
'''
The basic for Isomap algorithm steps are:
1. For each object, find a small set of neighboring objects and their distances.
2. Compute all-pairs shortest paths on the above neighborhood graph.
3. Run multidimensional scaling (i.e., isomap) using the matrix of shortest-path distances.
'''
#Geodetic Data Management Feature measures distance between two points along a geodesic.
#A geodesic is the shortest path between two points on the ellipsoidal shape of the Earth,
#and this shortest path might not follow a line of constant latitude even though the two end points are at the same latitude.