2 lichenghao 96 lichenghao_96 于 2017.09.05 10:32 提问

function takes at least 2 arguments(1 given)

机器学习实战kNN篇手写数字识别:

 def img2vector(filename):
    file=open(filename)
    imgvector=zeros((1,1024))
    for i in range(32):
        line=file.readline()
        for j in range(32):
            imgvector[0,32*i+j]=int(line[j])
    return imgvector

def handwritingClassifier():
    hwlabels=[]
    trainingfilelist=listdir('trainingDigits')
    m=len(trainingfilelist)
    trainingMat=zeros((m,1024))
    for i in range(m):
        fn=trainingfilelist[i]
        filename1=fn.split('.')[0]
        filelabel=int(filename1.split('_')[0])
        hwlabels.append(filelabel)
        trainingMat[i,:]=img2vector('trainingDigits/%s' % fn)
    testfilelist=listdir('testDigits')
    mT=len(testfilelist)
    errorCount=0.0
    for i in range(mT):
        filenameT=testfilelist[i]
        filename1T=filenameT.split('.')[0]
        filelabelT=int(filename1T.split('_')[0])
        testfilevector=img2vector('testDigits/%s' % filenameT)
        predictlabel=Classifier(testfilevector,trainingMat,hwlabels,5)
        if (predictlabel!=filelabelT):
            errorCount+=1.0
        print "the classifier came back with %d ,the real answer is %d " % (predictlabel,filelabelT)
    print "the error rate is %f" % (errorCount/float(mT))

结果在命令行输入import kNN ...kNN.handwritingClassifier()的时候

TypeError Traceback (most recent call last)
in ()
1 import kNN
----> 2 kNN.handwritingClassifier()

C:\Users\Administrator\kNN.py in handwritingClassifier()
99 filelabel=int(filename1.split('_')[0])
100 hwlabels.append(filelabel)
--> 101 trainingMat[i,:]=img2vector('trainingDigits/%s' % fn)
102 testfilelist=listdir('testDigits')
103 mT=len(testfilelist)

C:\Users\Administrator\kNN.py in img2vector(filename)
81
82 def img2vector(filename):
---> 83 file=open(filename)
84 imgvector=zeros((1,1024))
85 for i in range(32):

TypeError: function takes at least 2 arguments (1 given)

小白一枚,请问到底哪里有问题,open函数不是只要一个参数吗,用的是python2.7望指教!!

2个回答

devmiao
devmiao   Ds   Rxr 2017.09.05 11:09

函数需要2个参数,你只给了1个,看看手册去

lichenghao_96
lichenghao_96   2017.09.05 11:23

解决了,不是这个问题,我开头from os import * 好像内置函数把原来的open覆盖了,新的open可能要两个,怪不得

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
TypeError: resize_images() takes at least 3 arguments (3 given)
用tensorflow对图像进行resize时遇到如下问题:       TypeError: resize_images() takes at least 3 arguments (3 given)   原句是这样的:     resized_image = tf.image.resize_images(img_data_jpg,(28,28), method=0)   百度半天没找
MonkeyRunner_TypeError: __init__() takes at least 3 arguments (2 given)
121213 13:37:13.553:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception 121213 13:37:13.553:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOption
python将数据写入excel文件时报错ew = ExcelWriter(workbook=wb) TypeError: __init__() takes exactly 3 arguments
python利用openpyxl 将数据写入excel文件时报错ew = ExcelWriter(workbook=wb) TypeError: __init__() takes exactly 3 arguments
python 调用super()初始化报错“TypeError: super() takes at least 1 argument”
介绍了如何解决python中的“TypeError: super() takes at least 1 argument(0 given)”和“TypeError: super() argument 1 must be type, not classobj”错误。
TypeError: add_or_update() takes at most 3 arguments (4 given)
明明传了三个值,提示传值错误 如果传值二个,传值过去又不正确 解决: @staticmethod   或     def _init__(self, *args, *kwargs):                 pass
TypeError: __init__() takes exactly 2 arguments (1 given)(已解决)
原因:在多个文件的引用过程中,本文件类无需新建对象。
TypeError: distinct() takes exactly 2 arguments (3 given)
出现原因,错误的写法: table.distinct('snp',{'pid':'xxx'}) 正确的
TypeError: print_space() takes exactly 2 arguments (3 given)
#!/usr/bin/python # Filename: print_info.py class print_info: name='' #define init method def __init__(self,a): self.name=a def print_space(str,status
Python 'takes exactly 1 argument (2 given)' Python error
Python初学,定义class的函数之后,在调用的时候出现“'takes exactly 1 argument (2 given)' Python error”。 查询Interesting 'takes exactly 1 argument (2 given)' Python error 之后,原来在python中,在使用instance调用其class的方法的时候,是相当与在调用中加入本身
TypeError in Threading. function takes 1 positional argument but 100 were given
Python Threading问题:TypeError in Threading. function takes 1 positional argument but 100 were given 在使用python多线程module Threading时 import threading t = threading.Thread(target=getTemperature, args =...