Question: Machine learning and numpy in python excersize Age Income NofCard Class George 35 35 3 No Rachel 22 50 2 Yes Steve 63 200 1

Machine learning and numpy in python excersize
Age Income NofCard Class George 35 35 3 No Rachel 22 50 2 Yes Steve 63 200 1 No Tom 59 170 1 No Anna 25 40 4 Yes Lab Exercise 1) Convert the df to numpy array and call it arr, 2) Apply array slicing to separate x from y. When creating X make sure to set dtype=int or float. You should get the following: arr 3], 2], array([[35, 35, 3, 'No'], [22, 50, 2, 'Yes'], [63, 200, 1, 'No'], [59, 170, 1, 'No'], [25, 40, 4, 'Yes']], dtype=object) array([[ 35, 35, [ 22, 50, [ 63, 200, [ 59, 170, [ 25, 40, 1], 1], 4]]) array(['No', 'Yes', 'No', 'No', 'Yes'], dtype=object) 3) Create the case of John as following |x=[[37,50,2]] #note we use small letter of x 4) Now we need to calculate the distance between John and the rest, let's use the Euclidean distance. Distance functions Euclidean i=1 a. Based on the concept of broadcasting in numpy, subtract x from X and call the result diff b. Calculate the square of diff using np.square(). c. Calculate the sum of each row using np.sum()(note which axis you should use) and call it diffSquare Sum. d. Calculate the square root of diffSquare Sum using np.sqrt() and call it sort diffSquareSum At the end of this step you should have the distance between John and the rest as follows: print(sqrt_diffSquareSum) [ 15.16575089 15. 152.2399422 122.00409829 15.74801575] 5) Now, you need to find the nearest neighbor (the INN) i.e. the closest person to John's case. Use the np.min() to find the minimum distance (minimum value of sort diffSquare Sum) and call it min_dist. This should be 15. 6) To find the index of the nearest neighbor use the function np.argmin(sqrt diffSquaresum) and save the result in a vaiable called min indx. The function will return the index of the minimum value in sort_diffSquare Sum (i.e. "1"). 7) The last step is to find the label or "y" value of the closest person. To find that, you have use the min indx. Print the value of y at index min indx and that should be the output of your model. Congratulation, you have implemented your first Machine Learning Algorithm! 8) Define a function that takes three arrays, that is x, y, and x. The function should predict x based on X and y. (Note: you just need to copy paste your codes into the function body). Note x should be the test data so you may change its value and test your function. def nearest(X.X.X) Example of use: nearest(X,Y,x) 'Yes' xt=[[50, 200,1]] nearest(X,y,xt) "No
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
