Python matrix using min function in k-means -
i trying learn k-means book"machine learning in action" now. using code given book in ipython notebook, outcome
matrix([[<map object @ 0x0000000008832c88>]], dtype=object)
happened after input locmat = mat(loaddataset("user1.txt"))
, min(locmat[:,0])
.
what meaning of outcome? why not exact value 3.245555? code showed below, thank in advanced!
def loaddataset(filename): datamat = [] fr = open(filename) line in fr.readlines(): curline = line.strip().split('\t') fltline = map(float,curline) #map elements float() datamat.append(fltline) return datamat def disteclud(veca, vecb): return sqrt(sum(power(veca - vecb, 2))) #la.norm(veca-vecb) def randcent(dataset, k): n = shape(dataset)[1] centroids = mat(zeros((k,n))) j in range(n): minj = min(dataset[:,j]) rangej = float(max(dataset[:,j]) - minj) centroids[:,j] = mat(minj + rangej * random.rand(k,1)) return centroids
this happening because you're using python 3, map
returns generator instead of list.
you need use fltline = list(map(float, curline))
or fltline = [float(x) x in curline]
make sure result list, things work expected.
since assume you're using numpy here, can use genfromtxt
function load data file:
>>> import numpy np >>> np.genfromtxt(filename) array([[ 1., 2., 3.], [ 4., 5., 6.]])
Comments
Post a Comment