Machine Learning-Python

profilesara98
homework2.py

#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ CMPE 471 - CMPS 497 Homework 2 DUE: 21/2/2021 at 11:59pm (Sunday midnight) Q1: What is your name? Q2: What is your QUID? Q3: (4 pts) Name two advantages and two disadvantages of KNN when it is compared to Decision Trees: A3: Q4: (5 pts) Identify which of the classifiers learned so far (KNN, DT, or Perceptron) will not be suitable for the 2-D binary classification problem described by the data inside the file dataset.csv. In your own words, justify why you choose the classifier in your answer. Hint: Use scatter with two different colors for positive and negative examples A4: Fill in the body of the functions below following their descriptions """ def find_closest_example(data, test_example): """ (5 pts) Using the euclidean distance, this function finds the position of the closest example in the "data" parameter to "test_example". So, if the closest example is the third one inside data, then it returns 2. If it is the fifth example, it returns the number 4...etc Parameters: ---------- data: 2-D numpy array continuous data in N x M dimensions test_example: 1-D array with with M values for a test example Returns: ---------- The "index" of the closest example (one integer). """ return None def calculate_centroid_pos(data): """ (5 pts) function receives a 2D numpy array "data" (N x M dimensions) and calculates the new "updated" centroid position for these data Note: assume all the the examples given in "data" belong to one cluster function should return a numpy array with 1xM dimensions (centroid position). Parameters: ---------- data: 2D numpy array (N x M dim) with continuous numerical values. Returns: --------- The numpy array with 1xM dimensions of the centroid: one centroid position in M dimensions. """ return None def classify_examples(weights, test_examples): """ (5 pts) This function classifies examples using the perceptron algorithm. To do that, it receives one single dimensional numpy array weights, and 2D numpy array (test_examples) then returns a 1D numpy array with the classification results for each example (1 is positive, and -1 if it is a negative). Parameters: ---------- weights: 1-D numpy array with the perceptron weights and bias (bias is in position 0) test_examples: 2-D numpy array with the feature-values of the test examples. Note: the width (number of columns) of test_examples should be length of weights - 1. Note: assume weights and the features vectors follow the same order: e.g. w0, w1, w2, w3... x1, x2, x3... Returns: ---------- 1D numpy array with the classification results of the test_examples: (each is either 1, or -1). """ return None # BELOW IS "EXTRA PRACTICE" (FOR FUN ONLY AND NOT GRADED) def kmeans_cluster(data, k_init_centroids): """ (extra practice) Can you combine the two parts above to create the K-Means algorithm? This is where you can try it. Use the above two methods (find_closest_example, calculate_centroid_pos) to implement the K-Means algorithm. It should terminate either when no examples move across clusters, or if the number of iterations reaches 10. Parameters: ---------- data: 2-d numpy array of the data to be clustered (data can be more than 2 features) k_init_centroids: 2d numpy array containing the initial centroid positions (number of rows should determine "K"). Returns: ---------- the final centroid positions (should be the same length and dimension as the k_initial_centroids) """ return None