K Nearest Neighbour

I have doubts how value of y is deciding that the point belong of which class.

def knn(x,y,query_x,k=5):
vals=[]
m=x.shape[0]

for i in range(m):
    d=dist(query_x,x[i])
    vals.append((d,y[i]))

vals=sorted(vals)
vals=vals[:k]


vals=np.array(vals)

    
new_vals=np.unique(vals[:,1],return_counts=True)

index=new_vals[1].argmax()
pred=new_vals[0][index]

If k=5, we are taking 5 nearest points for a given query_x.
In these 5 points, there might be points belonging to different different classes(y).

So, we are also considering the class_label (y) of the nearest points.
Thus, count the frequency of class_label(y) in 5 nearest neighbors and assign the label which has the highest frequency.

For e,g
if in a query point, 4 points belong to class_0 and 1 point belongs to class_1. So, we can see that the query point is surrounded by mostly class_0 (max). Therefore we assign class_0 to this query point.
So, it is only because of class labels (y) we have, we can decide which class_label should be assigned to the new point, based on the maximum frequency of each class.