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をコメント解除する)ときに呼び出すか、(#TRT2をコメント解除すると)assingmentを使用してこのファイルを実行するとエラーを返します。
は同類を出現:ここ
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の場合は、2つの関数だけを保存して実行し、これらのコマンドを1行ずつ入力しますcmdまたはipythonのctiveモード:
>>>group,labels = createDataSet()
>>>sampleClass = classify0(array([111,111]), group, labels, 3)
>>>print sampleClass
正常に動作します。
理由を特定できません。 (pep8linterインストールsubliemlinter、)私のsublime3がfrom numpy import *
またはimport numpy
またはimport numpy as np
が間違っているwarnning続ける理由
もう一つ質問、。あなたの忍耐のため
感謝。
トレースバックを含めることはできますか? –
あなたの "もう一つの質問"への答えは、一般的にnumpyのようなモジュールから 'import *'をしたくないということです - あなたが実際に使っている 'numpy'の機能を見分けにくくなり、組み込みの名前。 – exp1orer
申し訳ありませんが、あまりにも無意味です。@ JoseRaulBarreras – aslan