我如何修复此Python类名称错误

发布于 2025-01-22 09:17:04 字数 3437 浏览 2 评论 0原文

在为KNN创建课程时,我具有这两个不同的功能“ precention_new_item”和“ get_ids_of_k_closest” ive创建了一个称为“ crosceStk”的变量,并将其设置为等于get_ids_f_closest等于

get_ids_closest

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)

下面添加了整个班级代码,以提出评论中的询问:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女净妖师 2025-01-29 09:17:04

我的猜测。您缺少@StaticMethod,而不是将调用以类名称为前面:

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)

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)

不必了 2025-01-29 09:17:04

当功能应该在班级外面时,功能就在班级内部。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文