Probably Approximately Correct

MSc Machine Learning @ UCL | Alumnus @ IIT Madras| Google DeepMind Scholar | Interests: Machine learning

Saturday, December 23, 2023

Iterating and Indexing in Nested Lists for Data Selection : #High_Performance_Python(Post_1)



Often in image processing, we encounter image data organized in nested lists. A notable example is the image data titled 'dtrain123.dat', available at the link 

To effectively visualize such data, we use nested loops to traverse and manipulate these complex data structures. Let's start with a 'toy' example to illustrate the basic concept and its output. Following that, I'll provide the actual code needed to display the image data from 'dtrain123.dat'.

Code :

x = np.arange(10, 20)
y = np.array([0, 1, 2, 3, 0, 2, 1, 3, 0, 2])
print(x, y)

index = [np.argwhere(y == i).flatten() for i in range(4)]
print(index)

for i in range(4):
    if len(index[i]) >= 3: 
        img_index = index[i][2] 
        print(f"Category {i}, Index: {img_index}, 
           Value in x: {x[img_index]}")


The code creates an 'index' list that maps each category (0 to 3) to its occurrences in the array `y` using `np. argwhere(y == i).flatten()`. It then iterates over these categories, ensuring at least three instances exist to avoid indexing errors. For each qualifying category, it retrieves the index of the third occurrence from `index[i][2]` and uses this to access the related value in the `x` array.


This same logic is applied to the image data.

img_indices = [np.argwhere(y == i) for i in range(10)] 
plt.figure(figsize=(10, 3))
for i in range(10):
        plt.subplot(2, 5, i + 1)
        img_index = img_indices[i][2, 0]
        print(img_index)
        plt.imshow(x[img_index, :].reshape(16, 16))
        plt.axis('off')

The desired output is :




Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home