Visualize the data, display it on a 2-dimensional plot by calling the function plotData to display a
Question:
Visualize the data, display it on a 2-dimensional plot by calling the function plotData to display a 1, where the axes are the two exam scores, and the positive and negative examples are shown with different markers. = 1) examples and o indicating (y = 0) % Plot the data with + indicating (y examples. plotData(x, y); % Labels and Legend xlabel('Exam 1 score') ylabel('Exam 2 score') % Specified in plot order legend('Admitted', 'Not admitted') function plotData(x, y) %PLOTDATA Plots the data points X and y into a new figure % PLOTDATA(x,y) plots the data points with + for the positive examples % and o for the negative examples. X is assumed to be a Mx2 matrix. % Create New Figure; hold on; % Instructions: Plot the positive and negative examples on a % 2D plot, using the option 'k+' for the positive % examples and 'ko' for the negative examples. % % Find Indices of Positive and negative Examples pos = find(y==1); neg = find(y == 0); % Plot Examples plot(X(pos, 1), X(pos, 2), 'k+', 'LineWidth', 2, 'MarkerSize', 7); plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7); hold off; end
Sol64:
The code is written in MATLAB/Octave and consists of two functions: plotData and main. The main function loads the data from a text file, calls the plotData function to display the data, and then sets the labels and legend for the plot.
The plotData function takes two arguments, x and y, which represent the feature values and labels, respectively. The function first finds the indices of the positive and negative examples based on the label values. It then creates a new figure and uses the plot function to display the positive examples as black crosses ('k+') and negative examples as yellow circles ('ko') with a black border.
Finally, the hold off command is used to stop adding elements to the current figure, and the function ends.