我如何修复此Python类名称错误
在为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测。您缺少
@StaticMethod
,而不是将调用以类名称为前面:My guess. You are missing a
@staticmethod
and not prefixing the invocation with the class name:当功能应该在班级外面时,功能就在班级内部。
Function was inside the class when it should have been outside the class.