You are on page 1of 10

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/317313022

MATLAB Cheat Sheet for Data Science - London School of Economics

Technical Report · May 2017


DOI: 10.13140/RG.2.2.16859.77607

CITATIONS READS

0 11,772

2 authors:

Ali Habibnia Eghbal Rahimikia


Virginia Polytechnic Institute and State University The University of Manchester
13 PUBLICATIONS   10 CITATIONS    3 PUBLICATIONS   5 CITATIONS   

SEE PROFILE SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Foreign Exchange Rate Risk Measurement and Managemen View project

Deep Learning in Finance View project

All content following this page was uploaded by Eghbal Rahimikia on 02 June 2017.

The user has requested enhancement of the downloaded file.


MATLAB Cheat Sheet for Data Science map container Map values to unique keys (dictionary). Create basic variables
x=5 Define variable x to be 5.
Data import/export x=nan Define variable x to be Not-a-Number.
j:k Row vector from j to k (step size: 1).
j:i:k Row vector from j to k (step size: i).
xlsread/xlswrite Read/write Excel spreadsheet. linspace(a,b,n) n numbers linearly spaced between a and b.
load/save Load/save MATLAB variables. logspace(a,b,n) n numbers logarithmically spaced between a and b.
Ali Habibnia Eghbal Rahimikia load/save -ascii Load/save text files (.txt, .csv). NaN(a,b) a × b matrix of NaN values.
dlmread/dlmwrite Read/write ASCII-delimited file. ones(a,b) a × b matrix of 1 values.
Last update: May, 2017 readtable/writetable Create/write table from file. zeros(a,b) a × b matrix of 0 values.
MATLAB r2017a fscanf/fprintf Read/write data from/to text file. eye(a) Identity matrix of size a.
textscan Read formatted data from text file. sparse(a,b) a × b sparse matrix.
Preliminaries fgetl Read line from file, removing rand(a,b) Uniform a × b random numbers in [0,1).
newline characters. randi(imax,a,b) Uniform a × b random integers in [1,imax].
fgets Read line from file, keeping randn(a,b) Gaussian a × b random numbers.
newline characters. randperm(a) Integer random permutation in [1,a].
fuc : This function or command requires a toolbox to execute. fread/fwrite Read/write from/to binary file. diag(x) Square matrix (vector x: diagonal elements).
fopen/fclose Open/close file.
importdata Load data from file.
Ways to get help readall Read data from data-store. Basic math functions
imread/imwrite Read/write image file.
doc Display documentation. save filename Save all variables to .mat file. abs(x) Absolute value of x.
doc command Display documentation for function. save filename x,y Save x,y variables to .mat file. sqrt(x) Square root of x.
help Display documentation in command window. load filename Load all variables from .mat file. sign(x) Sign of x.
help command Display help text in command window. webread/webwrite (URL) Read/write content from/to URL. round(x) Round of x.
lookfor (X) Search all M-files for X. websave (URL) Save data from URL to file. ceil(x) Round x toward positive infinity.
docsearch (X) Search documentation for X. fix(x) Round x toward zero.
demo Access demonstration examples. floor(x) Round x toward negative infinity.
which command Locate functions. Basic commands complex(a,b) Complex array (z = a + bi).
real(x) Real part of complex number.
image(x) Imaginary part of complex number.
clc Clear command window.
File extensions clear Clear workspace.
conj(x) Complex conjugate of x.
log(x) Natural logarithm of x.
clear (X) Clear (X) from memory. log10(x) Common logarithm of x.
.m A MATLAB script, function, or class. close (X) Close figure (X).
.mat A MATLAB data, stores workspace. exp(x) Exponential of x (ex ).
close all Close all figures. rem(a,b) Remainder after division of a by b.
.fig MATLAB figure or GUI template. ... Continue entering statement.
.p MATLAB protected function file. mod(a,b) Remainder after division of a by b (modulo operation).
clf Clear current figure. lcm(a,b) Least common multiples of a and b.
.mlx MATLAB live script. whos (X) Size, bytes, class, and attributes of (X).
.mex MATLAB executable. gcd(a,b) Greatest common multiples of a and b.
ver List MATLAB version and toolboxes. nthroot(a,n) Real n-th root of a.
.mlapp MATLAB App Designer template. dir List current folder contents.
.mdl .slx Simulink model. tic/toc Start/stop stopwatch timer.
.mdlp .slxp Simulink protected model.
.mlappinstall MATLAB app installer file.
beep Produce system beep sound. Trigonometric functions
ans Last answer.
.mltbx MATLAB toolbox file. pwd Current directory.
#: sin, cos, tan, sec, or cot. sine, cosine, tangent, secant, or cotangent.
path View/change search directory.
#/#d(x) # of x in radians/degrees.
pathtool Open set path window.
Common data types mkdir Make new directory.
#h(x) Hyperbolic # of x.
a#/a#d(x) Inverse # of x in radians/degrees.
cd Change current directory.
a#h(x) Inverse hyperbolic # of x.
single Single precision numerical data (32 bits). what List of MATLAB files in folder.
atan2/atan2d(x) Four-quadrant inverse tan of x in
double Double precision numerical data (64 bits). which Find directory of functions.
radians/degrees.
char Character array. lasterr Last error message.
hypot(x) Square root of sum of squares of x.
string String array. lastwarn Last warning message.
deg2rad(x) Convert x from degrees to radians.
logical True (1) or false (0). rehash Refresh caches.
rad2deg(x) Convert x from radians to degrees.
struct Structure array. home Send cursor home.
cell Cell array. exit Close MATLAB.

