Question

I need this code to be written in Python: Given a dataset, D, which consists of...

I need this code to be written in Python:

Given a dataset, D, which consists of (x,y) pairs, and a list of cluster assignments, C, write a function centroids(D, C) that computes the new (x,y) centroid for each cluster. Your function should return a list of the new cluster centroids, with each centroid represented as a list of x, y:

def centroid(D, C):

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def centroid(D,C):
clusters_vote = []
for (x,y) in D: # for each point
dist_from_cluster = []
for (cx,cy) in C: # for each cluster center
d = (x-cx)**2+(y-cy)**2 # calculate distance form the cluster
dist_from_cluster.append(d)
# find closest cluster center to this point
closest_cluster_ind = dist_from_cluster.index(min(dist_from_cluster))
clusters_vote.append(closest_cluster_ind)

# Recentering
new_C = []
for i in range(len(C)): # for each cluster index
# find the point whose is closest to i
cx = 0;
cy = 0;
count =0;
for j in range(len(D)):
if i==clusters_vote[j]:
cx += D[j][0]
cy += D[j][1]
count +=1;
if count>0:
cx = cx/count # centeroid will be average of all point closter to that cluster
cy = cy/count
else: # if no count cluster will remain as previous
cx,cy = C[i][0],C[i][1]
new_C.append((cx,cy))
return new_C
  

D = [(1,2),(0,0),(0,1),(1,0),(11,12),(10,10),(10,11),(11,10)]
C = [(13,13),(1,1),(5,5)]
centroid(D,C)
  

In [22]: def centroid(D,C): clusters_vote = [] for (x,y) in D: # for each point dist_from_cluster = [] for (cx,cy) in C: # fo

Add a comment
Know the answer?
Add Answer to:
I need this code to be written in Python: Given a dataset, D, which consists of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to rewrite this code without using lambda function using python. def three_x_y_at_one(x): result =...

    I need to rewrite this code without using lambda function using python. def three_x_y_at_one(x): result = (3 * x *1) return result three_x_y_at_one(3) # 9 zero_to_four = list(range(0, 5)) def y_values_for_at_one(x_values): return list(map(lambda x : three_x_y_at_one(x), x_values)) -------> this is the function that I need to rewrite without using a lambda function . y_values_for_at_one(zero_to_four) # [0, 3, 6, 9, 12] Thanks

  • I need help with my python class. thanks! Question 12 Code Example 4-4 main program: import...

    I need help with my python class. thanks! Question 12 Code Example 4-4 main program: import arithmetic as a def multiply(num1, num2):     product = num1 * num2     result = a.add(product, product)     return result     def main():     num1 = 4     num2 = 3     answer = multiply(num1, num2)     print("The answer is", answer) if __name__ == "__main__":     main() arithmetic module: def add(x, y):     z = x + y     return z Refer to Code...

  • To insure that file output is written to the disk you need to execute what method?...

    To insure that file output is written to the disk you need to execute what method? a. commit() b. write() c. close() d. complete() The following is called the ________ of a function. def myFunction(x,y) : a. header b. footer c. sentinel d. index True or False: A commonly used program that uses regular expressions is grep. True or False: In Python exception handling provides a mechanism for passing control from the point of the error detection to a handler...

  • With the code given write python code that prints the probablity of getting a straight in...

    With the code given write python code that prints the probablity of getting a straight in poker when you run 10**5 trails. HELPER CODE: # We will represent cards as a string, e.g., 'AC' will be Ace of Clubs # Denominations: 2, ..., 10, 'J' = Jack, 'Q' = Queen, 'K' = King, 'A' = Ace Denominations = ['2','3','4','5','6','7','8','9','10','J','Q','K','A'] # Suits 'S' = Spades, 'H' = Hearts, 'D' = Diamonds, 'C' = Clubs Suits = ['C', 'H', 'S', 'D'] #...

  • Write a function in python that takes a set A and a relation R(x, y) on...

    Write a function in python that takes a set A and a relation R(x, y) on A (as a python function such that R(x, y) returns true if and only if the relation xRy holds), and returns True if and only if the relation R is reflexive. Here is the function signature you need to use. def is reflexive(A, R): You can test your code as follows. s = {1,2,3} def y(x, y): return x == y def n(x, y):...

  • Write a Python Code for a Function: you need to come up with code for shift_char....

    Write a Python Code for a Function: you need to come up with code for shift_char. Write the code in a way in which you can shift the character by writing the number of shifts. Use the ASCII code for this. For example in lie 11, the input for char_to shift is r. U shift it by 1 so it becomes s. below is the function def shift_char(char_to_shift, amount_to_shift): ''' shifts any character by moving it a certain amount on...

  • Please write the following code as simple as possible in python: You will need to define...

    Please write the following code as simple as possible in python: You will need to define a function with four arguments. Here is what I used: > def find_matches(file1.txt, output1.txt, strings, value): file1.txt will contain a list of various strings. The program must copy from the first argument, and it should be written in the second argument (the second file, "output1.txt"). The third and fourth arguments will determine which specific strings will be copied over to the second file. For...

  • PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK...

    PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK YOU! Bonus Question Implement an efficient algorithm (Python code) for finding the 11th largest element in a list of size n. Assume that n will be greater than 11. det find 11th largest numberlissy Given, lissy, a list of n integers, the above function will return the 11th largest integer from lissy You can assume that length of lissy will be greater than 11....

  • I need help writing a code for this. It has to be written in Python. Thank...

    I need help writing a code for this. It has to be written in Python. Thank you. Suppose there is a class A Conditioner. The dass supports the following behaviors: turning the air condition on, off and setting the desired temperature. The following which accept no arguments and return no value, and set temp, which accepts an int argument and returns no value methods are provided for these behaviors: tur n on and turn off There is a reference yariable...

  • python List1=[ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2]]...

    python List1=[ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2]] 1 def function(Letter,List),example, function("c",List) return 3 2 def function(word,List),example, function("a",List) return 1, function("be",List) return 4, function("bee",[b,5],[e,20]) return 45 Dictionary = ["a","bee","ad","ae"] 3 ["a","b","y","e"] return[["a",1],["ae",2],["bee",5]] 4 input a list, find largest value words can spell, return words and value dont using loop, using recursion for those code

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT