python-Cannot call a function in script but can in the interactive mode
<p>It's a simple task about kNN, and I'm a newbee of pyhton.</p>

<pre><code># 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
</code></pre>

<p>There is something really strange. I created a function name <strong>classify0()</strong>, but if i call it while writing the codes(uncomment the #TRY1),or use it to make assingment(if uncomment the #TRT2), it will return error when I run this file.</p>

<p>Appears likes:</p>

<pre><code>TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('S11') dtype('S11') dtype('S11')
</code></pre>

<p>Here is the traceback of TRY1:</p>

<pre><code>Traceback (most recent call last):
  File "C:\Users\zhongzheng\Desktop\ML_Code\temp2.py", line 39, in &lt;module&gt;
    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')
</code></pre>

<p>And the traceback of TRY2:</p>

<pre><code>Traceback (most recent call last):
  File "C:\Users\zhongzheng\Desktop\ML_Code\temp2.py", line 46, in &lt;module&gt;
    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')
</code></pre>

<p>But if I save the file without uncommenting  either TRT1 or TRY2, save and run the file with only two functions in it, then enter these commands line by line in interactive mode in cmd or ipython:</p>

<pre><code>&gt;&gt;&gt;group,labels = createDataSet()
&gt;&gt;&gt;sampleClass = classify0(array([111,111]), group, labels, 3)
&gt;&gt;&gt;print sampleClass
</code></pre>

<p>It will work just fine.</p>

<p>Cannot figure out why.</p>

<p><strong>One more question</strong>, why my sublime3(subliemlinter, pep8linter installed) keeps warnning <code>from numpy import *</code> or <code>import numpy</code> or <code>import numpy as np</code> is wrong.</p>

<p>Thanks for your patience.</p>

<p>Your raw_input is not taken what you expect as input for the classify0 function.</p>

<pre><code>sample = array(raw_input('Enter you data:'))
</code></pre>

<p>This would give something like ["111 111"]</p>

<pre><code>sample = [int(x) for x in raw_input().split()]
</code></pre>

<p>This would give [111,111]</p>

<p>You could also change the delimiter to split on, i.e. use a , if input is comma separated</p>

python numpy