Submission says idx is not defined

submission says idx is not defined, although I defined it as … idx = np.zeros(X_img.shape[0], dtype=int)

Hi @Sandeep_Sharma1

Please make sure you use a parameters that sent to you in function to make your code generalize over all cases so instead of use np.zeros(X_img.shape[0],…) you use np.zeros(X.shape[0],…)


but, name of my example data set is ‘X_img’ not X

Does the solution I said pass the test?

the test cases run on different training names to make sure your code is correct so we make our code in function and pass a name of training set as a parameter to make code generalize

first of tell me, how I have to do assignment, means I should write (final) whole code in parts or I should write whole code in just one cell, I did it in one cell and it is running with no error, I am pasting it below, it is not even running

{code removed by mentor}

also, the solution you told, is not passing

Hello @Sandeep_Sharma1,

I suggest two things:

  1. Make sure your assignment notebook is in its original format (please don’t put the codes together). The autograder relies on the right format to work properly. If you need, you may follow these instructions to refresh your notebook, and then you can copy your work back.

  2. After you make sure the format is correct, run the code again, and share a screenshot of the Error Traceback, if any.

not defined bug isn’t difficult to solve, and it only requires some careful inspection, but we need to see the Error Traceback first.


PS: Sharing assignment work is in violation of the community’s guideline, so I am removing it for you.

ok, I have understood your point, I have written my own code, I also refreshed the notebook, but my code is not running, it runs only one time, but when I try to run it again it is not showing anything not any output nor any error, yes it is not full code, but up to here it is not running. Please Help
here is the code:-

import numpy as np
import matplotlib.pyplot as plt
image1 = plt.imread(‘bird_small.png’)
image1 = image1/255
image_data = np.reshape(image1, (image1.shape[0] * image1.shape[1], 3))
k = 18
itrations = 12
ini_centr = image_data[(np.random.permutation(image_data.shape[0]))[:k]]
p = 0
index =
for i in range(image_data.shape[0]):
dist_i =
for j in range(ini_centr.shape[0]):
t = p
dist = np.linalg.norm(image_data[i] - ini_centr[j])
if i == t:
p = p +1
idxr = np.array(index)
v =
for j in range(ini_centr.shape[0]):
l =
idx_grup_j = np.array(np.where(idxr == j))
for h in range(idx_grup_j.shape[1]):
m = idx_grup_j[0][h]
g = image_data[m]
z = np.array(l)
mean = np.mean(z, axis = 0)
d = np.array(v)

Hello @Sandeep_Sharma1,

If a mentor needs to read your code, they will ask. I am not going to ask for it now. We can offer you some suggestions, but it is your assignment to finish and if there is any bug, you will need to fix it yourself.

From what I have seen, here are my suggestions as long as passing the assignment is your concern:

  1. did you combine codes from multiple cells into one? If you did, please reverse it, or refresh your notebook to its original state. Combining codes can cause trouble for the autograder which can ultimately fail your submission. Also, distributing codes into cells is a good way for us to handle less code at a time, especially when we need to debug. You are debuging code, and from your message, you don’t know where the problem is. Combining code isn’t the best idea to pinpoint a problem.

  2. You say it runs but without any output nor error. It is expected to output something because there is a print line at the end. What I am going to suggest is:

    • make sure the code are back to their original code cells as suggested in (1)
    • run those code cells to see which one starts not responding
    • Once you identify the problematic code cell, put someprint in between your lines to see which very line is really the one that stops responding
    • If there is a loop in that problematic code cell, verify that it is not an infinite loop

@Sandeep_Sharma1, please follow my suggestions to break your code into smaller chunks, which will be easier for you to locate the problem. Mentors can give you suggestion, but not fixing the code for you. I hope you can write working code, and you can debug unworking code yourself.


1 Like