MATLAB Cheat Sheet for Data Science - London School of Economics. 1 of 9


Linear algebra x([i,j]) i-th and j-th elements. char(x) Create character from numeric array.
strfind(x1,ptrn) Search x1 char/str for ptrn pattern.
x=[1,2,3] 1 × 3 vector (double array). If A is a matrix: strjoin(x) Construct char array using x char/str
x=[1;2;3] 3 × 1 vector. A(i,j) * Element i,j of A (i, j, l, m ≥ 1). elements.
x=[1,2;3,4] 2 × 2 matrix (double array). A(i) * Element i-th of A. lower(x) Convert char/str to lowercase.
x=[1,2;3,4;5,6] 3 × 2 matrix. A(i:j,l:m) * Elements in rows from i to j which are upper(x) Convert char/str to uppercase.
x={1,‘st’} 1 × 2 cell array. in columns from l to m. strcmp(x1,x2) Compare char/str of x1 and x2.
sx.x1=[1,2,3] 1 × 3 vector stored in sx structure array. A([a,b],[c,d]) * a-th and b-th elements in rows and c-th strcmpi(x1,x2) Compare char/str of x1 and x2 (case
sx.x2={1,‘st’} 1 × 2 cell stored in sx structure array. and d-th elements in columns. insensitive).
x*y Matrix multiplication. A(:,i) * i-th column elements. split(x,dl) Split strings in string array (x) at dl
x+y Element by element addition. A(:,[a,b]) a-th and b-th columns elements. delimiters.
x-y Element by element subtraction. A(i,:) * i-th row elements. strsplit(x,dl) Split x char/str at dl delimiters.
x.*y Element by element multiplication. A([i,j],:) * i-th and j-th rows elements. sprintf(‘fmt’,x1,x2) Format data based on fmt structure.
x./y Element by element division. A>i Logical array of elements higher than i. strvcat(x1,x2) Vertically concatenate x1 and x2
A^n Normal matrix power of A. find(A>i) Indices of elements higher than i. (ignore spaces).
A.^n Element-wise power of A. find(A==i) Indices of elements equal to i.
A’ Transpose of A. diag(A) Elements in the principal diagonal of A. Regular expression
inv(A) Inverse. tril(A) Lower triangular part of A.
size(A) Size (rows and columns). triu(A) Upper triangular part of A. regexp/regexpi(str,exp) Search exp pattern in str char/
numel(A) Number of elements. A(i,j)=a Replace element i,j of A with a. str (case sensitive/insensitive).
min(A) Smallest elements. A(:,i)=[a;b] Replace i-th column of A with [a;b]. regexprep(str,exp,rpc) Replace str which matches exp with
cummin(A) Cumulative minimum of array elements. A(i,:)=[] * Delete i-th row of A. the rpc.
max(A) Largest elements. A([i,j],:)=[] * Delete i-th and j-th rows of A. regexptranslate(type,str) Translate str to regular expression
cummax(A) Cumulative maximum of array elements. A(A>m)=v Replace all elements over m with v. by type as type of translation.
sum(A) Sum of array elements. A(A==m)=v Replace all elements is equal to m with v.
Cumulative sum of array elements. arrayfun(func,A) Apply a function to each element of A.
cumsum(A)
bsxfun(func,A,B) Apply an element-wise binary operation ‘IS*’ functions
mean(A) Average of elements.
median(A) Median of elements. specified by func to A and B.
Return true where:
mode(A) Mode of elements. isnan(X) Elements of X which are NaN.
prod(A) Product of array elements. If A is a cell:
isnumeric(X) Elements of X which are numeric.
cumprod(A) Cumulative product of array elements. Above matrix operations which are marked
isinf(X) Elements of X which are infinite.
diff(x,k) Successive k-differences of x. with asterisk(*). Output is cell array of
isinteger(X) Elements of X which are integer.
std(A) Standard deviation. elements.
isfloat(X) Elements of X which are floating-point.
var(A) Variance. A{i,j} Element i,j of A (i, j, l, m ≥ 1).
isbetween(X,a,b) Elements of X which are between a and b
cov(A) Covariance. A(i,j)={a} Replace element i,j of A with cell {a}.
(date and time).
corrcoef(A) Correlation coefficients (columns: random A(:,i)={a;b} Replace i-th column of A with cell {a;b}.
ismember(X,B) Elements of X which are found in B.
variables, rows: observations). cellfun(func,A) Apply a function to each cell in A.
ismissing(X,B) Elements of X which are missing.
eig(A) Eigenvalues and eigenvectors.
svd(A) Singular values. Return true if:
norm(A) Norms. Character and string isvector(X) X is a vector.
sort(A) Sorts vector from smallest to largest. ismatrix(X) X is a logical array.
sortrows(A) Sorts rows of A in ascending order. st: string. isstring(X) X is a string (char).
rank(A) Rank. char: character. iscell(X) X is a cell array.
chol(A) Cholesky factorization of matrix. iscellstr(X) X is a cell array of strings.
det(A) Determinant of square matrix. x=‘text’ Define variable x to be ‘text’ (char). isstruct(X) X is a structure.
factor(A) Prime factors. x(i) i-th part of char. istable(X) X is a table.
perm(A) Permutations of the elements. x(i:j) i-th to j-th part of char. islogical(X) X is a logical array.
x=string(‘text’) Define variable x to be ‘text’ (str). isscalar(X) X is a scalar (size=[1,1]).
x={‘s1’,‘s2’} Define more than one char. isreal(X) There isn’t imaginary value in X.
Accessing/assignment elements x=[string(‘st1’)... isrow(X) X is a row vector.
,string(‘st2’)] Define more than one str. iscolumn(X) X is a column vector.
If x is a vector: x{i,j} Element i,j of x (char). isdiag(X) X is a lower diagonal matrix.
x(i) Element i-th of x (i, j ≥ 1). x(i,j) Element i,j of x (str). istril(X) X is a lower triangular matrix.
x(i:j) Elements from the i-th to the j-th of x. x{i}(j) i-th part of j-th char/str. istriu(X) X is a upper triangular matrix.
x(i:end) Elements from the i-th to the last one of x. x{i:m}(j) i to m-th part of j-th char/str. isdir(X) X is directory (folder).
x(:) All the elements of x. strcat(x1,x2) Concatenate characters/strings.

