Home > Software engineering >  How do i fix this python class name error
How do i fix this python class name error

Time:04-21

In creating a class for KNN I have these 2 different functions "predict_new_item" and "get_ids_of_k_closest" Ive created a variable called "closestK" and set this equal to get_ids_of_k_closest however python is throwing me a name error saying

"'get_ids_of_k_closest' is not defined"

def predict_new_item(self, newItem):
    """make prediction for single item. Step 1 is same as 1-NN steps 2 and 3 need writing"""

    distFromNewItem = np.zeros((self.numTrainingItems))

    for stored_example in range(self.numTrainingItems-1):
        distFromNewItem[stored_example] = self.distance(newItem, self.modelX[stored_example])
        closestK = get_ids_of_k_closest(k, distFromNewItem)
        labelCounts = np.zeros(len(self.labelsPresent))

        for k in range(k-1):
            thisindex = closestK[k]
            self.thislabel = self.y_train[thisindex]
            labelCounts[thisindex] 1
            thisPrediction = np.amax(labelCounts)
        return (thisPrediction)



def get_ids_of_k_closest(distFromNewItem, k):
    closestK = np.empty(k, dtype=int)

    arraySize = len(distFromNewItem.shape[0])

    for k in range(k-1):
        thisClosest = 0
        for exemplar in range(arraySize-1):
            if (distFromNewItem[exemplar] < distFromNewItem[thisClosest]):
                thisClosest = exemplar
                closestK[k] = thisClosest
        distFromNewItem[thisClosest] = 100000
    return (closestK)

Added whole class code below for those asking in the comments:

class simple_KNN:
def __init__(self, k, verbose=True):
    self.k = k
    self.distance= W7utils.euclidean_distance

    self.verbose = verbose

def fit(self, X, y):
    self.numTrainingItems = X.shape[0]
    self.numFeatures = X.shape[0]
    self.modelX = X
    self.modelY = y
    self.labelsPresent = np.unique(self.modelY)

    if (self.verbose):
        print(
            f"There are {self.numTrainingItems} training examples, each described by values for {self.numFeatures} features")
        print(
            f"So self.modelX is a 2D array of shape {self.modelX.shape}")
        print(
            f"self.modelY is a list with {len(self.modelY)} entries, each being one of these labels {self.labelsPresent}")


def predict(self, newItems):

    numToPredict = newItems.shape[0]
    predictions = np.empty(numToPredict)

    for item in range(numToPredict-1):
        thisPrediction = self.predict_new_item(newItems[item])
        predictions[item] = thisPrediction
    return predictions

def predict_new_item(self, newItem):    
    distFromNewItem = np.zeros((self.numTrainingItems))

    for stored_example in range(self.numTrainingItems-1):
        distFromNewItem[stored_example] = self.distance(newItem,  self.modelX[stored_example])
        closestK = get_ids_of_k_closest(k, distFromNewItem)
        labelCounts = np.zeros(len(self.labelsPresent))

        for k in range(k-1):
            thisindex = closestK[k]
            self.thislabel = self.y_train[thisindex]
            labelCounts[thisindex] 1
            thisPrediction = np.amax(labelCounts)

        return (thisPrediction)

def get_ids_of_k_closest(distFromNewItem, k):
    closestK = np.empty(k, dtype=int)
    arraySize = len(distFromNewItem.shape[0])
    for k in range(k-1):
        thisClosest = 0
        for exemplar in range(arraySize-1):
            if (distFromNewItem[exemplar] < distFromNewItem[thisClosest]):
                thisClosest = exemplar
                closestK[k] = thisClosest
        distFromNewItem[thisClosest] = 100000
    return (closestK)

CodePudding user response:

My guess. You are missing a @staticmethod and not prefixing the invocation with the class name:

class MyClass:
    def predict_new_item(self, newItem):
        ...

    @staticmethod
    def get_ids_of_k_closest(distFromNewItem, k):
        ...



closestK = MyClass.get_ids_of_k_closest(k, distFromNewItem)

CodePudding user response:

Function was inside the class when it should have been outside the class.

  • Related