Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please asnwer the qeustion by fill in the format given below Question: Reference Format class KMeans: # K: the number of clusters def __init__(self, K):

Please asnwer the qeustion by fill in the format given below

Question:

image text in transcribed

Reference Format

class KMeans: # K: the number of clusters def __init__(self, K): self.K = K

# The features of centroids; set when `fit` is called self.cluster_centers_ = None

# fit: make clusters # X: data points to cluster -- np.ndarray # (shape: [# of data points, # of features]) def fit(self, X): # TODO: IMPLEMENT ME # Store the feature ndarray of centroids in `self.cluster_centers_` # The shape of `self.cluster_centers_` has to be # [self.K, # of features] pass

# predict: Predict the cluster indices of input data points # X: data points predicted -- np.ndarray # (shape: [# of data points, # of features]) # Return an ndarray with shape [# of data points] where # each element is an integer from 0 to self.K-1 def predict(self, X): # TODO: IMPLEMENT ME pass

# check this is a main file if __name__ == '__main__': import numpy as np from sklearn.datasets import load_iris from sklearn.metrics import mean_squared_error

K = 3 iris_dataset = load_iris() kmeans = KMeans(K) kmeans.fit(iris_dataset.data) predict = kmeans.predict(iris_dataset.data) for k in range(K): indices = np.where(predict == k) features = iris_dataset.data[indices] MSE = mean_squared_error( np.tile(kmeans.cluster_centers_[k], (features.shape[0], 1)), features) print('Cluster', k, 'MSE', MSE) assert(MSE

Assignment: K-Means You are required to submit Python code that includes an implementation of K-Means and a test on it. It is allowed to use the attached template kmeans.py (including a minimum test) and fill the unimplemented parts there. The sub- mitted code must pass the same test as that in kmeans.py (even for the case that you do not use the template and write code only by yourself)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

3-1. What are the three steps in the writing process? [LO-1]

Answered: 1 week ago

Question

Current market share for Perrier water

Answered: 1 week ago

Question

Contact information for the American Management Association

Answered: 1 week ago

Question

How do proposal writers use an RFP? [LO-7]

Answered: 1 week ago