In Readme.md for both E11 and E12 there are questions that needs to be answered by sending in code, plots or text. This is all explained in the “Readme.md” for E11 and E12 There are 9 questions in E11...

1 answer below »



In Readme.md for both E11 and E12 there are questions that needs to be answered by sending in code, plots or text. This is all explained in the “Readme.md” for E11 and E12


There are 9 questions in E11 and 9 questions in E12.


First question is always the code and it needs to be filled in template.py





Answered 5 days AfterNov 02, 2021

Answer To: In Readme.md for both E11 and E12 there are questions that needs to be answered by sending in code,...

Vicky answered on Nov 08 2021
121 Votes
kmeans/1_6_1.png
kmeans/1_7_1.png
kmeans/2_1_1.png
kmeans/2_1_2.png
kmeans/2_1_3.png
kmeans/2_1_4.png
kmeans/3_2_1.png
kmeans/template.py
import numpy as np
import sklearn as sk
from sklearn.cluster import KMeans
from sklearn.metrics import accuracy_s
core, confusion_matrix
from sklearn.mixture import GaussianMixture
import matplotlib.pyplot as plt
from typing import Union
from tools import load_iris, image_to_numpy, plot_gmm_results
def distance_matrix(
X: np.ndarray,
Mu: np.ndarray
) -> np.ndarray:
'''
Returns a matrix of euclidian distances between points in
X and Mu.
Input arguments:
* X (np.ndarray): A [n x f] array of samples
* Mu (np.ndarray): A [k x f] array of prototypes
Returns:
out (np.ndarray): A [n x k] array of euclidian distances
where out[i, j] is the euclidian distance between X[i, :]
and Mu[j, :]
'''
return np.array([[np.linalg.norm(i - j) for j in Mu] for i in X])
def determine_r(dist: np.ndarray) -> np.ndarray:
'''
Returns a matrix of binary indicators, determining
assignment of samples to prototypes.
Input arguments:
* dist (np.ndarray): A [n x k] array of distances
Returns:
out (np.ndarray): A [n x k] array where out[i, j] is
1 if sample i is closest to prototype j and 0 otherwise.
'''
return np.array([[1 if j == np.min(i) else 0 for j in i] for i in dist])
def determine_j(R: np.ndarray, dist: np.ndarray) -> float:
'''
Calculates the value of the objective function given
arrays of indicators and distances.
Input arguments:
* R (np.ndarray): A [n x k] array where out[i, j] is
1 if sample i is closest to prototype j and 0
otherwise.
* dist (np.ndarray): A [n x k] array of distances
Returns:
* out (float): The value of the objective function
'''
return np.sum(np.multiply(R, dist)) / R.shape[0]
def update_Mu(
Mu: np.ndarray,
X: np.ndarray,
R: np.ndarray
) -> np.ndarray:
'''
Updates the prototypes, given arrays of current
prototypes, samples and indicators.
Input arguments:
Mu (np.ndarray): A [k x f] array of current prototypes.
X (np.ndarray): A [n x f] array of samples.
R (np.ndarray): A [n x k] array of indicators.
Returns:
out (np.ndarray): A [k x f] array of updated prototypes.
'''
out = np.empty((2, 3), dtype=object)
out[:1, :] = np.multiply(X[:1, :], Mu[:1, :])
out[1:2, :] = X[1:2, :]
return out
def k_means(
X: np.ndarray,
k: int,
num_its: int
)...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here