www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/VisualizingDataLearnMLExample.m

    %% Visualizing Data
% This example shows shows how to visualize data.
%% Overview
% You can use many MATLAB graph types for visualizing data patterns and
% trends. Scatter plots, described in this section, help to visualize
% relationships among the traffic data at different intersections. Data
% exploration tools let you query and interact with individual data points
% on graphs.
%%
% This section continues the data analysis from Summarizing Data.
%% 2-D Scatter Plots
% A two-dimensional scatter plot, created with the scatter function, shows
% the relationship between the traffic volume at the first two
% intersections:
%% Visualize - step 1

% Copyright 2015 The MathWorks, Inc.

load count.dat
c1 = count(:,1); % Data at intersection 1
c2 = count(:,2); % Data at intersection 2

figure
scatter(c1,c2,'filled')
xlabel('Intersection 1')
ylabel('Intersection 2')
%% Results - step 1
% The covariance, computed by the cov function measures the strength of the
% linear relationship between the two variables (how tightly the data lies
% along a least-squares line through the scatter):
%% Visualize - step 2
C12 = cov([c1 c2])
%% Results - step 2
% The results are displayed in a symmetric square matrix, with the
% covariance of the ith and jth variables in the (i, j)th position. The ith
% diagonal element is the variance of the ith variable.
%% 
% Covariances have the disadvantage of depending on the units used to
% measure the individual variables. You can divide a covariance by the
% standard deviations of the variables to normalize values between +1 and
% -1. The corrcoef function computes correlation coefficients:
%% Visualize - step 3a
R12 = corrcoef([c1 c2])
%% Visualize - step 3b
r12 = R12(1,2) % Correlation coefficient
%% Visualize - step 3c
r12sq = r12^2 % Coefficient of determination
%% Results - step 3 
% Because it is normalized, the value of the correlation coefficient is
% readily comparable to values for other pairs of intersections. Its
% square, the coefficient of determination, is the variance about the
% least-squares line divided by the variance about the mean. Thus, it is
% the proportion of variation in the response (in this case, the traffic
% volume at intersection 2) that is eliminated or statistically explained
% by a least-squares line through the scatter.
%% 3-D Scatter Plots
% A three-dimensional scatter plot, created with the scatter3 function,
% shows the relationship between the traffic volume at all three
% intersections. Use the variables c1, c2, and c3 that you created in the
% previous step:
%% Visualize - step 4
figure
c3 = count(:,3); % Data at intersection 3
scatter3(c1,c2,c3,'filled')
xlabel('Intersection 1')
ylabel('Intersection 2')
zlabel('Intersection 3')
%% Results - step 4
% Measure the strength of the linear relationship among the variables in
% the three-dimensional scatter by computing eigenvalues of the covariance
% matrix with the eig function:
%% Visualize - step 5a
vars = eig(cov([c1 c2 c3]))
%% Visualize - step 5b
explained = max(vars)/sum(vars)
%% Results - step 5
% The eigenvalues are the variances along the principal components of the
% data. The variable explained measures the proportion of variation
% explained by the first principal component, along the axis of the data.
% Unlike the coefficient of determination for two-dimensional scatters,
% this measure distinguishes predictor and response variables.
%% Scatter Plot Arrays
% Use the plotmatrix function to make comparisons of the relationships
% between multiple pairs of intersections:
%% Visualize - step 6
figure
plotmatrix(count)
%% Results - step 6
% The plot in the (i, j)th position of the array is a scatter with the ith
% variable on the vertical axis and the jth variable on the horizontal
% axis. The plot in the ith diagonal position is a histogram of the ith
% variable.
%% Exploring Data in Graphs
% Using your mouse, you can pick observations on almost any MATLAB graph
% with two tools from the figure toolbar:
%% 
% - Data Cursor 
%%
% - Data Brushing 
%%
% These tools each place you in exploratory modes in which you can select
% data points on graphs to identify their values and create workspace
% variables to contain specific observations. When you use data brushing,
% you can also copy, remove or replace the selected observations.
%%
% For example, make a scatter plot of the first and third columns of count:
%% Visualize - step 7
load count.dat
scatter(count(:,1),count(:,3))
%% Results - step 7
% Select the Data Cursor Tool and click the rightmost data point. A datatip
% displaying the point's x and y value is placed there.
%%
% Datatips display x-, y-, and z- (for three-dimensional plots) coordinates
% by default. You can drag a datatip from one data point to another to see
% new values or add additional datatips by right-clicking a datatip and
% using the context menu. You can also customize the text that datatips
% display using MATLAB code.
%%
% Data brushing is a related feature that lets you highlight one or more
% observations on a graph by clicking or dragging. To enter data brushing
% mode, click the left side of the Data Brushing tool on the figure
% toolbar. Clicking the arrow on the right side of the tool icon drops down
% a color palette for selecting the color with which to brush observations.
% This figure shows the same scatter plot as the previous figure, but with
% all observations beyond one standard deviation of the mean (as identified
% using the Tools > Data Statistics GUI) brushed in red.
%% Visualize - step 8
scatter(count(:,1),count(:,3))
%% Results - step 8
% After you brush data observations, you can perform the following operations on them:
%%
% - Delete them.
%%
% - Replace them with constant values.
%%
% - Replace them with NaN values.
%%
% - Drag or copy, and paste them to the Command Window.
%%
% - Save them as workspace variables.
%%
% For example, use the Data Brush context menu or the Tools > Brushing >
% Create new variable option to create a new variable called count13high.
%%
% Linked plots, or data linking, is a feature closely related to data
% brushing. A plot is said to be linked when it has a live connection to
% the workspace data it depicts. The copies of variables stored in a plot
% object's XData, YData, (and, where appropriate, ZData), automatically
% updated whenever the workspace variables to which they are linked change
% or are deleted. This causes the graphs on which they appear to update
% automatically.
%%
% Linking plots to variables lets you track specific observations through
% different presentations of them. When you brush data points in linked
% plots, brushing one graph highlights the same observations in every graph
% that is linked to the same variables.
%%
% Data linking establishes immediate, two-way communication between figures
% and workspace variables, in the same way that the Variable Editor
% communicates with workspace variables. You create links by activating the
% Data Linking tool on a figure's toolbar. Activating this tool causes the
% Linked Plot information bar, displayed in the next figure, to appear at
% the top of the plot (possibly obscuring its title). You can dismiss the
% bar (shown in the following figure) without unlinking the plot; it does
% not print and is not saved with the figure.
%%
% The following two graphs depict scatter plot displays of linked data
% after brushing some observations on the left graph. The common variable,
% count carries the brush marks to the right figure. Even though the right
% graph is not in data brushing mode, it displays brush marks because it is
% linked to its variables.
%% Visualize - step 9
figure
scatter(count(:,1),count(:,2))
xlabel ('count(:,1)')
ylabel ('count(:,2)')
figure
scatter(count(:,3),count(:,2))
xlabel ('count(:,3)')
ylabel ('count(:,2)')
%% Results - step 9
% The right plot shows that the brushed observations are more linearly
% related than in the left plot.
%%
% Brushed data observations appear highlighted in the brushing color when
% you display those variables in the Variable Editor, as you can see here:
%% Visualize - step 10
openvar count
%% Results - step 10
% In the Variable Editor, you can alter any values of linked plot data, and
% the graphs will reflect your edits. To brush data observation from the
% Variable Editor, click its Brushing Tool button. If the variable you
% brush is currently depicted in a linked plot, the observations you brush
% highlight in the plot, as well as in the Variable Editor. When you brush
% a variable that is a column in a matrix, the other columns in that row
% are also brushed. That is, you can brush individual observations in a row
% or column vector, but all columns in a matrix highlight in any row you
% brush, not just the observations you click.