MATLAB Cheat Sheet for Data Science - London School of Economics. 2 of 9


‘IS*’ functions... % This is a comment line. while (Condition)
x=2; %This is a comment. MATLAB Commands
isequal(X,B) X is equal to B. y=3; end
isequaln(X,B) X is equal to B (NaN values are equal).
issorted(X) X elements are in ascending order.
For loop statement: Loop from a to b in steps of s in the
isvarname(X) X is a valid MATLAB variable name.
User-defined functions variable i.

for i = a:s:b
Convert functions Function structure: in1 and in2 are function inputs and MATLAB Commands
out1 and out2 are function outputs. end
num2str(x) Convert numeric array (x) to char array.
num2cell(x) Convert numeric array (x) to cell array. function [out1,out2] = fun_name(in1,in2)
num2int(x) Convert numeric array (x) to signed integer. Break: break terminates the execution of for or while loop.
... Code lines after the break do not execute. In nested loops,
num2hex(x) Convert numeric array (x) to IEEE end
hexadecimal string. break exits from the loop in which it mentions.
str2num(x) Convert char array (x) to numeric array.
str2mat(x) Convert char/str array (x) to matrix. Continue: continue passes control to the next iteration of for
str2double(x) Convert char/str array (x) to double Anonymous function structure: @ operator creates a or while loop. In nested loops, continue passes the iteration in
precision. function handle. which it mentions.
str2func(x) Convert char array (x) to function handle.
cell2mat(x) Convert cell array (x) to matrix. f = @(x)(x.^2+exp(x))
cell2table(x) Convert cell array (x) to table. % i.e. f(2) returns 11.3891. Errors
cell2struct(x) Convert cell array (x) to structure array.
cellstr(x) Convert array x to cell array. Common errors
mat2str(x) Convert matrix (x) to char array. Return: return forces MATLAB to return control to the
mat2cell(x) Convert matrix (x) to cell array. invoking function before reaching to the end of that function. Error using *: inner matrix dimensions must agree.
table2cell(x) Convert table (x) to cell array. The * operator performs matrix multiplication, where an NxM
table2array(x) Convert table (x) to homogeneous array. matrix is multiplied by an MxP matrix, resulting in an NxP
table2struct(x) Convert table (x) to structure array. Flow control matrix. Use .* instead of * to perform the element-wise
struct2cell(x) Convert structure array (x) to cell array. multiplication.
struct2table(x) Convert structure array (x) to table array.
If statement: An if statement can be followed by an (or
int2str(x) Convert integer (x) to char array. Index exceeds matrix dimensions.
more) optional elseif and an else statement, which is useful
datenum(x) Convert date and time to a number. This error arises when you try to reference an element that
to test various condition.
datestr(x) Convert date and time to string. doesn’t exist. Some useful functions to check sizes and number
of elements are numel(), size(), and length().
if (Condition_1)
MATLAB Commands
Programming elseif (Condition_2)
The expression to the left of the equals sign is not a
valid target for an assignment.
MATLAB Commands This error message arises because of misuse of the = and ==
Script vs. Function vs. Live script else operators. The = operator does an assignment, and the ==
MATLAB Commands operator does a logical test for equality.
end
Script M-files: Contain a list of commands that MATLAB
simply executes in order. They are useful to batch simple Subscripted assignment dimension mismatch.
Switch statement: Evaluate a statement and selection one This error message arises because of an attempt to assign a
sequences of commonly used commands together. of the cases based on this evaluation. vector or matrix into a compartment that it does not fit in.
Function M-files: Can be executed by specifying some switch (statement) Matrix dimensions must agree.
inputs and return some desired outputs. case (value1) This error message arises when you try to do operations on
MATLAB Commands matrices with different dimensions. For example A+B when A is
Live scripts: Contain MATLAB codes, embedded outputs, case (value2) 2 × 2 and B is 2 × 3.
formated texts, equations, and images together in a single MATLAB Commands
environment. end Subscript indices must either be real positive integers
or logicals.
* Add comment: To put a comment within a line, type % This error message arises because of indexing problem. For
followed by the comment in MATLAB command window, While loop statement: Repeat the commands while example A(1) is the first element in a matrix not A(0) (like
MATLAB script, or live script enviroment. condition holds. other programming languages).

