Reputation: 17
when I do some exercises in the book --Machine learning in Action, in the dating matching problem, I meet the following problem, but I don't know why!
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)
Here is my code:
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
I made the modification,but the problem still exists! It describe the kNN algorithm.
Upvotes: 0
Views: 2105
Reputation: 22989
You are overwriting the built-in open function with the one inside os
:
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
This is why you should avoid from somewhere import *
Upvotes: 3