Clean Messy Data and Locate Extrema Using Live Editor Tasks
You can interactively preprocess data using sequences of Live Editor tasks, visualizing the data at each step. This example uses five tasks to clean noisy data with missing values and outliers in order to identify local minima and maxima. For more information on Live Editor tasks, seeAdd Interactive Tasks to a Live Script.
First, create and plot a vector of messy data, which contains fourNaN
values and five outliers.
x = 1:100; data = cos(2*pi*0.05*x+2*pi*rand) + 0.5*randn(1,100); data(20:20:80) = NaN; data(10:20:90) = [-50 40 30 -45 35];
情节混乱的数据,打开创建图task. Start by typing the keywordplot
in a code block, and then click创建图
当它出现在菜单中时。选择绘图类型和输入数据以绘制数据。
To see the code that this task generates, expand the task display by clickingat the bottom of the task parameter area.
%创建X和数据的图h =图(x,数据,"DisplayName","data");%添加Xlabel,Ylabel,Title和LegendXlabel("x")ylabel("data") title(“ X与数据”传说)
Fill Missing Data
To replaceNaN
数据中的值并可视化结果,打开Clean Missing Datatask. Start by typing the keywordmissing
in a code block, and then clickClean Missing Data
当它出现在菜单中时。选择the input data and the cleaning method to plot the filled data automatically.
To see the code that this task generates, expand the task display by clickingat the bottom of the task parameter area.
%填写缺少的数据[cleanedData,missingIndices] = fillmissing(data,"linear");%显示结果clf plot(cleanedData,"Color",[0 114 189]/255,“行宽”,1.5,..."DisplayName","Cleaned data") 抓住上填充丢失的条目的百分比plot(find(missingIndices),cleanedData(missingIndices),".","MarkerSize",12,..."Color",[217 83 25]/255,"DisplayName","Filled missing entries") title(“填充缺失的条目数量:”+ nnz(missingIndices)) holdoff传说
清除缺少
填写异常值
现在,您可以通过使用的Clean Outlier Datatask. Type the keyword异常值
in a new code block and clickClean Outlier Data
打开任务。选择清洁
as the input data. You can customize the methods for cleaning and detecting outliers and adjust the threshold to find more or fewer outliers.
To see the code that this task generates, expand the task display by clickingat the bottom of the task parameter area.
% Fill outliers[cleanedData2,outlierIndices,thresholdLow,thresholdHigh] =...filleoutliers(CleanData,"linear");%显示结果clf plot(cleanedData,"Color",[77 190 238]/255,"DisplayName","Input data") 抓住上plot(cleanedData2,"Color",[0 114 189]/255,“行宽”,1.5,..."DisplayName","Cleaned data")% Plot outliersplot(find(outlierIndices),cleanedData(outlierIndices),"x",..."Color",[64 64 64]/255,"DisplayName","Outliers") title("Number of outliers cleaned: "+ nnz(离群值))% Plot filled outliers绘图(find(outlierIndices),清洁data2(utlierIndices),".","MarkerSize",12,..."Color",[217 83 25]/255,"DisplayName","Filled outliers")% Plot outlier thresholdsplot([xlim missing xlim],[thresholdLow*[1 1] NaN thresholdHigh*[1 1]],..."Color",[145 145 145]/255,"DisplayName","Outlier thresholds") 抓住off传说
清除outlierIndicesthresholdLowthresholdHigh
流畅的数据
Next, smooth the cleaned data from the previous task by using the流畅的数据task. Type the keyword光滑的
and click the task when it appears. Select清洁2
,作为输入数据的上一个任务的输出。选择一种平滑方法,然后调整平滑因子或多或少的平滑因子。
To see the code that this task generates, expand the task display by clickingat the bottom of the task parameter area.
% Smooth input data光滑的edData = smoothdata(cleanedData2,"gaussian","SmoothingFactor",0.4);%显示结果clf plot(cleanedData2,"Color",[77 190 238]/255,"DisplayName","Input data") 抓住上plot(smoothedData,"Color",[0 114 189]/255,“行宽”,1.5,..."DisplayName","Smoothed data") 抓住off传说
Locate Extrema
Finally, start typing the keywordextrema
and click找到当地的极端
. Use光滑的edData
随着输入数据并更改极值类型,以找到清洁,平滑数据的局部最大值和本地最小值。您可以调整本地极端参数,以找到更多或更少的最大值和最小值。
To see the code that this task generates, expand the task display by clickingat the bottom of the task parameter area.
%找到本地最大值和最小值maxindices = islocalmax(SmoothedData);minindices = islocalmin(SmoothedData);%显示结果clf plot(smoothedData,"Color",[77 190 238]/255,"DisplayName","Input data") 抓住上% Plot local maximaplot(find(maxIndices),smoothedData(maxIndices),"^","Color",[217 83 25]/255,..."MarkerFaceColor",[217 83 25]/255,"DisplayName","Local maxima")%绘制本地最小值plot(find(minIndices),smoothedData(minIndices),"v","Color",[237 177 32]/255,..."MarkerFaceColor",[237 177 32]/255,"DisplayName",“本地最小值”) title("Number of extrema: "+ (nnz(maxIndices)+nnz(minIndices))) holdoff传说
See Also
Live Editor Tasks
Functions
ismissing
|rmmissing
|fillmissing
|isoutlier
|filloutliers
|rmoutliers
|ischange
|islocalmin
|islocalmax
|光滑的data