Python Forum
AttributeError: module 'sys' has no attribute 'maxint'
Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: module 'sys' has no attribute 'maxint'
#1
import math; #For pow and sqrt
import sys;
from random import shuffle, uniform;

###_Pre-Processing_###
def ReadData(fileName):
    #Read the file, splitting by lines
    f = open(fileName,'r');
    lines = f.read().splitlines();
    f.close();

    items = [];

    for i in range(1,len(lines)):
        line = lines[i].split(',');
        itemFeatures = [];

        for j in range(len(line)-1):
            v = float(line[j]); #Convert feature value to float
            itemFeatures.append(v); #Add feature value to dict
    
        items.append(itemFeatures);

    shuffle(items);

    return items;


###_Auxiliary Function_###
def FindColMinMax(items):
    n = len(items[0]);
    minima = [sys.maxsize for i in range(n)];
    maxima = [-sys.maxsize-1 for i in range(n)];
    
    for item in items:
        for f in range(len(item)):
            if(item[f] < minima[f]):
                minima[f] = item[f];
            
            if(item[f] > maxima[f]):
                maxima[f] = item[f];

    return minima,maxima;

def EuclideanDistance(x,y):
    S = 0; #The sum of the squared differences of the elements
    for i in range(len(x)):
        S += math.pow(x[i]-y[i],2);

    return math.sqrt(S); #The square root of the sum

def InitializeMeans(items,k,cMin,cMax):
    #Initialize means to random numbers between
    #the min and max of each column/feature
    
    f = len(items[0]); #number of features
    means = [[0 for i in range(f)] for j in range(k)];
    
    for mean in means:
        for i in range(len(mean)):
            #Set value to a random float
            #(adding +-1 to avoid a wide placement of a mean)
            mean[i] = uniform(cMin[i]+1,cMax[i]-1);

    return means;

def UpdateMean(n,mean,item):
    for i in range(len(mean)):
        m = mean[i];
        m = (m*(n-1)+item[i])/float(n);
        mean[i] = round(m,3);
    
    return mean;

def FindClusters(means,items):
    clusters = [[] for i in range(len(means))]; #Init clusters
    
    for item in items:
        #Classify item into a cluster
        index = Classify(means,item);

        #Add item to cluster
        clusters[index].append(item);

    return clusters;


###_Core Functions_###
def Classify(means,item):
    #Classify item to the mean with minimum distance
    
    minimum = sys.maxint;
    index = -1;

    for i in range(len(means)):
        #Find distance from item to mean
        dis = EuclideanDistance(item,means[i]);

        if(dis < minimum):
            minimum = dis;
            index = i;
    
    return index;

def CalculateMeans(k,items,maxIterations=100000):
    #Find the minima and maxima for columns
    cMin, cMax = FindColMinMax(items);
    
    #Initialize means at random points
    means = InitializeMeans(items,k,cMin,cMax);
    
    #Initialize clusters, the array to hold
    #the number of items in a class
    clusterSizes = [0 for i in range(len(means))];

    #An array to hold the cluster an item is in
    belongsTo = [0 for i in range(len(items))];

    #Calculate means
    for e in range(maxIterations):
        #If no change of cluster occurs, halt
        noChange = True;
        for i in range(len(items)):
            item = items[i];
            #Classify item into a cluster and update the
            #corresponding means.
        
            index = Classify(means,item);

            clusterSizes[index] += 1;
            means[index] = UpdateMean(clusterSizes[index],means[index],item);

            #Item changed cluster
            if(index != belongsTo[i]):
                noChange = False;

            belongsTo[i] = index;

        #Nothing changed, return
        if(noChange):
            break;

    return means;


###_Main_###
def main():
    items = ReadData('data.txt');
    
    k = 3;

    means = CalculateMeans(k,items);
    clusters = FindClusters(means,items);
    print (means)
    print (clusters);

    #newItem = [5.4,3.7,1.5,0.2];
    #print Classify(means,newItem);

if __name__ == "__main__":
    main();
Traceback (most recent call last):
File "C:\python\program\Machine Learning\Clustering\kMeans - Standard\kMeans_Plot.py", line 57, in <module>
main();
File "C:\python\program\Machine Learning\Clustering\kMeans - Standard\kMeans_Plot.py", line 52, in main
means = kMeans.CalculateMeans(k,items);
File "C:\python\program\Machine Learning\Clustering\kMeans - Standard\kMeans.py", line 128, in CalculateMeans
index = Classify(means,item);
File "C:\python\program\Machine Learning\Clustering\kMeans - Standard\kMeans.py", line 92, in Classify
minimum = sys.maxint;
AttributeError: module 'sys' has no attribute 'maxint'
Reply
#2
maxint was taken out of sys in Python 3.0, since integers no longer have a maximum value. sys.maxsize is often used as a replacement, although it is more like the largest size for a list or other such structure.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
What language is this?  My eyes are giving SyntaxErrors due to all the semicolons.
Reply
#4
(Sep-13-2017, 07:16 PM)nilamo Wrote: What language is this? My eyes are giving SyntaxErrors due to all the semicolons.

[Image: 21742869_1464324613616412_83511826074139...e=5A5B7989]
Reply
#5
Then there's people that take it the other way, too.  I've seen javascript where all the semicolons were in the 80th column, so they weren't visible in their text editor.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 791 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,759 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 767 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,931 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,402 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,691 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,144 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,443 Jun-24-2022, 08:37 AM
Last Post: Anushka00
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,914 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  AttributeError: 'function' object has no attribute 'metadata 3lnyn0 5 4,648 Mar-28-2022, 04:42 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020