'Python: Function Error TypeError: tuple indices must be integers or slices, not tuple & TypeError: 'int' object is not subscriptable

Have been applying below code on my function, but regularly getting an error. Please can you explain where am i going wrong

clusters = np.zeros((len(dataset),1))

def assign(centroids,dataset,clusters,k):
    numOfObject=len(dataset)
    #for every object in the dataset
    for i in range(numOfObject):
        X=dataset[i,1:-1]
        #find the closest centroid
        centroidOfX= -1
        distanceToClosestcentroids = np.Inf
        for y in range(k):
            
            currentcentroids=centroids[y,:]
            dist=distance(X,currentcentroids)
            if dist<distanceToClosestcentroids:
                #Found closer Centroid
                distanceToClosestcentroids= dist
                centroidOfX=y
        #assign to X its closest centroid
        clusters[i]=int(centroidOfX)


#assign((2.5),dataset,clusters,20)
assign((2,1),dataset,clusters,20)

Dont really know why i am prompted with this error

Traceback (most recent call last):
  File "c:\library\K-Mean.py", line 71, in <module>
    assign((2.5),dataset,clusters,20)
  File "c:\library\K-Mean.py", line 62, in assign
    currentcentroids=centroids[y,:]
TypeError: 'float' object is not subscriptable
PS C:\library> & "C:/Users/ASHISH SHARMA/AppData/Local/Microsoft/WindowsApps/python3.9.exe" c:/library/K-Mean.py
Traceback (most recent call last):
  File "c:\library\K-Mean.py", line 71, in <module>
    assign((2,1),dataset,clusters,20)
  File "c:\library\K-Mean.py", line 62, in assign
    currentcentroids=centroids[y,:]
TypeError: tuple indices must be integers or slices, not tuple


Solution 1:[1]

The code worked perfectly, the values entered was not in right order

def assign(centroids, dataset, clusters,k): """The Assign Function helps overall assigning the value to the functions used in the K-Means and K-Medians

Args:
    centroids (NDArray(Float64)): This helps us store the value for the centroid of the cluster
    dataset (NDArray): Dataset which we are using in the program to calculate K-Means and K-Medians
    clusters (NDArray(Float64)): The different clusters formed by the program to classify them into different categories
    k (int): The number of times we have to iterate the function
"""
numOfObjects = len(dataset)
#for every object in the dataset
for i in range(numOfObjects):
    X = dataset[i, 1:-1]
    #find the closest centroid
    centroidsOfX = -1
    distanceToClosestcentroids = np.Inf
    for y in range(k):
        currentcentroids = centroids[y,:]
        dist = distance(X, currentcentroids)
        if dist < distanceToClosestcentroids:
            #Finally found closer Centroid
            distanceToClosestcentroids = dist
            centroidsOfX = y
    #Assign to X its closest centroid
    clusters[i] = int(centroidsOfX)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ash