Professional Documents
Culture Documents
Lecture :
by
TANGERANG
2017
1
Table of Contents
2
Import data from Spreadsheet
% DESCRIPTIVE TEXTImport data from spreadsheet
% Script for importing data from the following spreadsheet:
%
% Workbook: D:\02. S1 Industrial Engineering SGU\02. Engineering
% Statistic\MATLAB\Receipts and Outlays.xlsx Worksheet: Sheet1
%
% To extend the code for use with different selected data or a different
% spreadsheet, generate a function instead of a script.
3
I. Visualization – How can I see what my data show ?
1. Bar
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1);
box(axes1,'on');
hold(axes1,'all');
% Create bar
bar(Year,Outlays);
% Create xlabel
xlabel('Tahun');
% Create ylabel
ylabel('Outlays %');
% Create title
title('Grafik Outlays');
4
% Melalui grafik batang, kita dapat melihat data ‘Outlays’ secara individual.
Dari grafik di atas terlihat rentang data tahun 1944-1946, US memiliki “%
Outlays” yang tinggi dibandingkan tahun-tahun lainnya.
%CREATEFIGURE(XVECTOR1, YVECTOR1)
% XVECTOR1: bar xvector
% YVECTOR1: bar yvector
% Create figure
figure2 = figure;
% Create axes
axes2 = axes('Parent',figure2);
box(axes2,'on');
hold(axes2,'all');
% Create bar
bar(Year,Receipts);
% Create xlabel
xlabel('Tahun');
% Create ylabel
ylabel('Receipts %');
% Create title
title('Grafik Receipts');
5
% Melalui grafik batang, kita dapat melihat data ‘Receipts’ secara
individual. Dari grafik di atas terlihat rentang data tahun 1940-1944, US
memiliki “% Receipts” yang rendah dibandingkan tahun-tahun lainnya.
2. Hist
%CREATEFIGURE(VERTEXNORMALS1, YDATA1, XDATA1, VERTICES1, FACES1,
FACEVERTEXCDATA1, CDATA1)
% VERTEXNORMALS1: patch vertexnormals
% YDATA1: patch ydata
% XDATA1: patch xdata
% VERTICES1: patch vertices
% FACES1: patch faces
% FACEVERTEXCDATA1: patch facevertexcdata
% CDATA1: patch cdata
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1,'CLim',[1 2]);
box(axes1,'on');
% Create patch
patch('Parent',axes1,'VertexNormals',VertexNormals1,'YData',YData1,...
'XData',XData1,...
'Vertices',Vertices1,...
'Faces',Faces1,...
6
'FaceColor','flat',...
'FaceVertexCData',FaceVertexCData1,...
'DisplayName','Outlays',...
'CData',CData1);
% Create title
title('Histogram Outlays');
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1,'CLim',[1 2]);
box(axes1,'on');
7
% Create patch
patch('Parent',axes1,'VertexNormals',VertexNormals1,'YData',YData1,...
'XData',XData1,...
'Vertices',Vertices1,...
'Faces',Faces1,...
'FaceColor','flat',...
'FaceVertexCData',FaceVertexCData1,...
'DisplayName','Receipts',...
'CData',CData1);
% Create title
title('Histogram Receipts');
3. Boxplot
XData3, YData5, XData4, YData6, YData7, YData8, XData5, YData9, XData6,
YData10, XData7, YData11, XData8, YData12, XData9, YData13, XData10,
YData14)
%CREATEFIGURE1(XDATA1, YDATA1, XDATA2, YDATA2, YDATA3, YDATA4, XDATA3,
YDATA5, XDATA4, YDATA6, YDATA7, YDATA8, XDATA5, YDATA9, XDATA6, YDATA10,
XDATA7, YDATA11, XDATA8, YDATA12, XDATA9, YDATA13, XDATA10, YDATA14)
% XDATA1: line xdata
% YDATA1: line ydata
% XDATA2: line xdata
% YDATA2: line ydata
% YDATA3: line ydata
% YDATA4: line ydata
8
% XDATA3: line xdata
% YDATA5: line ydata
% XDATA4: line xdata
% YDATA6: line ydata
% YDATA7: line ydata
% YDATA8: line ydata
% XDATA5: line xdata
% YDATA9: line ydata
% XDATA6: line xdata
% YDATA10: line ydata
% XDATA7: line xdata
% YDATA11: line ydata
% XDATA8: line xdata
% YDATA12: line ydata
% XDATA9: line xdata
% YDATA13: line ydata
% XDATA10: line xdata
% YDATA14: line ydata
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1,'XTickLabel','','XTick',zeros(1,0));
%% Uncomment the following line to preserve the X-limits of the axes
% xlim(axes1,[0.5 2.5]);
%% Uncomment the following line to preserve the Y-limits of the axes
% ylim(axes1,[4.96 45.44]);
box(axes1,'on');
% Create hggroup
hggroup1 = hggroup('Parent',axes1);
% Create line
line(XData1,YData1,'Parent',hggroup1,'Tag','Upper Whisker','LineStyle','--
');
% Create line
line(XData2,YData2,'Parent',hggroup1,'Tag','Upper Whisker','LineStyle','--
');
% Create line
line(XData1,YData3,'Parent',hggroup1,'Tag','Lower Whisker','LineStyle','--
');
% Create line
line(XData2,YData4,'Parent',hggroup1,'Tag','Lower Whisker','LineStyle','--
');
% Create line
line(XData3,YData5,'Parent',hggroup1,'Tag','Upper Adjacent Value');
% Create line
line(XData4,YData6,'Parent',hggroup1,'Tag','Upper Adjacent Value');
% Create line
line(XData3,YData7,'Parent',hggroup1,'Tag','Lower Adjacent Value');
9
% Create line
line(XData4,YData8,'Parent',hggroup1,'Tag','Lower Adjacent Value');
% Create line
line(XData5,YData9,'Parent',hggroup1,'Tag','Box','Color',[0 0 1]);
% Create line
line(XData6,YData10,'Parent',hggroup1,'Tag','Box','Color',[0 0 1]);
% Create line
line(XData7,YData11,'Parent',hggroup1,'Tag','Median','Color',[1 0 0]);
% Create line
line(XData8,YData12,'Parent',hggroup1,'Tag','Median','Color',[1 0 0]);
% Create line
line(XData9,YData13,'Parent',hggroup1,'Tag','Outliers',...
'MarkerEdgeColor',[1 0 0],...
'Marker','+',...
'LineStyle','none',...
'Color',[0 0 1]);
% Create line
line(XData10,YData14,'Parent',hggroup1,'Tag','Outliers',...
'MarkerEdgeColor',[1 0 0],...
'Marker','+',...
'LineStyle','none',...
'Color',[0 0 1]);
% Create text
text('Parent',hggroup1,'VerticalAlignment','bottom','Interpreter','none',..
.
'Units','points',...
'String','1',...
'Position',[81.375 -13.5 0],...
'HorizontalAlignment','center');
% Create text
text('Parent',hggroup1,'VerticalAlignment','bottom','Interpreter','none',..
.
'Units','points',...
'String','2',...
'Position',[244.125 -13.5 0],...
'HorizontalAlignment','center');
% Create title
title('Outlays-Receipts Boxplot');
10
% Melalui grafik boxplot, kita dapat melihat outliers dari data ‘Outlays’
dan data ‘Receipts’. Outlier untuk data Outlays ada 6 data. Nilai maksimum
data 43.6, minimum 9.8, jumlah titik 76, lower adjacent 14.2, upper adjacent
25.4 sedangkan outlier untuk data Receipts ada 11 data. Nilai maksimum data
20.9, minimum 6.8, jumlah titik 76, lower adjacent 16.1, upper adjacent 19.9.
1. Dfittool Outlays
11
12
function pd1 = createFit(Outlays)
%CREATEFIT Create plot of datasets and fits
% PD1 = CREATEFIT(OUTLAYS)
% Creates a plot, similar to the plot in the main distribution fitting
% window, using the data that you provide as input. You can
% apply this function to the same data you used with dfittool
% or with different data. You may want to edit the function to
% customize the code and this help message.
%
% Number of datasets: 1
% Number of fits: 1
%
% See also FITDIST.
13
Outlays = Outlays(:);
% Prepare figure
clf;
hold on;
LegHandles = []; LegText = {};
% Adjust figure
box on;
hold off;
14
function pd1 = createFit(Outlays)
%CREATEFIT Create plot of datasets and fits
% PD1 = CREATEFIT(OUTLAYS)
% Creates a plot, similar to the plot in the main distribution fitting
% window, using the data that you provide as input. You can
% apply this function to the same data you used with dfittool
% or with different data. You may want to edit the function to
% customize the code and this help message.
%
% Number of datasets: 1
% Number of fits: 1
%
% See also FITDIST.
% Prepare figure
clf;
hold on;
LegHandles = []; LegText = {};
15
hLine = stairs(CdfX,CdfY,'Color',[0.333333 0 0.666667],'LineStyle','-',
'LineWidth',1);
xlabel('Data');
ylabel('Cumulative probability')
LegHandles(end+1) = hLine;
LegText{end+1} = 'Outlays data';
% Adjust figure
box on;
hold off;
Comment :
Distribution: Normal
Log likelihood: -235.983
Domain: -Inf < y < Inf
Mean: 20.7079
Variance: 29.5285
2. Dfittool Receipts
16
17
function pd1 = createFit(Receipts)
%CREATEFIT Create plot of datasets and fits
% PD1 = CREATEFIT(RECEIPTS)
% Creates a plot, similar to the plot in the main distribution fitting
% window, using the data that you provide as input. You can
% apply this function to the same data you used with dfittool
% or with different data. You may want to edit the function to
% customize the code and this help message.
%
% Number of datasets: 1
% Number of fits: 1
%
% See also FITDIST.
% Prepare figure
clf;
hold on;
LegHandles = []; LegText = {};
18
[CdfF,CdfX] = ecdf(Receipts,'Function','cdf'); % compute empirical cdf
BinInfo.rule = 1;
[~,BinEdge] = internal.stats.histbins(Receipts,[],[],BinInfo,CdfF,CdfX);
[BinHeight,BinCenter] = ecdfhist(CdfF,CdfX,'edges',BinEdge);
hLine = bar(BinCenter,BinHeight,'hist');
set(hLine,'FaceColor','none','EdgeColor',[0.333333 0 0.666667],...
'LineStyle','-', 'LineWidth',1);
xlabel('Data');
ylabel('Density')
LegHandles(end+1) = hLine;
LegText{end+1} = 'Receipts data';
% Adjust figure
box on;
hold off;
19
20
function pd1 = createFit(Receipts)
%CREATEFIT Create plot of datasets and fits
% PD1 = CREATEFIT(RECEIPTS)
% Creates a plot, similar to the plot in the main distribution fitting
% window, using the data that you provide as input. You can
% apply this function to the same data you used with dfittool
% or with different data. You may want to edit the function to
% customize the code and this help message.
%
% Number of datasets: 1
% Number of fits: 1
%
% See also FITDIST.
% Prepare figure
clf;
hold on;
LegHandles = []; LegText = {};
21
[CdfY,CdfX] = ecdf(Receipts,'Function','cdf'); % compute empirical
function
hLine = stairs(CdfX,CdfY,'Color',[0.333333 0 0.666667],'LineStyle','-',
'LineWidth',1);
xlabel('Data');
ylabel('Cumulative probability')
LegHandles(end+1) = hLine;
LegText{end+1} = 'Receipts data';
% Adjust figure
box on;
hold off;
Distribution: Normal
Mean: 17.4211
Variance: 5.52782
mu 17.4211 0.269693
22
Estimated covariance of parameter estimates:
mu sigma
mu 0.0727344 -1.92503e-16
measures ?
ans =
20
>> mean(Outlays)
ans =
20.7079
>> std(Outlays)
ans =
5.43440
>> mode(Outlays)
ans =
18.8000
>> skewness(Outlays)
ans =
2.5078
>> kurtosis(Outlays)
ans =
12.1827
% skewness positif menunjukkan mean dan median di kanan modus dan jika dibuat
grafik akan cenderung ke kanan. Kurtosis yang lebih besar dari 3
(leptokurtik) menunjukkan distribusi dengan puncak relative tinggi.
23
2. median, mean, std, mode, skewness, kurtosis Receipts
>> median(Receipts)
ans =
17.7500
>> mean(Receipts)
ans =
17.4211
>> std(Receipts)
ans =
2.3511
>> mode(Receipts)
ans =
17.5000
>> skewness(Receipts)
ans =
-2.5823
>> kurtosis(Receipts)
ans =
11.4908
% skewness negatif menunjukkan mean di kiri modus, jika dibuat grafik maka
akan cenderung ke kiri. Kurtosis yang lebih besar dari 3 (leptokurtik)
menunjukkan distribusi dengan puncak relatif tinggi.
data ?
%CREATEFIT1(RECEIPTS,OUTLAYS,SURPLUSORDEFICIT)
% Create a fit.
24
%
% Data for 'untitled fit 1' fit:
% X Input : Receipts
% Y Input : Outlays
% Z Output: SurplusorDeficit
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
Hasil nya :
Linear interpolant:
Coefficients:
p = coefficient structure
Goodness of fit:
25
SSE: 2.958e-31
R-square: 1
RMSE: NaN
26
V. Machine Learning : Classification
>> figure,plot(Outlays,Receipts,'k*','MarkerSize',5)
>> [i,centroid]=kmeans(US,2);
>> gscatter(Outlays,Receipts,kmeans(US,2))
>> hold on
>> plot(centroid(1,1),centroid(1,2),'bo','MarkerSize',12)
>> plot(centroid(2,1),centroid(2,2),'ro','MarkerSize',12)
>> disp(i(knnsearch(US,[11,9])))
>> disp(i(knnsearch(US,[45,50])))
27
VI. Machine Learning : Curve Fitting
%% DESCRIPTIVE TEXTImport data from spreadsheet
% Script for importing data from the following spreadsheet:
%
% Workbook: D:\02. S1 Industrial Engineering SGU\02. Engineering
% Statistic\MATLAB\Receipts and Outlays.xlsx Worksheet: Sheet1
%
% To extend the code for use with different selected data or a different
% spreadsheet, generate a function instead of a script.
inputs = Receipts';
targets = Outlays';
28
net.divideParam.testRatio = 15/100;
% Plots
% Uncomment these lines to enable various plots.
figure, plotperform(tr)
figure, plottrainstate(tr)
figure, plotfit(net,inputs,targets)
figure, plotregression(targets,outputs)
figure, ploterrhist(errors)
performance =
14.1635
trainPerformance =
7.9688
valPerformance =
4.7740
testPerformance =
29
53.9636
30
31
32
33
34