Question

In: Computer Science

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):

Solutions

Expert Solution

The function to get the new centroid was not given, so I took it as average of all data points associated with that cluster. (k-means)

Program:

def centroid(D, C):
  
   # the list of new centroids
   new_centroids = []
   # number of data points in a cluster (associated with a centroid)
   number_of_elements_in_cluster = []
   # number of clusters -- starting from 1 to n
   max_cluster_number = 0
  
   # number of data points
   n1 = len(D)
  
   # get number of clusters by getting the max cluster number
   for i in range(0,n1):
       if(C[i]>max_cluster_number):
           max_cluster_number = C[i]
  
   # initialze the new centroids as 0,0
   # and the number of elements in each cluster = 0
   for i in range(0, max_cluster_number):
       new_centroids.append([0,0])
       number_of_elements_in_cluster.append(0)
      
   # get the sum of data points in each cluster
   # add that data point in the number of elements for that cluster
   for i in range(0,n1):
       new_centroids[C[i]-1][0] += D[i][0]
       new_centroids[C[i]-1][1] += D[i][1]
       number_of_elements_in_cluster[C[i]-1] += 1
      
   # Divide each cluster sum by number of data points to get the means
   # which is the new centroid
   for i in range(0, max_cluster_number):
       new_centroids[C[i]-1][0] /= number_of_elements_in_cluster[C[i]-1]
       new_centroids[C[i]-1][1] /= number_of_elements_in_cluster[C[i]-1]
  
   return new_centroids

# sample run
D = [[2,0],[3,5],[4,6]]
C = [2,1,1]
print(centroid(D,C))


Related Solutions

this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
I need to add this checkpoint to an existing code that i have in python Checkpoint...
I need to add this checkpoint to an existing code that i have in python Checkpoint 1: Once you have created and tested the Bank Account Class create subclasses to the BankAccount class. There should be two subclasses, CheckingAccount and SavingsAccount. You should add interest_rate to the parent BankAccount class. To the CheckingAccount add variables called per_check_fee default to false and allow_overdraft default to True. To SavingsAccount add the variable transactions_per_month, default it to 5. Create instances of CheckingAccount and...
I need this in R code please: Use the dataset ’juul’ in package ’ISwR’ to answer...
I need this in R code please: Use the dataset ’juul’ in package ’ISwR’ to answer the question. (1) Conduct one-way ANOVA test to test if the mean of igf1 of each level of tanner are the same? (2) What is the mean of igf1 in each level of tanner? (3) If there is any difference, which ones appear to be different? (Use pairwise t test for each pair of level with bonferroni method)
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
I need the code in python where I can encrypt and decrypt any plaintext. For example,...
I need the code in python where I can encrypt and decrypt any plaintext. For example, the plaintext "hello" from each of these Block Cipher modes of Operation. Electronic Code Block Mode (ECB) Cipher block Mode (CBC) Cipher Feedback Mode (CFB) Output feedback Mode (OFB) Counter Mode (CTR) Here is an example, Affine cipher expressed in C. Encryption: char cipher(unsigned char block, char key) { return (key+11*block) } Decryption: char invcipher(unsigned char block, char key) { return (163*(block-key+256)) }
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
From the MNIST dataset introduced in class, write code in Python that a) Splits the 42000...
From the MNIST dataset introduced in class, write code in Python that a) Splits the 42000 training images into a training set (50% of all the data) and a test set (the rest). The labels should also be split accordingly. (PLEASE ONLY SOLVE 2 & 3) 2) Basically repeat Part 1, but now use 80% of the images for training and the other 20% for testing. Report scores. [10 points] 3) Use the SVM model from part 2 to print...
For the following code I need to open the file of a persons choice which I...
For the following code I need to open the file of a persons choice which I did. I can get it to read the file, but I need help with counting lines and characters feel like i have the code I just cant make it work. Either way, the program should then ask the user Analyze another file (y/n)? and repeat the process again (asking for a file and analyzing its meta-data). This should continue repeating until the user chooses...
Study and consider the following Python code, which tracks the movement of a balloon given the...
Study and consider the following Python code, which tracks the movement of a balloon given the wind direction and distance # Establish Balloon Behavior(Parts, Direction, Distance) n = int(input('Input number of parts: ')) northDistance = 0 eastDistance = 0 balloonOutput = [] for i in range(0, n):     outputLine = []     print('Part %d- ' % (i + 1), end='')     direction = input('What direction is the balloon heading? type N, E, S or W ')     direction = direction.upper()     while len(direction) != 1...
Study and consider the following Python code, which tracks the movement of a balloon given the...
Study and consider the following Python code, which tracks the movement of a balloon given the wind direction and distance # Establish Balloon Behavior(Parts, Direction, Distance) n = int(input('Input number of parts: ')) northDistance = 0 eastDistance = 0 balloonOutput = [] for i in range(0, n):     outputLine = []     print('Part %d- ' % (i + 1), end='')     direction = input('What direction is the balloon heading? type N, E, S or W ')     direction = direction.upper()     while len(direction) != 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT