In [1]:
import pandas as pd
import os

from scipy import misc
from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import matplotlib.pyplot as plt

# Look pretty...
# matplotlib.style.use('ggplot')
plt.style.use('ggplot')

In [2]:
# TODO: Start by creating a regular old, plain, "vanilla"
# python list. You can call it 'samples'.
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.
#

for file in os.listdir('Datasets/ALOI/32'):
    a = os.path.join('Datasets/ALOI/32', file)
    img = misc.imread(a)#.reshape(-1)
    samples.append(img)
print(type(samples))
print(len(samples))

for file1 in os.listdir('Datasets/ALOI/32i'):	# Also append the 32i images to the list/dataframe
    b = os.path.join('Datasets/ALOI/32i', file1)
    img1 = misc.imread(b).reshape(-1)
    samples.append(img1)

print(type(samples))
print(len(samples))

colors = []
for i in range(72):
	colors.append('b')
for j in range(12):
	colors.append('r')


<class 'list'>
72
<class 'list'>
84

In [7]:
df = pd.DataFrame(samples) # Convert list of numpy arrays to Pandas DataFrame
df


Out[7]:
0
0 [[4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,...
1 [[4, 4, 4, 3, 4, 4, 3, 3, 4, 4, 3, 3, 3, 4, 4,...
2 [[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,...
3 [[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,...
4 [[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,...
5 [[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,...
6 [[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,...
7 [[4, 4, 4, 4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 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ...
73 [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ...
74 [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ...
75 [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ...
76 [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ...
77 [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ...
78 [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ...
79 [4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 4, 5, 4, 4, 5, ...
80 [4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 5, 5, ...
81 [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ...
82 [5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ...
83 [4, 4, 4, 3, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 3, ...

84 rows × 1 columns


In [5]:
# Run Isomap on the DataFrame:
from sklearn import manifold
iso = manifold.Isomap(n_neighbors = 6, n_components = 3)
iso.fit(df)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-41515920dac5> in <module>()
      2 from sklearn import manifold
      3 iso = manifold.Isomap(n_neighbors = 6, n_components = 3)
----> 4 iso.fit(df)

D:\anaconda\lib\site-packages\sklearn\manifold\isomap.py in fit(self, X, y)
    162         self : returns an instance of self.
    163         """
--> 164         self._fit_transform(X)
    165         return self
    166 

D:\anaconda\lib\site-packages\sklearn\manifold\isomap.py in _fit_transform(self, X)
    101 
    102     def _fit_transform(self, X):
--> 103         X = check_array(X)
    104         self.nbrs_ = NearestNeighbors(n_neighbors=self.n_neighbors,
    105                                       algorithm=self.neighbors_algorithm,

D:\anaconda\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    400         # make sure we actually converted to numeric:
    401         if dtype_numeric and array.dtype.kind == "O":
--> 402             array = array.astype(np.float64)
    403         if not allow_nd and array.ndim >= 3:
    404             raise ValueError("Found array with dim %d. %s expected <= 2."

ValueError: setting an array element with a sequence.

In [26]:
def Plot2D(T, title, x, y):
  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
  # It also plots the full scatter:
  ax.scatter(T[:,x],T[:,y], marker='.', c = colors, alpha=0.7)

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
  # It also plots the full scatter:
  ax.scatter(T[:,x],T[:,y],T[:,z], marker='.', c = colors, alpha=0.65)

In [27]:
Plot2D(Z, "Isomap transformed data, 2D", 0, 1)
Plot3D(Z, "Isomap transformed data 3D", 0, 1, 2)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-27-79e5b18494bd> in <module>()
----> 1 Plot2D(Z, "Isomap transformed data, 2D", 0, 1)
      2 Plot3D(Z, "Isomap transformed data 3D", 0, 1, 2)

NameError: name 'Z' is not defined

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: