Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:TypeError:函数至少接受2个参数_Python - Fatal编程技术网

Python:TypeError:函数至少接受2个参数

Python:TypeError:函数至少接受2个参数,python,Python,当我在书中做一些练习——机器学习在行动中,在约会匹配问题中,我遇到了以下问题,但我不知道为什么 Traceback (most recent call last): File "(stdin)", line 1, in (module) File 'kNN.py', line 27 ,in file2matrix fr = open(filename) TypeError: function takes at least 2 arguments (1 given) 这是我的密

当我在书中做一些练习——机器学习在行动中,在约会匹配问题中,我遇到了以下问题,但我不知道为什么

Traceback (most recent call last):
  File "(stdin)", line 1, in (module)
  File 'kNN.py', line 27 ,in file2matrix
     fr = open(filename)
TypeError: function takes at least 2 arguments (1 given)
这是我的密码:

    from numpy import *
    import operator
    from os import *

def file2matrix(filename):
    fr = open(filename)
    arrayOLines = fr.readlines()
    numberOfLines = len(arrayOLines)
    returnMat = zeros((numberOfLines,3))
    classLabelVector = []
    index = 0
    for line in arrayOLines:
        line = line.strip()
        listFromLine = line.split('\t')
        returnMat[index,:] = listFromLine[0:3]
        classLabelVector.append(int(listFromLine[-1]))
        index += 1
    return returnMat,classLabelVector
我做了修改,但问题仍然存在!
它描述了kNN算法。

您正在用
os
中的函数覆盖内置的open函数:

In [1]: open?
Docstring:
open(name[, mode[, buffering]]) -> file object

Open a file using the file() type, returns a file object.  This is the
preferred way to open a file.  See file.__doc__ for further information.
Type:      builtin_function_or_method

In [2]: import os

In [3]: os.open?
Docstring:
open(filename, flag [, mode=0777]) -> fd

Open a file (for low level IO).
Type:      builtin_function_or_method

这就是为什么您应该避免从某处导入*

您正在用操作系统内部的函数覆盖内置的打开函数的原因:

In [1]: open?
Docstring:
open(name[, mode[, buffering]]) -> file object

Open a file using the file() type, returns a file object.  This is the
preferred way to open a file.  See file.__doc__ for further information.
Type:      builtin_function_or_method

In [2]: import os

In [3]: os.open?
Docstring:
open(filename, flag [, mode=0777]) -> fd

Open a file (for low level IO).
Type:      builtin_function_or_method

这就是为什么您应该避免从某处导入*

哪些行是第27行?为什么在调用
方法时使用双括号<代码>零((numberOfLines,3))。我猜完了。:)疯狂的心理猜测:您显然使用了numpy import*中的
,您是否有可能使用其他定义
打开
函数或
int
len
的软件包?你可能导入了一些内置Python的阴影,现在它把你搞砸了。当然,这就是为什么x导入的
*
通常不受欢迎的原因,它打破了名称空间,而且您甚至没有指定要“展开”的名称。我打赌是这样的。认真地说,不要使用
import*
。哪一行是第27行?为什么在调用
zero
方法时使用双括号<代码>零((numberOfLines,3))
。我猜完了。:)疯狂的心理猜测:您显然使用了numpy import*中的
,您是否有可能使用其他定义
打开
函数或
int
len
的软件包?你可能导入了一些内置Python的阴影,现在它把你搞砸了。当然,这就是为什么x导入的
*
通常不受欢迎的原因,它打破了名称空间,而且您甚至没有指定要“展开”的名称。我打赌是这样的。说真的,不要使用
import*