在k-means中使用min函数的Python矩阵。

时间:2022-02-26 16:00:59

I am trying to learn k-means from Book"Machine Learning in Action" now. Using the code given by this book in ipython notebook, the outcome

我正在尝试从书本“机器学习的行动”中学习k-means。使用这本书在ipython笔记本中给出的代码,结果。

matrix([[<map object at 0x0000000008832C88>]], dtype=object)

happened after I input locMat = mat(loadDataSet("user1.txt")) and min(locMat[:,0]).

在输入locMat = mat(loadDataSet("user1.txt"))和min(locMat[: 0])后发生。

What the meaning of that outcome? Why is not a exact value like 3.245555? The code is showed below, thank you in advanced!

这个结果的意义是什么?为什么它的值不像3。245555?代码如下,谢谢您先进!

def loadDataSet(fileName):     
    dataMat = []             
    fr = open(fileName)
    for line in fr.readlines():
    curLine = line.strip().split('\t')
    fltLine = map(float,curLine) #map all elements to 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)))
    for 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

1 个解决方案

#1


0  

This is happening because you're using Python 3, where map returns a generator instead of a list.

这是因为您使用的是Python 3,其中map返回的是生成器而不是列表。

You need to use fltLine = list(map(float, curLine)) or fltLine = [float(x) for x in curLine] to make sure that the result is a list, then things will work as expected.

您需要使用fltLine = list(map(float, curLine))或fltLine = [float(x) for x in curLine],以确保结果是一个列表,然后事情将按照预期工作。

Since I assume you're using numpy here, you can use the genfromtxt function to load your data file:

因为我假设这里使用的是numpy,所以可以使用genfromtxt函数来加载数据文件:

>>> import numpy as np
>>> np.genfromtxt(fileName)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

#1


0  

This is happening because you're using Python 3, where map returns a generator instead of a list.

这是因为您使用的是Python 3,其中map返回的是生成器而不是列表。

You need to use fltLine = list(map(float, curLine)) or fltLine = [float(x) for x in curLine] to make sure that the result is a list, then things will work as expected.

您需要使用fltLine = list(map(float, curLine))或fltLine = [float(x) for x in curLine],以确保结果是一个列表,然后事情将按照预期工作。

Since I assume you're using numpy here, you can use the genfromtxt function to load your data file:

因为我假设这里使用的是numpy,所以可以使用genfromtxt函数来加载数据文件:

>>> import numpy as np
>>> np.genfromtxt(fileName)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])