2016-09-30 42 views
0

這是一個關於kNN的簡單任務,而且我是pyhton的新手。python-無法在腳本中調用函數,但可以在交互模式下運行

# 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),或者用它來做出assingment(如果取消註釋#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安裝)保持WARNNING from numpy import *import numpyimport numpy as np是錯誤的。

感謝您的耐心等待。

+0

您可以包含回溯? –

+1

對於「多一個問題」的答案是,通常情況下,您不希望從諸如numpy之類的模塊中導入*,這很難看出您實際使用的「numpy」功能,而且可能會遮蓋內置名稱。 – exp1orer

+0

對不起,我太沒心沒氣了@ JoseRaulBarreras – aslan

回答

0

您的raw_input沒有采用您期望的classify0函數的輸入。

sample = array(raw_input('Enter you data:')) 

這將給像[ 「111 111」]

sample = [int(x) for x in raw_input().split()] 

這將使[111111]

你也可以改變分隔符,如果拆就,即用,輸入以逗號分隔

+0

它的工作原理。非常感謝! @西蒙布萊克 – aslan

相關問題