R on the Raspberry Pi

R on the Raspberry Pi

I’m interested in running R on the Raspberry Pi, and on Raspbian in particular. There are loads of Debian packages for R, and I’m hoping that many of these find there way into Raspbian eventually. Right now it is possible to install and run R from Raspbian, but relatively few packages are available. However, the package r-base can be installed, and that is enough to get up and running with a basic R installation. So,

% sudo apt-get install r-base
% R

should be enough to get started. Indeed, here’s a little Raspbian session to illustrate R running on the Pi:

pi@raspberrypi ~/src/r $ uname -a
Linux raspberrypi 3.1.9+ #168 PREEMPT Sat Jul 14 18:56:31 BST 2012 armv6l GNU/Linux
pi@raspberrypi ~/src/r $ R
R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0Platform: arm-unknown-linux-gnueabihf (32-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.  Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> rnorm(5)
[1] -1.8385888 -1.1114294  0.7943391  1.0070076  0.7702747>

Nice! Base graphics such as scatter plots and histograms all work fine, and can be piped to a remote X server if needed. So even without all the add-on packages it is a perfectly reasonable platform for basic data analysis. To benchmark it, I used my standard Gibbs sampling script, gibbs.R

gibbs=function(N,thin)
{
        x=0
        y=0
        cat(paste("Iter","x","y","n"))
        for (i in 1:N) {
                for (j in 1:thin) {
                        x=rgamma(1,3,y*y+4)
                        y=rnorm(1,1/(x+1),1/sqrt(2*x+2))
                }
                cat(paste(i,x,y,"n"))
        }
}
gibbs(50000,1000)

which I can run and time from the linux command line with

% time Rscript gibbs.R > /dev/null

Unfortunately, this takes over 400 minutes, which is around 3 times slower than the equivalent python benchmarking script that I have run on Raspbian. On Intel, R is around half the speed of python, so there’s a bit of a gap there, but actually python runs slower than it should on the Pi anyway. Comparing against R on Intel, on my fast i7 laptop, this R script takes around 7 minutes, and on my Atom based netbook, it takes around 57 minutes. This is consistent with my other findings – namely that the speed difference between C and higher level languages is greater on the Pi than on Intel. Nevertheless, for many basic data analysis tasks, speed isn’t that much of an issue, and it’s certainly going to be very convenient to have R on the Pi.

Advertisements

Using Linear Regression on ios: Numbers

[ Using Linear Regression on ios: #Numbers ]

Basic course :

https://www.khanacademy.org/math/probability/scatterplots-a1/estimating-trend-lines/e/linear-models-of-bivariate-data

 

Download sample files here :

  1. Developing my muscles.pdf
  2. LinearRegression.numbers.zip

Contents of Using Functions

1. CORREL: Calculates the correlation between two data sets based on linear
regression analysis.
2. FORECAST: Calculates the y (dependent) value that corresponds to a chosen x
(independent) value by using linear regression analysis of known value pairs.
3. INTERCEPT: Calculates the y-intercept of the best-fit line for a data set using linear regression analysis.
4. SLOPE: Finds the slope of the best-fit line for a data set based on linear regression analysis.


1. CORREL
The CORREL function calculates the correlation between two data sets based on linear
regression analysis.
CORREL(y-range, x-range)

  • y-range: A range of cells containing the dependent variable (y).
  • x-range: A range of cells containing the independent variable (x).

Examples
Given the following table:

corell

// Numbers Codes:

CORREL(D2:D7, E2:E7) returns 1.
CORREL(B2:B7, A2:A7) returns 0.977265.

2. FORECAST
The FORECAST function uses linear regression analysis of known value pairs to find the
y (dependent) value that corresponds to a chosen x (independent) value.
FORECAST(x, y-values, x-values)

  • x: The x value for which you want to find a corresponding y value.
  • y-values: A range of cells containing the known y values. Must be the same size as xvalues.
  • x-values: A range of cells containing the known x values.

Notes
You can use the SLOPE and INTERCEPT functions to find the equation used to calculate forecast values.

Examples
Given the following table:

forecast

Numbers Codes:

FORECAST(9, A3:F3, A2:F2) returns 19.

3. INTERCEPT
The INTERCEPT function calculates the y-intercept of the best-fit line for the data set
using linear regression analysis.

INTERCEPT(y-range, x-range)

  • y-range: A list of values for the dependent variable y. Must be the same size as xrange.
  • x-range: A range of cells containing values for the independent variable x. Must be the same size as y-range.

Notes
To find the slope of the best-fit line, use the SLOPE function.

Examples
Given the following table:

intercept

Numbers Code:

INTERCEPT(A2:F2, A1:F1) returns 1.
SLOPE(A2:F2, A1:F1) returns 2.
INTERCEPT(A5:F5, A4:F4) returns 2.392.

4. SLOPE
The SLOPE function calculates the slope of the best-fit line for the data set based on linear regression analysis.

SLOPE(y-range, x-range)

  • y-range: A range of cells containing the dependent variable y. Must be the same size as x-range.
  • x-range: A range of cells containing the independent variable x. Must be the same size as y-range.

Notes
To find the y-intercept of the best-fit line, use the INTERCEPT function.

Examples
Given the following table:

slope

// Numbers Codes:

SLOPE(A2:F2, A1:F1) returns 2.
INTERCEPT(A2:F2, A1:F1) returns 1.
SLOPE(A5:F5, A4:F4) returns 0.402.

Total Examples

TotalLinearRegression

// Numbers Codes:
// y = ax + b

SLOPE(B3:F3,B2:F2)      // a
INTERCEPT(B3:F3,B2:F2)  // b

// Linear Regression line
FORECAST(B2,B3:F3,B2:F2)
FORECAST(C2,B3:F3,B2:F2)
FORECAST(D2,B3:F3,B2:F2)
FORECAST(E2,B3:F3,B2:F2)
FORECAST(F2,B3:F3,B2:F2)

Application (To Expect the Size of muscle in the future)

(Using Linear Function:   y = ax + b  )

LinearRegression_ios

// Numbers Codes:

// 1. Arm size:
SLOPE(E6:E10,A6:A10)
INTERCEPT(E6:E10,A6:A10)
FORECAST(A10,E6:E10,A6:A10)

// 2. Forearm size:
SLOPE(I6:I10,A6:A10)
INTERCEPT(I6:I10,A6:A10)
FORECAST(A10,I6:I10,A6:A10)

END

#numbers #linearregression #stat #correl #forecast #intercept #slope

Octave bindings for OpenCV

[ Octave bindings for OpenCV ]
Here are Octave bindings for OpenCV, a collection of over 500 functions implementing computer vision, image processing, machine learning, and general-purpose numeric algorithms. The library can optionally use Intel’s IPP library for better performance (via MMX, SSE, SSE2, etc).


Installing
This is a port of the Python bindings for OpenCV, and as such lives in the OpenCV source tree. When configured properly, the tree will produce an Octave binary package.

You must have a recent version of SWIG in your PATH (like from SVN, such that it supports Octave) and configure the tree with –with-octave and –with-swig. The build will produce an Octave package at $prefix/share/opencv/opencv-1.0.tar.gz. For example (assuming the loader sees /usr/local/lib),

$ tar xzf octave-opencv-031808.tar.gz
$ cd opencv-031808
$ ./configure --with-swig --with-octave
$ make
$ make install
$ octave -q
octave:1>> pkg install /usr/local/share/opencv/octave/opencv-1.0.tar.gz
octave:2>> pkg list
Package Name  | Version | Installation directory
--------------+---------+-----------------------
      opencv *|   1.0.0 | /home/x/octave/opencv-1.0.0
octave:3> I=cvLoadImage("image002.png");

 


Using the library
There is extensive documention on the library available here, and online help is available using the help command in octave. The online help gives only the function prototypes, but when used in conjunction with the OpenCV manual, it should be sufficient to use the library.

Interoperability with Octave matrices and images is provided via the cv2mat, mat2cv, cv2im, and im2cv functions. There is online help available for these.

 


Releases
3/27/08: These bindings have been integrated into OpenCV. Latest sources are now available only from OpenCV CVS.

3/17/08: first release; alpha quality. Samples translated but not all running.
octave-opencv-031808.tar.gz (patched opencv, based on recent cvs snapshot)
octave-opencv-031808.patch.gz (just the patch)

octave-opencv was written by Xavier Delacour. Please send feedback, bugs, and/or patches to xavier dot delacour at gmail dot com.

05: Octave and MATLAB

[ 05: Octave and MATLAB ]

Previous Next Index

Video Lectures

 

Installation

 

Basic Operation

Changing Octave Prompt

%% Changing Octave Prompt
PS1('>>');
%% Change working directory in windows example:
cd 'c:/path/to/desired/directory name'
%% Note that it uses normal slashes and does not uses escape characters for the empty spaces.

Elementary Operations

%% elementary operations
5+6

ans =  11

3-2

ans =  1

5*8

ans =  40

1/2

ans =  0.50000

2^6

ans =  64

1 == 2  % false

ans = 0

1 ~= 2  % true. note, not "!="

ans =  1

1 && 0

ans = 0

1 || 0

ans =  1

xor(1,0)

ans =  1

Variable Assignment

%% variable assignment
a = 3;

b = 'hi';

c = 3>=1;

% Displaying them:

a = pi;

disp(a)

3.1416

disp(sprintf('2 decimals: %0.2f', a))

2 decimals: 3.14

disp(sprintf('6 decimals: %0.6f', a))

6 decimals: 3.141593

format long

a

a =  3.14159265358979

format short

a

a =  3.1416

Vectors and Matrices

%% vectors and matrices

A = [1 2; 3 4; 5 6]

A =

   1   2

   3   4

   5   6

v = [1 2 3]

v =

   1   2   3

v = [1; 2; 3]

v =

   1

   2

   3

%% from 1 to 2, with stepsize of 0.1. Useful for plot axes

v = [1:0.1:2]

v =

   1.0000   1.1000   1.2000   1.3000   1.4000   1.5000   1.6000   1.7000   1.8000   1.9000   2.0000

%% from 1 to 6, assumes stepsize of 1 (row vector)

v = 1:6

v =

   1   2   3   4   5   6

% same as C = [2 2 2; 2 2 2]

C = 2*ones(2,3)

C =

   2   2   2

   2   2   2

% 1x3 vector of ones

w = ones(1,3)

w =

   1   1   1

w = zeros(1,3)

w =

   0   0   0

% drawn from a uniform distribution

w = rand(1,3)

w =

   0.558722   0.596400   0.056719

w = randn(1,3)

w =

  -0.23185   2.32378   0.52952

% (mean = -6, var = 10) - note: add the semicolon

w = -6 + sqrt(10)*(randn(1,10000));

% plot histogram using 10 bins (default)
hist(w)

Plot histogram using 50 bins

hist(w,50)  % plot histogram using 50 bins
% note: if hist() crashes, try “graphics_toolkit(‘gnu_plot’)”

I = eye(4)

I =

Diagonal Matrix

   1   0   0   0

   0   1   0   0

   0   0   1   0

   0   0   0   1

help eye
help rand
help help

Moving Data Around

Data files are used in this sectionfeaturesX.datpriceY.dat

Matrix and Dimensions

A = [1 2; 3 4; 5 6]

A =

   1   2

   3   4

   5   6

%% dimensions

% 1×2 matrix: [(number of rows) (number of columns)]

sz = size(A)

sz =

   3   2

%% dimensions

% 1×2 matrix: [(number of rows) (number of columns)]

sz = size(A)

sz =

   3   2

% number of rows

size(A,1)

ans =  3

% number of cols

size(A,2)

ans =  2

% size of longest dimension

v = [1 2 3]

v =

   1   2   3

v = [1; 2; 3]

v =

   1

   2

   3

length(v)

ans =  3

Loading Data

%% Loading Data
% show current directory (current path)

pwd
ans = /home/pi

pwd
ans = /home/pi

ls
bcm2835-1.16	     Desktop	gather_gps.py  opencv-3.0.0		  python_games			sw_linux
bcm2835-1.16.tar.gz  Documents	indiecity      opencv-3.1.0		  raspberry-pi-starter-project	tf
book		     Downloads	octave-core    parse-embedded-sdks-1.0.0  sketchbook

% change directory
cd Downloads
ls
featuresX.dat  opencv-3.1.0.zip  priceY.dat

load priceY.dat % alternatively, load('priceY.dat')
load featuresX.dat

% list variables in workspace
who
Variables in the current scope:

A          C          ans        featuresX  priceY     v
B          a          c          ind        r          val

whos
Variables in the current scope:

   Attr Name           Size                     Bytes  Class
   ==== ====           ====                     =====  =====
        A              9x9                        648  double
        B              3x2                         48  double
        C              2x2                         32  double
        a              1x4                         32  double
        ans            1x8                          8  char
        c              3x1                         12  double
        featuresX     27x2                        432  double
        ind            1x1                          8  double
        priceY        27x1                        216  double
        r              3x1                         12  double
        v              3x1                         24  double
        val            1x2                         16  double

Total is 196 elements using 1488 bytes

v = featuresX(1:10)
v =

   2104   1600   2400   1416   3000   1985   1534   1427   1380   1494

v = priceY(1:10)
v =

   3999
   3299
   3690
   2320
   5399
   2999
   3149
   1989
   2120
   2425

% save variable v into file hello.mat
save hello.mat v;

ls
featuresX.dat  hello.mat  opencv-3.1.0.zip  priceY.dat
% save as ascii
save hello.txt v -ascii;
ls
featuresX.dat  hello.mat  hello.txt  opencv-3.1.0.zip  priceY.dat

% fopen, fread, fprintf, fscanf also work  [[not needed in class]]

Indexing Matrix

%% indexing
% indexing is (row,col)
A(3,2)
ans =  78

% get the 2nd row.
A(2,:)
ans =

   57   68   79    9   11   22   33   44   46

% ":" means every element along that dimension

% print all  the elements of rows 1 and 3
A([1 3],:)
ans =

   47   58   69   80    1   12   23   34   45
   67   78    8   10   21   32   43   54   56

A = [1 2; 3 4; 5 6]
A =

   1   2
   3   4
   5   6

% change second column
A(:,2) = [10; 11; 12]
A =

    1   10
    3   11
    5   12

% print all  the elements of rows 1 and 3
A([1 3],:)
ans =

    1   10
    5   12

A(2,:)
ans =

    3   11

% ":" means every element along that dimension

% append column vec
A = [A, [100; 101; 102]];
A = [A, [100; 101; 102]]
A =

     1    10   100   100
     3    11   101   101
     5    12   102   102

% Select all elements as a column vector.
A(:)
ans =

     1
     3
     5
    10
    11
    12
   100
   101
   102
   100
   101
   102

% Putting data together
A = [1 2; 3 4; 5 6]
A =

   1   2
   3   4
   5   6

B = [11 12; 13 14; 15 16]
B =

   11   12
   13   14
   15   16

% same dims as A
C = [A B]
C =

    1    2   11   12
    3    4   13   14
    5    6   15   16

% concatenating A and B matrices side by side

C = [A; B]
C =

    1    2
    3    4
    5    6
   11   12
   13   14
   15   16

% Concatenating A and B top and bottom

  

Computing On Data

Initialize Variables

%% initialize variables
A = [1 2;3 4;5 6]
A =

   1   2
   3   4
   5   6

B = [11 12;13 14;15 16]
B =

   11   12
   13   14
   15   16

C = [1 1;2 2]
C =

   1   1
   2   2

v = [1;2;3]
v =

   1
   2
   3

%% matrix operations
% matrix multiplication
A * C
ans =

    5    5
   11   11
   17   17

% element-wise multiplication
A .* B
ans =

   11   24
   39   56
   75   96

% element-wise square of each element in A
A .^ 2
ans =

    1    4
    9   16
   25   36

% element-wise reciprocal
1./v
ans =

   1.00000
   0.50000
   0.33333

% functions like this operate element-wise on vecs or matrices
log(v)
ans =

   0.00000
   0.69315
   1.09861

exp(v)
ans =

    2.7183
    7.3891
   20.0855

abs(v)
ans =

   1
   2
   3

% -1*v
-v
ans =

  -1
  -2
  -3

v + ones(length(v), 1)
ans =

   2
   3
   4

% v + 1 % same

% matrix transpose
A'
ans =

   1   3   5
   2   4   6

%% misc useful functions
% max  (or min)
a = [1 15 2 0.5]
a =

    1.00000   15.00000    2.00000    0.50000

val = max(a)
val =  15

% val -  maximum element of the vector a and index - index value where maximum occur
[val,ind] = max(a)
val =  15
ind =  2

% if A is matrix, returns max from each column
val = max(A)
val =

   5   6

% find
a < 3
ans =

   1   0   1   1

find(a < 3)
ans =

   1   3   4

A = magic(3)
A =

   8   1   6
   3   5   7
   4   9   2

% row, column indices for values matching comparison
[r,c] = find(A>=7)
r =

   1
   3
   2

c =

   1
   2
   3

% sum, prod
sum(a)
ans =  18.500

prod(a)
ans =  15

% or ceil(a)
floor(a)
ans =

    1   15    2    0

max(rand(3),rand(3))
ans =

   0.27772   0.74698   0.39357
   0.82662   0.76164   0.45336
   0.67367   0.92308   0.81734

% maximum along columns(defaults to columns - max(A,[]))
max(A,[],1)
ans =

   8   9   7

% maximum along rows
max(A,[],2)
ans =

   8
   7
   9

A = magic(9)
A =

   47   58   69   80    1   12   23   34   45
   57   68   79    9   11   22   33   44   46
   67   78    8   10   21   32   43   54   56
   77    7   18   20   31   42   53   55   66
    6   17   19   30   41   52   63   65   76
   16   27   29   40   51   62   64   75    5
   26   28   39   50   61   72   74    4   15
   36   38   49   60   71   73    3   14   25
   37   48   59   70   81    2   13   24   35

sum(A,1)
ans =

   369   369   369   369   369   369   369   369   369

sum(A,2)
ans =

   369
   369
   369
   369
   369
   369
   369
   369
   369

sum(sum( A .* eye(9) ))
ans =  369

sum(sum( A .* flipud(eye(9)) ))
ans =  369

% Matrix inverse (pseudo-inverse)
% inv(A'*A)*A'
pinv(A)
ans =

 Columns 1 through 8:

   4.5353e-04  -1.2230e-03   1.6729e-03   1.2647e-02  -1.2062e-02   3.1805e-04   3.0300e-04   2.9902e-04
   3.0111e-04   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04
  -1.0706e-03   1.4019e-02  -1.2045e-02   3.0091e-04   4.5374e-04   1.4870e-04   2.8418e-04   3.1993e-04
   1.2647e-02  -1.2045e-02   3.0132e-04   3.0300e-04  -1.0725e-03   1.6729e-03   4.5353e-04   1.3176e-04
  -1.0810e-02   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04
   2.8418e-04   4.7047e-04   1.4870e-04  -1.0706e-03   1.6747e-03   2.9923e-04   3.0091e-04   1.2647e-02
   3.0300e-04   2.8230e-04   3.1805e-04   4.5353e-04   1.4849e-04   3.0132e-04   1.2647e-02  -1.3416e-02
   3.0111e-04   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04
   3.0091e-04   3.0320e-04   2.9923e-04   2.8418e-04   1.2664e-02  -1.2045e-02  -1.0706e-03   1.8253e-03

 Column 9:

   3.0132e-04
   3.0111e-04
   2.9923e-04
   3.1805e-04
   1.1412e-02
  -1.2045e-02
   1.6729e-03
-- less -- (f)orward, (b)ack, (q)uit
ans =

 Columns 1 through 8:

   4.5353e-04  -1.2230e-03   1.6729e-03   1.2647e-02  -1.2062e-02   3.1805e-04   3.0300e-04   2.9902e-04
   3.0111e-04   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04
  -1.0706e-03   1.4019e-02  -1.2045e-02   3.0091e-04   4.5374e-04   1.4870e-04   2.8418e-04   3.1993e-04
   1.2647e-02  -1.2045e-02   3.0132e-04   3.0300e-04  -1.0725e-03   1.6729e-03   4.5353e-04   1.3176e-04
  -1.0810e-02   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04
   2.8418e-04   4.7047e-04   1.4870e-04  -1.0706e-03   1.6747e-03   2.9923e-04   3.0091e-04   1.2647e-02
   3.0300e-04   2.8230e-04   3.1805e-04   4.5353e-04   1.4849e-04   3.0132e-04   1.2647e-02  -1.3416e-02
   3.0111e-04   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04
   3.0091e-04   3.0320e-04   2.9923e-04   2.8418e-04   1.2664e-02  -1.2045e-02  -1.0706e-03   1.8253e-03

 Column 9:

   3.0132e-04
   3.0111e-04
   2.9923e-04
   3.1805e-04
   1.1412e-02
  -1.2045e-02
   1.6729e-03
   3.0111e-04
-- less -- (f)orward, (b)ack, (q)uit

ans =

 Columns 1 through 8:

   4.5353e-04  -1.2230e-03   1.6729e-03   1.2647e-02  -1.2062e-02   3.1805e-04   3.0300e-04   2.9902e-04
   3.0111e-04   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04
  -1.0706e-03   1.4019e-02  -1.2045e-02   3.0091e-04   4.5374e-04   1.4870e-04   2.8418e-04   3.1993e-04
   1.2647e-02  -1.2045e-02   3.0132e-04   3.0300e-04  -1.0725e-03   1.6729e-03   4.5353e-04   1.3176e-04
  -1.0810e-02   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04   3.0111e-04
   2.8418e-04   4.7047e-04   1.4870e-04  -1.0706e-03   1.6747e-03   2.9923e-04   3.0091e-04   1.2647e-02
   3.0300e-04   2.8230e-04   3.1805e-04   4.5353e-04   1.4849e-04   3.0132e-04   1.2647e-02  -1.3416e-02
   3.0111e-04   3.0111e-04  -1.0878e-03   1.6900e-03   3.0111e-04   1.2801e-02  -1.2199e-02   3.0111e-04
   3.0091e-04   3.0320e-04   2.9923e-04   2.8418e-04   1.2664e-02  -1.2045e-02  -1.0706e-03   1.8253e-03

 Column 9:

   3.0132e-04
   3.0111e-04
   2.9923e-04
   3.1805e-04
   1.1412e-02
  -1.2045e-02
   1.6729e-03
   3.0111e-04
   1.4870e-04

~
~
~
~
~
~
~
~
~
~
-- less (100%) (f)orward, (b)ack, (q)uit

 

Plotting Data

%% plotting
t = [0:0.01:0.98];
y1 = sin(2*pi*4*t);
plot(t,y1);

 Screenshot 2016-06-11 15.18.35

y2 = cos(2*pi*4*t);
hold on;  % "hold off" to turn off
plot(t,y2,'r');
xlabel('time');
ylabel('value');
legend('sin','cos');
title('my plot');
print -dpng 'myPlot.png'

Screenshot 2016-06-11 15.31.12

close;  % or, "close all" to close all figures
figure(1); plot(t, y1);
figure(2); plot(t, y2);
figure(2), clf;  % can specify the figure number
subplot(1,2,1);  % Divide plot into 1x2 grid, access 1st element
plot(t,y1);
subplot(1,2,2);  % Divide plot into 1x2 grid, access 2nd element
plot(t,y2);
axis([0.5 1 -1 1]);  % change axis scale

Screenshot 2016-06-11 15.59.01


%% display a matrix (or image)
figure;
imagesc(magic(15)), colorbar, colormap gray;

Screenshot 2016-06-11 16.13.05

% comma-chaining function calls.
a=1,b=2,c=3
a = 1
b = 2
c = 3
a=1;b=2;c=3;

Control Statements : for, while, if statements

v = zeros(10,1);
v =

   0
   0
   0
   0
   0
   0
   0
   0
   0
   0

 

for i=1:10,
  v(i) = 2^i;
end;
v
v =

      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024

 

indices=1:10;
indices
indices =

    1    2    3    4    5    6    7    8    9   10

 

for i=indices,
   disp(i);
end;
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

 

v
v =

      2
      4
      8
     16
     32
     64
    128
    256
    512
   1024

 

i = 1;
while i <= 5,
   v(i) = 100;
   i = i+1;
end;
v
v =

    100
    100
    100
    100
    100
     64
    128
    256
    512
   1024

 

i=1;
while true,
   v(i) = 999;
   i = i+1;
   if i == 6,
     break;
   end;
end;
v
v =

    999
    999
    999
    999
    999
     64
    128
    256
    512
   1024

 

v(1)
ans =  999
v(1)=2;
if v(1)==1,
   disp('The value is one');
 elseif v(1)==2,
   disp('The value is two');
 else
   disp('The value is not one ro two');
end;
The value is two

Functions

To create a function, type the function code in a text editor (e.g. gedit or notepad), and save the file as “functionName.m”

Example function:

% In a text ("functionName.m")

function y = squareThisNumber(x)
y = x^2;

To call the function in Octave, do either:

1) Navigate to the directory of the functionName.m file and call the function:

% Navigate to directory:
cd /path/to/function
% Call the function:
functionName(5)
ans = 25  % ( y=5^2 = 25 )

2) Add the directory of the function to the load path and save it:

% To add the path for the current session of Octave:
addpath('/path/to/function/')
% To remember the path for future sessions of Octave, after executing addpath above, also do:
savepath

Octave’s functions can return more than one value unlike most other programming languages . Example:

% In a editor

function [y1, y2] = squareandCubeThisNo(x)
y1 = x^2
y2 = x^3

Call the above function this way :

% [a,b] = squareandCubeThisNo(x)

[a,b] = squareandCubeThisNo(5)
a
 a = 25   % (y1=5^2 = 25)
b
 b = 125  % (y2=5^3 = 125)

 

 

Cost Function Algorithm:

 CostFunction

Cost Function J(θ) – is a way to, using your training data, determine values for your θ values which make the hypothesis as accurate as possible

There is a m-file called “costFunctionJ.m”.

% File name: costFunctionJ.m
% Date: 16th June 2016

function J = costFunctionJ(x, y, theta)

% x is the "design matrix" containing our training examples.
% y is the class labels

m = size(x,1);          % number of training examples.
predictions = x*theta;  % predictions of hypothesis on all m examples
sqrErrors = (predictions-y).^2;  %squared errors

J = 1/(2*m) * sum(sqrErrors);

Call the above function this way :

x = [1 1; 1 2; 1 3]
x =

   1   1
   1   2
   1   3

y = [1; 2; 3]
y =

   1
   2
   3

theta = [0; 0]
theta =

   0
   0

j = costFunctionJ(x,y,theta)
j = 2.3333

Manual method:

(1^2 + 2^2 + 3^2)/ (2*3)
ans = 2.3333

Vectorization

Vectorization example

vectorization

Unvectorized implementation

 Lets look on the example in Octave(Matlab).

prediction = 0.0;
for j = 1:n+1,
  prediction = prediction + theta(j) * x(y)
end;

Lets look on the example in C++.

double prediction = 0.0;
for (int j = 0; j < n; j++)
  prediction += theta[j] * x[y];

Vectorized implementation

Lets look on the example in Octave(Matlab).

prediction = theta’ * x;

Lets look on the example in C++.

double prediction
  = theta.transpose() * x;

 

 

Gradient Descent

GradientDescent

GradientDescent02

 

 

Octave Resources

At the Octave command line, typing help followed by a function name displays documentation for a built-in function. For example, help plot will bring up help information for plotting. Further documentation can be found at the Octave documentation pages.

MATLAB Resources

At the MATLAB command line, typing help followed by a function name displays documentation for a built-in function. For example, help plot will bring up help information for plotting. Further documentation can be found at the MATLAB documentation pages.

MathWorks also has a series of videos about various MATLAB features:

Introduction to MATLAB

Learning Module Learning Goals
What is MATLAB? Introduce MATLAB
The MATLAB Environment Navigate the command line, workspace, directory, and editor
MATLAB Variables Use the assignment operator to define scalar variables
MATLAB as a Calculator Perform arithmetic calculations with scalars and functions using MATLAB syntax and order of operations.
Mathematical Functions Use MATLAB variables for input and output to functions. Examples include: COS, SIN, EXP, and NTHROOT.

Vectors

Learning Module Learning Goals
Creating Vectors via Concatenation Create vectors by entering individual elements
Accessing Elements of a Vector Access specific elements of a vector
Vector Arithmetic Perform arithmetic calculations with vectors including element-wise operations
Vector Transpose Use the transpose operator to convert between row and column vectors
Creating Uniformly Spaced Vectors (The Colon Operator) Use the colon operator syntax to create vectors given the starting and ending values and the size of the interval
Creating Uniformly Spaced Vectors (The LINSPACE Function) Use the LINSPACE function to create a vector.

Visualization

Learning Module Learning Goals
Line Plots Create a line plot of a vector and customize plot markers and colors
Annotating Graphs Label axes, add a title, and add a legend to a plot

Matrices and Arrays

Learning Module Learning Goals
Creating Matrices Create matrices by directly entering scalars
Array Creation Functions Create larger matrices and vectors with built in MATLAB functions such as ZEROS and EYE
Accessing Elements of and Array Access elements of an array including entire columns or rows using row-column indexing.
Array Size and Length Use built-in functions to determine array dimensions
Concatenating Arrays Build larger arrays from smaller ones
Matrix Multiplication Perform matrix multiplication and interpret error messages related to incompatible dimensions.

Programming

Learning Module Learning Goals
Using the MATLAB Editor Write a script in the MATLAB Editor, break code into sections to execute, and find help on functions
Logical Operators Use relational and logical operators to create logical variables for program control
Conditional Data Selection Access and change elements for a vector the meet a specified criteria
If-Else Statements Use if-else statements to control which lines of code are evaluated
For Loops Repeat a sequence of commands a specified number of times
While Loops Repeat a sequence of commands while a specified condition is true

 

Installing Octave/MATLAB on Windows

[ Installing Octave/MATLAB on Windows ]

Installing Octave on Windows

Use this link to install Octave for windows: http://wiki.octave.org/Octave_for_Microsoft_Windows

Octave on Windows can be used to submit programming assignments in this course but will likely need a patch provided in the discussion forum. Refer to https://www.coursera.org/learn/machine-learning/discussions/vgCyrQoMEeWv5yIAC00Eog?page=2 for more information about the patch for your version.

 

Installing MATLAB on Windows

MathWorks is providing you access to MATLAB for use in your coursework. When planning your activity, please note that access is valid for 120 days upon activating.

Use the link provided to download and install. You will need to log-in to your MathWorks account, or create one if you do not have one. After starting the installer, accept all defaults and log-in to your MathWorks account when prompted.

Installing Octave/MATLAB on GNU/Linux

[ Installing Octave/MATLAB on GNU/Linux ]

Installing Octave on GNU/Linux

We recommend using your system package manager to install Octave.

On Ubuntu, you can use:

  • sudo apt-get update && sudo apt-get install octave

On Fedora, you can use:

  • sudo yum install octave-forge

Please consult the Octave maintainer’s instructions for other GNU/Linux systems.

 

Installing MATLAB on GNU/Linux

MathWorks is providing you access to MATLAB for use in your coursework. When planning your activity, please note that access is valid for 120 days upon activating.

Use the link provided to download and install. You will need to log-in to your MathWorks account, or create one if you do not have one. After starting the installer, accept all defaults and log-in to your MathWorks account when prompted.

Installing Octave/MATLAB on Mac OS X (10.10 Yosemite and 10.9 Mavericks)

[ Installing Octave/MATLAB on Mac OS X (10.10 Yosemite and 10.9 Mavericks) ]

Installing Octave on Mac OS X (10.10 Yosemite and 10.9 Mavericks)

Mac OS X has a feature called Gatekeeper that may only let you install applications from the Mac App Store. You may need to configure it to allow the Octave installer. Visit your System Preferences, click Security & Privacy, and check the setting to allow apps downloaded from Anywhere. You may need to enter your password to unlock the settings page.

2. Download the Octave 3.8.0 installer. The file is large so this may take some time.

3. Open the downloaded image, probably named GNU_Octave_3.8.0-6.dmg on your computer, and then openOctave-3.8.0-6.mpkg inside.

4. Follow the installer’s instructions. You may need to enter the administrator password for your computer.

5. After the installer completes, Octave should be installed on your computer. You can find Octave-cli in your Mac’s Applications, which is a text interface for Octave that you can use to complete Machine Learning’s programming assignments.

Octave also includes an experimental graphical interface which is called Octave-gui, also in your Mac’s Applications, but we recommend using Octave-cli because it’s more stable.

Note: If you use a package manager (like MacPorts or Homebrew), we recommend you follow the package manager installation instructions.

Installing MATLAB on Mac OS X (10.10 Yosemite and 10.9 Mavericks)

MathWorks is providing you access to MATLAB for use in your coursework. When planning your activity, please note that access is valid for 120 days upon activating.

Use the link provided to download and install. You will need to log-in to your MathWorks account, or create one if you do not have one. After starting the installer, accept all defaults and log-in to your MathWorks account when prompted.