MATLAB Cheat Sheet for Data Science - London School of Economics. 3 of 9


Handling errors ‘distributed’: Partition listed functions out among the
workers in a pool: zeros(5,5,‘distributed’), ones, false, plot(x,y) Plot y versus x (have same length).

Try, catch statement: try a statement, if it returns an true, NaN, inf, eye, rand, randi, and randn.
error, catch it and do another statement. ‘codistributed’: Access the arrays distributed among the
workers in a pool: zeros(5,5,‘codistributed’), etc.
try
statements GPU:
catch expression
statements gpuDevice(idx) Select GPU device specified by idx.
end
gpuArray(x) Copy x array to GPU.
arrayfun(func,A) Apply function to elements of A on GPU.
bsxfun(func,A,B) Apply an element-wise binary operation
error(‘msg’) Display message and abort function. plot(y) Plot y, with 1,2,3,... as the x axis.
specified by func to A and B on GPU.
warning(‘msg’) Display warning message. plot(x,f(x)) If f is a function, plot the points.
gather(A) Transfer gpuArray to local workspace.
assert(‘msg’) Throw error if condition is false. existsOnGPU(x) Determine if x is stored in GPU.
st=MException(ID... Input arguments:
,txt) Capture information of a specific
error and save it in the st object. A basic calculation on GPU: Line styles: { - / : / -. / -- }.
Markers: o : Circle / + : Plus sign / * : Asterisk
W=rand(5,‘single’); % Basic random numbers. / . : Point / x : Cross / s : Square
GD=gpuArray(W); % Send W to GPU. / d : Diamond / p : Pentagram / h :
GO=GD.*GD; % Execute multiplication on GPU. Hexagram / ^ : Upward triangle.
Parallel computing (CPU & GPU) Colors: y : Yellow / m : Magenta / c : Cyan
Plotting & Figures / r : Red / g : Green / b : Blue
CPU: / w : White / k : Black.
figure Open up a new figure window.
parpool(size) Create a new parallel pool (size is number of axis normal Default axis limits and scaling behavior.
CPU workers). Name-value pair arguments:
axis tight Force axis to be equaled to data range.
gcp Return the current parallel pool. axis equal Force axis to be scaled equally.
ticBytes/... Color Line color.
axis square Axis lines with equal lengths.
tocBytes(gcp) Start/stop calculation of the bytes transferred axis fill LineStyle Line style.
Lengths of each axis line fill the rectangle.
to the workers. LineWidth Line width.
title(‘Title’) Add a title at the top of the plot.
batch(‘scr’) Run a script or function on a worker. Marker Marker symbol.
xlabel(‘lbl’) Label the x axis as lbl.
gather(A) Transfer distributed array to local workspace. ylabel(‘lbl’) MarkerIndices Indices of marker data points.
Label the y axis as lbl.
MarkerEdgeColor Marker outline color.
zlabel(‘lbl’) Label the z axis as lbl.
MarkerFaceColor Marker fill color.
legend(‘v’,‘w’) Add label to v and w curves.
parfor: Replace for with parfor to execute code on CPU MarkerSize Size of marker.
grid on/off Include/Omit a grid in the plot.
workers or cores without any guaranteed order.
box on/off Display/hide the box outline around axes.
datetick(‘x’,fm) Date formatted tick labels (fm is format).
parfor i = a:s:b xtickformat(fm) X-axis label format.
MATLAB Commands ytickformat(fm) Y-axis label format.
end loglog(x,y) Logarithmic x and y axes.
xlim([min,max]) X-axis limits from min to max.
ylim([min,max]) Y-axis limits from min to max.
spmd: Execute code in parallel on workers of a pool. zlim([min,max]) Z-axis limits from min to max.
hold on/off Allow/prevent plotting on the same graph.
spmd text(x,y,text) Add text to a point (x and y are scalars
statements in data units).
spmd
fn is a function:
fplot(fn,rn) Plot a 2-D plot using fn over rn range.
parfeval: Directly execute a defined function on a specified fmesh(fn,rn) Plot a 3-D mesh using fn over rn range.
worker. fsurf(fn,rn) Plot a 3-D surface using fn over rn range.
fcontour(fn,rn) Plot contour using a function (fn) over semilogx(x,y) Logarithmic x axis.
rn range. semilogy(x,y) Logarithmic y axis.
p=gcp(); % Return current MATLAB pool.
f=parfeval(p,@sum,4,3); % Parallel execution of 4+3.

MATLAB Cheat Sheet for Data Science - London School of Economics. 4 of 9


plot3(x,y,z) Three-dimensional analogue of plot. histfit(y) Histogram plot with distribution fit. gscatter(x,y,group) 2-D scatter plot of x and y
by group.

surf(x,y,z) 3-D shaded surface plot. polarhistogram(y) Histogram plot (polar coordi-
nates). bar(y) Bar plot.

mesh(x,y,z) 3-D mesh surface plot. scatter(x,y) 2-D scatter plot by x and y. bar3(y) 3-D bar plot.

histogram(y) Histogram plot. scatter(x,y,z) 3-D scatter plot by x, y, and z. pie(y)/pie3(y) 2-D/3-D pie plot.

MATLAB Cheat Sheet for Data Science - London School of Economics. 5 of 9


area(y) 2-D area plot. candle(y) Candlestick chart. contour(y) Contour plot of matrix y.

subplot(a,b,c) For multiple figures in a plot


polarplot(theta,rho) Polar plot (theta: angle, highlow(h,l,o,c) Plot h (high), l (low), o (open),
(a/b: number of rows/columns, c:
rho: radius). and c (close) prices of an asset.
selected plot).

compass(x) Compass plot (arrows from center). pointfig(y) Point and figure chart. Data science

Neural network
nnstart Neural network app (GUI).
patternnet(s,t,p) A pattern recognition (classification) network
with s, t, and p as number of hidden layers,
train, and performance function.
feedforwardnet(s,t) An approximation (regression) network with s
and t as number of hidden layers and train
function.
fitnet(s,t) Function fitting network with s and t as
number of hidden layers and train function.
boxplot(y) Box plot. dendrogram(tree) Dendrogram plot by tree.
cascade...
forwardnet(s,t) An approximation (regression) network with s
and t as number of hidden layers and train
function.
selforgmap Design a self-organizing map.
competlayer(nc) Design a competitive layer network with nc as
number of classes.
network Design a custom neural network with different
properties.
view(net) View a designed neural network.
train(net,i,o) Train a network using i and o as input and
output.

MATLAB Cheat Sheet for Data Science - London School of Economics. 6 of 9


predict(net,i) Predictions for i as input by net. mae Mean absolute error performance function. Support vector machines/regression
perform(net,i,o) Calculate network performance using i sae Sum absolute error performance function.
and o as input and output. sse Sum squared error performance function. Support vector machines:
crossentropy Cross entropy performance function.
classi...
learning functions: ficationLearner Open classification learner app (GUI).
Input/Output process functions: fitcsvm(i,o) Train SVM with i as input and o as binary
trainlm Levenberg-Marquardt backpropagation. output for low or moderate dimensional data.
trainbr Bayesian regularization backpropagation. fitclinear(i,o) Train linear SVM with i as input and o as binary
mapminmax Normalize inputs/outputs between -1 and 1. output for high dimensional data.
trainrp Resilient backpropagation.
mapstd Zero mean and unity variance normalization. fitcecoc(i,o) Train SVM with i as input and o as multi-class
trainscg Scaled conjugate gradient backpropagation.
processpca Principal component analysis for input. output by error-correcting output codes model.
trainbfg BFGS quasi-Newton backpropagation.
traincgb Conjugate gradient backpropagation with removecons... fitSVM...
Powell-Beale restarts. tantrows Remove constant inputs/outputs. Posterior(svm_m) Return trained SVM that contains the estimated
traincgp Conjugate gradient backpropagation with fixunknowns Process unknown inputs. score transformation function (svm_m is trained
Polak-Ribire updates. model).
traincgf Conjugate gradient backpropagation with Plots: templateSVM
Fletcher-Reeves updates. /Linear/ECOC SVM/Linear SVM/Error-correcting output
traingda Gradient descent with adaptive learning rate ploterrhist Plot error histogram. templates.
backpropagation. plotregression Plot linear regression. predict(svm_c,i) Predict class labels using svm_c trained model
traingdx Gradient descent with momentum and adaptive plotfit Plot function fit. and i as predictor data.
learning rate backpropagation.
plotperform Plot network performance.
traingdm Gradient descent with momentum backpropagation. Support vector regression:
trainru Unsupervised random order weight/bias training. plottrainstate Plot training state values.
trainr Random order incremental training. plotconfusion Plot classification confusion matrix.
regres...
trains Sequential order incremental training. plotroc Plot receiver operating characteristic.
sionLearner Open regression learner app (GUI).
learncon Conscience bias learning function. plotsomtop Plot self-organizing map topology. fitrsvm(i,o) Train SVR with i as input and o as output for
learnk Kohonen weight learning function. plotsomhits Plot self-organizing map sample hits. low or moderate dimensional data.
learnis Instar weight learning function. plotsomnc Plot self-organizing map neighbor fitrlinear(i,o) Train linear SVR with i as input and o as output
learnos Outstar weight learning function. connections. for high dimensional data.
plotsomnd Plot self-organizing map neighbor distances. predict(svm_r,i) Predict response using svm_r trained model and
plotsomplanes Plot self-organizing map weight planes. i as predictor data.
Transfer functions: plotsompos Plot self-organizing map weight positions.
Basic Support Vector Machines & Support Vector Regression
implementations:
tansig Hyperbolic tangent sigmoid transfer function. Basic Neural Network implementations (classification &
radbas Radial basis transfer function. regression):
radbasn Normalized radial basis transfer function. %% Classification:
logsig Log-sigmoid transfer function. %% Classification: load fisheriris; % Load iris dataset.
tribas Triangular basis transfer function. [i,o]=iris_dataset; % Import iris dataset. i=meas; % Input data.
purelin Linear transfer function. nt=patternnet(5); % Design a netwrok. o=species; % Output data.
satlin Saturating linear transfer function. i_w=nt.IW; % Store initial input weights. sz=numel(o); % Sample size.
poslin Positive linear transfer function. trn_r=1:1:sz-40; % Train range.
satlins Symmetric saturating linear transfer function. %% Regression: tst_r=sz-39:1:sz; % Test range.
hardlim Hard-limit transfer function. [i,o]=simplefit_dataset; % Import a sample dataset. svm_c=fitcecoc(i(trn_r,:),o(trn_r,:)); % Train SVM.
hardlims Symmetric hard-limit transfer function. nt=feedforwardnet(10); % Design a network. svm_cl=crossval(svm_c); % Cross-validation.
elliotsig Elliot symmetric sigmoid transfer function. nt.performFcn=‘mae’; % Change performance func. svm_c_loss=kfoldLoss(svm_cl); % Error estimation.
elliot2sig Elliot 2 symmetric sigmoid transfer function. nt.inputs{1}.processFcns={‘processpca’}; % PCA for pr_out=predict(svm_c,i(tst_r,:)); % Prediction.
softmax Soft max transfer function. input 1.
compet Competitive transfer function. %% Regression:
[nt,tx]=train(nt,i,o); % Train the network. [i,o]=simplefit_dataset; % Import a sample dataset.
view(nt) % Show the network. svm_r=fitrsvm(i’,o’); % Train SVR.
Performance functions: y=nt(i); % Insert input into the network. svm_r_loss_1=resubLoss(svm_r); % Resubstitution loss.
perf=perform(nt,o,y); % Calculate performance. conv=Mdl.ConvergenceInfo.Converged; % Convergance info.
mse Mean squared normalized error performance plotconfusion(c,y); % Plot confusion matrix. nml=Mdl.NumObservations; % Number of observations.
function. plotperform(tx); % Plot network performance.

MATLAB Cheat Sheet for Data Science - London School of Economics. 7 of 9


Deep learning classificationLayer Classification output layer. Two basic Decision Trees implementations:
regressionLayer Regression output layer.

Autoencoder: %% A simple classification decision tree.


