1. Implementing norms¶
(a) manually implement the 0, 1,2, and $\infty$ norms as functions which take a basic python list of arbitrary length as input and output the vector norm. Do not use numpy or any other implementation of norm functions (you can use python's math library).
(b) compare your functions to numpy's norm functions for a random vector of length 1000.
2. Distances with different norms¶
(a) Manually implement a function that takes a m-by-n dataset, a m-by-1 vector, and a norm function (you can pass functions as inputs in python just like numbers). Have your new function use the input norm to compute the distance over all data, then return that array of distances as the function output.
(b) Demonstrate the function using all four norms for an example dataset consisting of a 100-by-10 random matrix, treating rows as samples. Plot the distances between a random vector with 10 elements and every row of the data using each norm. You can use numpy to generate this data, and to compute the differences between vectors, but not to compute norms. For the random data, use random integers between -10 and 10.
(c) Point out using the graph which indices appear to be nearest to the random vector for each norm.
Hint: make sure your vectors are the same shape when taking differences to avoid broadcasting rather than computing distance.
Hint2: you may need to make use of .flatten() and/or .tolist() to handle numpy arrays as input to a function which expects a list.
3. Class comparisons with different norms¶
Compute vectors of mean and standard deviation feature value for each of the (three) flower classes in the Iris dataset from sklearn (you can use numpy mean and std functions for this).
Using your distance function from #2, plot the distances between the feature vector and each of the class means, for every flower in the dataset.
From your plots, point out the samples for which the target label does not correspond to the nearest mean.
Repeat for the 1 and 2 norms.
Hint: plot iris.target to see which ranges of indices correspond to which classes