关于kNN这是一个简单的任务,我是pyhton的新手。
# coding=utf-8
from numpy import *
import operator
def createDataSet():
group = array([[112, 110], [128, 162], [83, 206], [142, 267], [188, 184], [218, 268], [234, 108], [256, 146], [
333, 177], [350, 86], [364, 237], [378, 117], [409, 147], [485, 130], [326, 344], [387, 326], [378, 435], [434, 375]])
labels = [1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3]
return group, labels
def classify0(inX, dataSet, labels, k):
dataSetSize = dataSet.shape[0]
tempSet = array(tile(inX, (dataSetSize, 1)))
diffMat = tempSet - dataSet
sqDiffMat = diffMat**2
sqDistances = sqDiffMat.sum(axis=1)
distances = sqDistances**0.5
sortedDistIndices = distances.argsort()
classCount = {}
for i in range(k):
voteLabel = labels[sortedDistIndices[i]]
classCount[voteLabel] = classCount.get(voteLabel, 0) + 1
sortedClassCount = sorted(classCount.iteritems(),
key=operator.itemgetter(1), reverse=True)
return sortedClassCount[0][0]
# TRY1
# def with_intput():
# sample = array(raw_input('Enter you data:'))
# group, labels = createDataSet()
# sampleClass = classify0(sample, group, labels, 3)
# print sampleClass
# with_intput()
# TRY1
# TRY2
# sample = array(raw_input('Enter your sample data:'))
# group, labels = createDataSet()
# sampleClass = classify0(sample, group, labels, 3)
# print sampleClass
# TRY2
有些事情很奇怪。我创建了一个函数名 classify0(),但是如果我在编写代码时调用它(取消注释#TRY1),或者使用它来进行分配(如果取消注释#TRT2),它将返回错误当我运行这个文件时。
出现喜欢:
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('S11') dtype('S11') dtype('S11')
以下是TRY1的追溯:
Traceback (most recent call last):
File "C:\Users\zhongzheng\Desktop\ML_Code\temp2.py", line 39, in <module>
with_intput()
File "C:\Users\zhongzheng\Desktop\ML_Code\temp2.py", line 36, in with_intput
sampleClass = classify0(sample, group, labels, 3)
File "C:\Users\zhongzheng\Desktop\ML_Code\temp2.py", line 17, in classify0
diffMat = tempSet - dataSet
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('S11') dtype('S11') dtype('S11')
TRY2的追溯:
Traceback (most recent call last):
File "C:\Users\zhongzheng\Desktop\ML_Code\temp2.py", line 46, in <module>
sampleClass = classify0(sample, group, labels, 3)
File "C:\Users\zhongzheng\Desktop\ML_Code\temp2.py", line 17, in classify0
diffMat = tempSet - dataSet
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('S11') dtype('S11') dtype('S11')
但是如果我保存文件而不取消注释TRT1或TRY2,保存并运行只包含两个函数的文件,然后在cmd或ipython中以交互模式逐行输入这些命令:
>>>group,labels = createDataSet()
>>>sampleClass = classify0(array([111,111]), group, labels, 3)
>>>print sampleClass
它会正常工作。
无法弄清楚原因。
还有一个问题,为什么我的sublime3(subliemlinter,pep8linter已安装)保持警告from numpy import *
或import numpy
或import numpy as np
是错误的。
感谢您的耐心等待。
答案 0 :(得分:0)
您的raw_input未达到您对classify0函数的预期效果。
sample = array(raw_input('Enter you data:'))
这会给出像[“111 111”]
这样的东西sample = [int(x) for x in raw_input().split()]
这会给[111,111]
您也可以将分隔符更改为拆分,即如果输入为逗号分隔,则使用a