Basic Autoencoder & Convolutional Neural Network load fisheriris; % Load iris dataset.
dp1=train... implementations: i=meas; % Input data.
Autoencoder(i,hs) Train an Autoencoder. o=species; % Output data.
encode(dp1,x) Encode input data (x) using dp1. %% Autoencoder: dt1=fitctree(i,o,‘CrossVal’,‘on’); % Fit decision tree
decode(dp1,v) Decode encoded data (v) using dp1. dt=abalone_dataset; % Load Abalone dataset. to data using cross validation.
network(dp1) Convert Autoencoder to network object. dp1=trainAutoencoder(dt); % Train network. view(dt.Trained{1},‘Mode’,‘graph’); % Graph.
plotWeights(dp1) Plot encoder weights of trained Autoencoder. dt_p=predict(dp1,dt); % Reconstruction. er=kfoldLoss(dt1) % Cross validation error rate.
view(autoenc) View Autoencoder structure. mse_er=mse(dt-dt_p) % Calculate MSE.
stack(dp1,dp2) Stack encoders together. %% An ensemble learner.
predict(dp1,in) Predict response using dp1 trained model and %% Convolutional neural network: dt2=fitcensemble(i,o,‘Method’,‘Subspace’); Fit an ensemble
load lettersTrainSet; % Load train data. using Subspace method.
in as predictor data.
load lettersTestSet;; % Load test data. er=resubLoss(dt2); % Resubstitution loss.
lyrs=[imageInputLayer([21 21 1]);
Convolutional neural network: convolution2dLayer(8,15);
reluLayer();
op=training...
fullyConnectedLayer(2); Linear/Nonlinear regression
softmaxLayer();
Options(sv,ops) Training options (ops) of a network (sv: solver
classificationLayer()];
name). Linear:
ops=trainingOptions(’sgdm’); % Settings.
dp2=trainNet...
rng(‘default’) % For reproducibility.
work(i,o,lyr,op) Train a convolutional network using i and o as fitlm(x,y) Fit linear regression to x and y as input and
nt=trainNetwork(XTrain,TTrain,lyrs,ops)
predictor and response variables (op: options). response.
% Train network.
activations(... fitglm(x,y) Fit generalized linear regression to x and y as
o_t=classify(nt,XTest); % Classify test data.
dp2,i,lyr) Network activations for lyr layer using i and input and response.
r_t=testDigitData.Labels; % Real test labels.
dp2 as data and trained network. stepwiselm(x,y) Fit stepwise linear regression to x and y as input
acc=sum(o_t==r_t)/numel(r_t); % Test accuracy.
predict(dp2,i) Predict response using dp2 model and i as and response.
model and predictor data. stepwiseglm(x,y) Fit generalized stepwise linear regression to x and
classify(dp2,i) Classify data using dp2 model and i as y as input and response.
predictor data. Decision tree regress(x,y) Fit multiple linear regression to x and y as input
importCaffe... and response.
Network Import pretrained networks models (Caffe). Fit linear regression to x and y as input and
dt1=fitctree(i,o) Fit a binary classification decision tree to fitrlinear(x,y)
importCaffe...
i and o as predictor and response. response for high dimensional data.
Layers Import network layers (Caffe). Fit robust linear regression to x and y as input
dt2=fitrtree(i,o) Fit a binary regression decision tree to i robustfit(x,y)
dp3=alexnet Pretrained AlexNet network. and response for high dimensional data.
and o as predictor and response.
dp4=vgg16 Pretrained VGG-16 network. mvregress(x,y) Fit multivariate linear regression to x and y as
templateTree Design a decision tree template.
dp5=vgg19 Pretrained VGG-19 network. input and response.
dt3=fitc...
ensemble(i,o) Fit ensemble of classification decision fitlme(tb,fml) Fit linear mixed-effects model for tb and fml as
trees to i and o as predictor and data table and specified formula.
response. fitglme(tb,fml) Fit generalized linear mixed-effects model for tb
dt4=fitr... and fml as data table and specified formula.
Layers:
ensemble(i,o) Fit ensemble of regression decision trees ls=lasso(x,y) Fit regularized least-squares regression to x and y
to o and i as predictor and response. as input and response using lasso or elastic net
imageInputLayer Image input layer.
templateEnsemble Design an ensemble model template. algorithms.
reluLayer Rectified linear unit layer.
dt5=Tree... ls=lassoglm(x,y) Fit regularized least-squares regression to x and y
convolution2dLayer Create 2-D convolutional layer.
Bagger(nt,i,o) Fit bag of decision trees to i and o as as input and response using lasso or elastic net.
maxPooling2dLayer Max pooling layer.
predictor and response (nt: number of algorithm (generalized linear model regression).
fullyConnectedLayer Fully connected layer.
trees.). lasso(ls) Trace plot of lasso fitted model.
averagePooling
md: dt1, dt2, dt3, dt4, or dt5. ridge(y,x,k) Fit multilinear ridge regression to x, y, and k as
2dLayer Average pooling layer.
predict(md,in) Predict response using mt fitted model input, response, and ridge parameters.
crossChannel...
plsreg...
NormalizationLayer Channel-wise local response and in as predictor data.
normalization layer. ress(x,y,nc) Fit Partial Least-Squares (PLS) regression to x
softmaxLayer Softmax layer. mdo: dt3, dt4, or dt5. and y as input and response(nc: PLS components).
dropoutLayer Dropout layer. oobPredict(mdo) Predict out-of-bag response of mdo.

MATLAB Cheat Sheet for Data Science - London School of Economics. 8 of 9


Linear: ... Clustering pcares(x,d) Residuals from principal component analysis for x
and d as data and number of dimensions.
mnrfit(x,y) Fit multinomial logistic regression to x and y f=factoran(x,m) Factor analysis of x and m as data and number of
as input and response. factors.
linkage(x) Agglomerative hierarchical cluster tree for
glmfit(x,y) Fit generalized linear model regression to x rotatefactors(f) Rotate factor loadings.
x as data.
sequentialfs...
and y as input and response. clusterdata(x,cf) Agglomerative clusters for x and cf as
(fun,i,o) Sequential feature selection using i, o, and fun as
predict(lr,xn) Predict response using lr trained model and data and cutoff.
input, predictor, and function handle that defines
xn as predictor data. kmeans(x,k) K-means clustering using x and k as data
criterion.
display(mdl) Display fitted model. and number of clusters.
relieff(i,o,k) ReliefF algorithm attributes importance extraction
findcluster Fuzzy clustering tool (GUI).
using i, o, and k as input, predictor, and number of
subclust(x,rng) Fuzzy subtractive clustering using x and
neighbors.
Nonlinear: rng as data and cluster influence range.
fcm(x,k) Fuzzy c-means clustering using x and k
fitnlm(x,y,...
as data and number of clusters. Cross-validation
kmedoids(x,k) Kmedoids clustering using x and k as
mdf,beta) Fit specified model of mdf for x, y and beta
data and number of clusters. c=cvpartition...
as input, response, and coefficients.
ts=KDTree... (o,‘KFold’,k) K-fold cross-validation (o: predictor).
nlinfit(x,y,...
Searcher(x) Grow kd-tree using x as data. repartition(c) Data repartition for cross-validation.
mdf,beta) Fit specified model of mdf for x, y and beta
createns(x) Create object for growing kd-tree using x crossval(fun,x) Loss estimate of cross-validation for the function
as input, response, and coefficients.
as data. fun and x as data.
mnrfit(x,y,...
ts=Exhaustive... training(c,ix) Training indices of cross-validation for repetition ix.
gr,v,mdf,beta) Fit nonlinear mixed-effects regression for x,
Searcher(x) Prepare exhaustive nearest neighbors test(c,ix) Test indices of cross-validation for repetition ix.
y, gr, v, mdf, and beta as input, response,
searcher using x as data. testcholdout...
groups, predictor (take the same value in a
knnsearch(ts,y) Search for the nearest neighbor in ts to (y1,y2,yr) Compare predictive accuracies of two classification
group), function, and initial estimates for
each point in y. models (McNemar test) using y1, y2, and yr as first
fixed effects.
range... model output, second model output, and true labels.
nlmefitsa(--) Fit nonlinear mixed-effects model with
search(ts,y,r) Find all neighbors within specified testckfold...
stochastic EM algorithm using above inputs.
distance in ts to each point in y (r: (c1,c2,x1,x2) Compare predictive accuracies of two classification
fitrgp(x,y) Fit a Gaussian Process Regression (GPR)
radius around each ts to each point). models by paired F cross-validation test using c1,
model to x and y as input and response.
c2, x1, and x2 as first model, second model,
Two basic clustering models implementations: first data table and second data table.
Basic linear & nonlinear regression implementations:
% k-means clustering.
load fisheriris; % Load iris dataset. Copyright 2017
c / MATLAB R2017a
%% Linear regression:
data=meas(:,1:2); % Select data. Ali Habibnia a.habibnia@lse.ac.uk
load carsmall; % Load carsmall dataset. Eghbal Rahimikia erahimi@ut.ac.ir
x=[Weight,Acceleration]; % Input data. [inx,c]=kmeans(data,3); % k-means clustering.
Download page: http://personal.lse.ac.uk/habibnia/
y=MPG; % Response data. % inx: cluster indices, c: cluster centers. * Feel free to send your feedback.
lm=fitlm(x,y); % Fit linear regression.
display(lm); % Display reports. % Fuzzy c-means clustering.
plot(lm); % Scatter plot. [cnt,u]=fcn(data,3); % Fuzzy c-means clustering.
plotAdjustedResponse(lm,1); % Adjusted response plot % cnt: cluster centers, u: fuzzy partition matrix.
of variable 1.
predict(lm,x); % Reconstruct response.

%% Nonlinear regression: Dimension reduction/feature selection


mdf=@(b,x)b(1)+b(2)*x(:,1);
% Model function.
bt=[-50 500]; % Beta values.
nl=fitnlm(x(:,1),y,mdf,bt); % Fit nonlinear c=pca(x) Principal component analysis of x.
regression. c=ppca(x,m) Probabilistic principal component analysis
display(nl); % Display reports. of x and m as data and number of
predict(nl,x(:,1)); % Reconstruct response. components.
biplot(c) Biplot of the PCA coefficients.
pcacov(w) Principal component analysis on w as
covariance matrix.

MATLAB Cheat Sheet for Data Science - London School of Economics. 9 of 9

View publication stats

You might also like