Question

# So, i have this code in python that i'm running. The input file is named input2.txt...

So, i have this code in python that i'm running. The input file is named input2.txt and looks like

1.8
4.5
1.1
2.1
9.8
7.6
11.32
3.2
0.5
6.5

The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this

Sample Program Output

70

-

510, [semester] [year]

NAME: [put your name here]

PROGRAMMING ASSIGN

MENT #2

Enter the name of the input file: prog2

-

input

-

d

ata.txt

Enter the name of the output file: prog2

-

output

-

data.txt

Enter the number of clusters: 5

Iteration 1

0 [1.8]

1 [4.5, 6.5]

2 [1.1, 0.5]

3 [2.1, 3.2]

4 [9.8, 7.6, 11.32]

Iteration 2

0 [1.8, 2.1]

1 [4.5, 6.5]

2 [1.1, 0.5]

3 [3.2]

4 [9.8, 7.6, 11.32]

Iteration 3

0 [1.8, 2.1]

1 [4.5, 6.5]

2 [1.1, 0.5]

3 [3.2]

4 [9.8, 7.6, 11.32]

Output File Contents

Point 1.8 in cluster 0

Point 4.5 in cluster 1

Point 1.1 in cluster 2

Point 2.1 in cluster 0

Point 9.8 in cluster 4

Point 7.6 in cluster 4

Point 11.32 in cluster 4

Point 3.2 in cluster 3

Point 0.5 in cluster 2

Point 6.5 in cluster 1

Any helps would be great.

Thanks

Code

def get_centroids(clusters,k):

centroids = {}

for x in range(k):

c = []

# print x

if(len(clusters[x])!=0):

for y in range(len(clusters[x][0])):

z=0.0

for l in range(len(clusters[x])):

z=z+clusters[x][l][y]

z = z/ len(clusters[x])

c.append(z)

centroids[x] = c

return centroids

#euclidian distance 2-dimensional

def distance(c,p):

dist = 0.0

for x in range(len(p)):

dist = dist + (p[x]-c[x])**2

return dist**0.5

def make_clusters(centroids,points,k):

clusters = {}

for x in range(k):

clusters[x] = []

for p1 in points:

mini = 100000000.0

cluster_number=-1

for x in range(k):

dist = distance(centroids[x],p1)

if(dist<mini):

mini = dist

cluster_number=x

clusters[cluster_number].append(p1)

return clusters

print "Python program for k-means clustering"

I = raw_input("Please enter the input file name: ")

O = raw_input("Please enter the output file name: ")

k = int(raw_input("Please enter the number of required clusters: "))

I= open(I)

O = open(O,'w')

# f= open("output2.txt")

# k=10

points = []

#strips data and makes list of list of tuples

for line in I:

line = line.rstrip()

line = line.split()

for x in range(len(line)):

line[x] = float(line[x])

points.append(line)

keys = []

values = []

#takes points and appends to centroids based on k as dictionary. No random aspect

for x in range(k):

keys.append(x)

values.append(points[x])

centroids = dict(zip(keys,values))

def get_centroids(clusters,k):

centroids = {}

for x in range(k):

c = []

# print x

if(len(clusters[x])!=0):

for y in range(len(clusters[x][0])):

z=0.0

for l in range(len(clusters[x])):

z=z+clusters[x][l][y]

z = z/ len(clusters[x])

c.append(z)

centroids[x] = c

return centroids

#euclidian distance 2-dimensional

def distance(c,p):

dist = 0.0

for x in range(len(p)):

dist = dist + (p[x]-c[x])**2

return dist**0.5

def make_clusters(centroids,points,k):

clusters = {}

for x in range(k):

clusters[x] = []

for p1 in points:

mini = 100000000.0

cluster_number=-1

for x in range(k):

dist = distance(centroids[x],p1)

if(dist<mini):

mini = dist

cluster_number=x

clusters[cluster_number].append(p1)

return clusters

print "Python program for k-means clustering"

I = raw_input("Please enter the input file name: ")

O = raw_input("Please enter the output file name: ")

k = int(raw_input("Please enter the number of required clusters: "))

I= open(I)

O = open(O,'w')

# f= open("output2.txt")

# k=10

points = []

#strips data and makes list of list of tuples

for line in I:

line = line.rstrip()

line = line.split()

for x in range(len(line)):

line[x] = float(line[x])

points.append(line)

keys = []

values = []