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)...
In python I have my code written and I want just 4 functions to be fix...
In python I have my code written and I want just 4 functions to be fix in my code according to rule. My code is running but it has some problem regarding to rules. (I have did all the other things so you do not have to worry about other functions) 1) all the players has to play until he/she reaches to at least 500 points in first round. When user reach 500 points for the first time, user may...
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...
This is A PYTHON Question. Developing a machine learning system for a given dataset. Dataset Diabetes...
This is A PYTHON Question. Developing a machine learning system for a given dataset. Dataset Diabetes dataset - https://www.kaggle.com/uciml/pima-indians-diabetes-database Algorithms Either one of the following: 1. K-Nearest Neighbors 2. Support Vector Machines 3. Neural Networks Notes: Explain your choice of algorithms and analyze the models developed. Show what patterns/insights can be extracted from your chosen dataset and the selected algorithms.
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 to implement a code in python to guess a random number. The number must...
I need to implement a code in python to guess a random number. The number must be an integer between 1 and 50 inclusive, using the module random function randint. The user will have four options to guess the number. For each one of the failed attempts, the program it should let the user know whether is with a lower or higher number and how many tries they have left. If the user guess the number, the program must congratulate...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT