[英]Getting error: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
I'm getting the error I stated in the title on my machine learning project. 我遇到了我在机器学习项目的标题中指出的错误。 I'm following a guide on the internet . 我正在网上浏览指南 。 here's the parts that I'm getting the error: 这是我遇到错误的部分:
def euclideanDistance(instance1, instance2, length):
distance = 0
for x in range(length):
distance += pow((instance1[x] - instance2[x]), 2)
return math.sqrt(distance)
def getNeighbors(trainingSet, testInstance, k):
distances = []
length = len(testInstance)-1
for x in range(len(trainingSet)):
dist = euclideanDistance(testInstance, trainingSet[x], length)
distances.append((trainingSet[x], dist))
distances.sort(key=operator.itemgetter(1))
neighbors = []
for x in range(k):
neighbors.append(distances[x][0])
return neighbors
neighbors = getNeighbors(training_feature_list, test_feature_list, 3)
print(neighbors)
I've looked around the internet about this question and noticed that many people asked this before but as I understand, the problem emerges from trying to use ufunc on different types of variables. 我在互联网上到处都是这个问题,并注意到之前有很多人问过这个问题,但是据我所知,问题出在试图对不同类型的变量使用ufunc。 But my training_feature_list and test_feature_list are similar. 但是我的training_feature_list和test_feature_list相似。
train set goes like [['5.1' '0.2']['4.9' '0.2']...(30 rows) 火车组像[['5.1''0.2'] ['4.9''0.2'] ...(30行)
test set goes like [['4.8' '0.2']['5.4' '0.4']...(20 rows). 测试集就像[['4.8''0.2'] ['5.4''0.4'] ...(20行)。
I'd be so glad if anyone could briefly explain why this problem emerges (because I probably didn't understand it well) and how to fix it. 如果有人能简要解释为什么出现此问题(因为我可能不太了解)以及如何解决它,我将感到非常高兴。
thanks in advance 提前致谢
If your lists really look like [['5.1' '0.2']['4.9' '0.2']... , then the error is probably caused by the fact that you are trying to subtract one string from another as '5.1' is a string, while 5.1 (which you prbably want) is a floating point number. 如果您的列表确实看起来像[[''5.1''0.2'] ['4.9''0.2'] ...,则该错误可能是由于您试图从另一个字符串中减去一个字符串作为'5.1'而引起的是一个字符串,而5.1(您可能希望使用)是一个浮点数。
If that is not the case than another possible cause for the error (although I would expect a different one) is that you are passing lists instead of numpy arrays, which you should preferably do for calculations, as you can not just subtract one list from another. 如果不是这种情况,则可能是导致错误的另一个可能原因(尽管我希望有另一个原因)是您传递的是列表而不是numpy数组,因此最好进行计算,因为您不能只从中减去一个列表另一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.