Python 2到3错误。TypeError:不可排序的类型:int()<= str()

用户名

我有一个基于k均值算法的程序。当我去在python 2上运行程序时,没有任何问题。当我在python 3上运行它时,出现以下错误:

Traceback (most recent call last):
  File "kmeans.py", line 111, in <module>
    main()
  File "kmeans.py", line 13, in main
    clusters = kmeans(points, num_cluster, cutoff)
  File "kmeans.py", line 67, in kmeans
    initial = random.sample(points, k) # generating k random points for initial centroids
  File "/afs/cad/linux/anaconda3/anaconda/pkgs/python-3.5.2-1/lib/python3.5/random.py", line 314, in sample
    if not 0 <= k <= n:
TypeError: unorderable types: int() <= str()

这是我到目前为止的代码,我似乎无法弄清楚该如何解决。

import sys
import math
import random

def main():
    points = readDataFile("datafile.txt")
    print ('Original dataset: \n' + str(points) + '\n')

    # generating clusters
    # num_cluster = 2 # k for default testing value
    num_cluster = input ('Please declare number of clusters k: ') #k
    cutoff = 0.5 # iter cut off
    clusters = kmeans(points, num_cluster, cutoff)

    # Print our clusters
    for i,c in enumerate(clusters):
      print ("Cluster " + str(i) + "\t" + str(c))

# reading in data points from file
def readDataFile(filename):
    points = []
    lines = [line.rstrip('\n') for line in open(filename)]
    for line in lines:
      pieces = line.split(' ')
      points.append(Point([int(pieces[0]), int(pieces[1])]))
    return points

# point class to contain a set of 2d coordinates
class Point:
    def __init__(self, coords):
        self.coords = coords
        self.n = len(coords)

    def __repr__(self):
        return str(self.coords)

# cluster class to define cluster functionality
class Cluster:
    # constructor
    def __init__(self, points):
        self.points = points
        self.n = points[0].n
        self.centroid = self.calculateCentroid()

    # to string method
    def __repr__(self):
        return str(self.points)

    # updates the current loc
    def update(self, points):
        old_centroid = self.centroid
        self.points = points
        self.centroid = self.calculateCentroid()
        shift = getDistance(old_centroid, self.centroid)
        return shift

    # calculates new location of centroid based on mean
    def calculateCentroid(self):
        numPoints = len(self.points) # finding center point
        coords = [p.coords for p in self.points] # list of all coordinates in curr cluster
        unzipped = zip(*coords)
        centroid_coords = [math.fsum(dList)/numPoints for dList in unzipped] # mean for each point
        return Point(centroid_coords)

# kmean algo to cluster data
def kmeans(points, k, cutoff):
    initial = random.sample(points, k) # generating k random points for initial centroids
    clusters = [Cluster([p]) for p in initial] # creating k clusters using generated centroids

    loopCounter = 0 # looping thru data until the clusters stabilize
    while True:
        # list of lists to hold point objects
        lists = [ [] for c in clusters]
        clusterCount = len(clusters)
        loopCounter += 1

        for p in points:
            # dist bw curr to first centroid
            smallest_distance = getDistance(p, clusters[0].centroid)
            clusterIndex = 0

            for i in range(clusterCount - 1):
                # calc point to point diff in distances
                distance = getDistance(p, clusters[i+1].centroid)
                # setting cluster index based on dist
                if distance < smallest_distance:
                    smallest_distance = distance
                    clusterIndex = i+1
            lists[clusterIndex].append(p) # appending point to cluster

        biggest_shift = 0.0 # resetting biggest_shift to zero for curr iteration
        for i in range(clusterCount):
            # calc centroid movement dist
            shift = clusters[i].update(lists[i])
            # keeping track of the largest move from all cluster centroid updates
            biggest_shift = max(biggest_shift, shift)

        # checking if centroids movement is not vast (convergence)
        if biggest_shift < cutoff:
            break
    return clusters


# generates euclidean distance between two points
def getDistance(a, b):
    ret = reduce(lambda x,y: x + pow((a.coords[y]-b.coords[y]), 2),range(a.n),0.0)
    return math.sqrt(ret)

# init
if __name__ == "__main__":
    main()

而且我的data.txt文件看起来像这样:

0 0
0 1
1 0
10 10
10 11
11 10
11 11

任何帮助将不胜感激。

深空
num_cluster = input ('Please declare number of clusters k: ') #k
cutoff = 0.5 # iter cut off
clusters = kmeans(points, num_cluster, cutoff)

input返回一个字符串,因此您需要将其转换为int

num_cluster = int(input ('Please declare number of clusters k: ')) #k

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python TypeError必须为str not int

Python str与unicode类型

TypeError:'str'对象不可调用(Python)

python-TypeError:无法排序的类型:str()> float()

groupby python TypeError:不可排序的类型:tuple()<str()

Python3-while ids> Stop:TypeError:不可排序的类型:str()> int()

错误TypeError:“ str”对象不可调用python

使用Python的TypeError:str和int

TypeError:'str'对象不可调用python

TypeError:'str'对象不可调用-Python

不可排序的类型:str()> int()

Python str():TypeError:'str'对象不可调用-停止代码

Python 3 TypeError:**或pow()不支持的操作数类型:“ str”和“ int”

TypeError'str'对象不可调用-Python

不可排序的类型:str()<int()

Python:类型错误:预期的 str、bytes 或 bytearray,而不是 int

Python:TypeError:无法连接“str”和“int”

类型错误:无法排序的类型:int() > str()

Python - 类型错误:无法排序的类型:str() < int()

类型错误:必须是 str,而不是 int;Python

Python str 错误

类型错误:将推文数据插入到 mysql Python 3 时,“str”对象不可调用

类型错误:“str”对象不可调用 Python 3.7.0

str 到 int Python 3

Python 错误消息:TypeError: 'str' 对象不可调用

Int + Str 类型的python

Python2 到 Python3 的转换?类型错误:内存视图:需要类似字节的对象,而不是“str”

Str 到 Int Python

不是 str 的 Iterable[str] 的 Python 类型提示