top of page
Search
  • Writer's pictureSachin Tah

Octave Basics



Octave is a high-level language that can be used for performing complex mathematical calculations and also creating prototypes for Machine Learning (ML) projects quickly and easily, and I mean it. It is one of the best languages to start your journey into the world of Machine Learning.


ML projects have a low success rate, as per Gartner, 87% of ML projects fail due to various reasons. It is therefore very important not to invest huge resources and money during initial analysis and POC for an ML-based project. That's were Octave can be useful


Below are some of the advantages of using Octave

  • Easy to learn with a low learning curve

  • Excellent CLI for directly defining variables and executing commands

  • Powerful mathematical and matrix operations

  • Powerful 2D and 3D plotting and visualization tool

  • Free and open-source and can run on multiple platforms including Windows, Mac, Linux, and more

Instead of implementing initial level ML prototypes in a language like Python or a library like Tensorflow, Octave can be used to quickly implement a working prototype, plot graphs, and analyze the nature of your ML data.


Matrices are building blocks of the data used in machine learning programs, they are an almost inseparable part of ML programs. Most ML libraries come with built-in support for Matrix operations like addition, subtraction, division, inverse, and so on.


Octave Operations


You can download and install Octave using the following link https://www.gnu.org/software/octave/download


Installation is easy and quick. You can run octave commands via Octave-cli or by writing functions inside a file. You can directly evaluate an expression, assign variables, print results, plot graphs even with CLI. Click on the Octave-cli icon to open Octave-cli. This will give you a prompt like below





Try some simple mathematical equations like 7 + 4 -2 and hit enter, octave CLI will provide you results and store it in a variable ans,




You can use any expression for evaluation as well ex, 1==2, which will returns 0 (false). Assign a variable with a value ex a=10.


Let us see what all basic operations we can perform in Octave


Basic Operations

octave:2> a =10
a=10
octave:3> 1==2
ans=0
octave:4> 1==1
ans=1
octave:5> b= 20
b =  20
octave:6> who
Variables in the current scope:
a    ans  b
octave:7> disp(a)
10
octave:8> x=pi
x =  3.1416
octave:9> r=10
r=10
octave:9> area=pi*r^2
area =  78.540

Initializing Matrix


Octave is really cool when we try to perform matrix operations. Let us see how to create a matrix and perform some operations on the matrix.


You can define the matrix using [] brackets. Elements in a column should be separated by space and semicolon (;) separates rows in a matrix.

octave:10> A=[1 2 3] % Creates a 1x3 Matrix, 1 Row & 3 Columns  
A =
   1   2   3
octave:11> A = [1 2; 3 4] % Creates a 2x2 Matrix
A =
      1   2
      3   4
octave:15> B = [5 6;7 8]
B =
   5   6
   7   8
octave:16> X = [1:1:10] % Starting 1 with difference of 1 upto 10
X =
    1    2    3    4    5    6    7    8    9   10
octave:17> X = [1:2:10] % Starting 1 with difference of 2 upto 10
X =
   1   3   5   7   9
octave:18> X = [1:0.1:2]
X =
 Columns 1 through 8:
    1.0000   1.1000   1.2000   1.3000   1.4000   1.5000   1.6000               
    1.7000
 Columns 9 through 11:
    1.8000   1.9000   2.0000

Matrix Operations


Now let us try to perform some basic matrix operations


octave:16> A + B
ans =   6    8
        10   12
octave:17> A * B
ans =   19   22
        43   50
octave:17> B * A
ans =   23   34
        31   46
octave:17> C = A - B % Store result in Matrix C
C =   19   22
        43   50
octave:17> C = A - B % Store result in Matrix C
C =   19   22
      43   50
octave:18> A = A.*2 % . Signifies element wise operator
A = 2  4
    6  8                     
octave:19> A = A./2
A = 1  2
    3  4
octave:20> A = ones(3,2) % Creates 3x2 matrix of 1
A =
   1   1
   1   1
   1   1
octave:21> B = zeros(3,2) % Creates 3x2 matrix of 0
B =
   0   0
   0   0
   0   0   
octave:22> C = eye(3,3) % Identity 3 x 3 matrix (Diagonal 1 and rest 0
   C =
   Diagonal Matrix
      1   0   0
      0   1   0
      0   0   1
octave:22> D = rand(2,2) % 2x2 Matrix of random numbers
ans =
   0.96599   0.56881
   0.43081   0.71877
   

I am sure you all are aware of rules related to operations that we can perform on Matix


For example, you can multiple matrices only if matrix A has m x n columns and matrix B has n x q columns. This means the number of columns in the first matrix should be equal to the number of rows in the second matrix.


Consider some examples




If you are not comfortable with Matrix operations, I recommend you to try out various combinations using pen and paper and verify the same using Octave CLI.


Here are some details on how to get the element out of Matrix


octave:23> A = [1 2 3;4 5 6] 
A =
	   1   2   3
	   4   5   6
octave:24> size(A)  % Returns matrix with details on A rows and columns
ans = 
	2	3
octave:25> C = size(A)	% 2x3 Matrix C, C(1) = 2 & C(2) = 3 
C =
	 2   3
octave:26> size(A,1)	% Returns total Rows
ans = 2
octave:27> size(A,2)	% Returns total Columns
ans = 3
octave:28> length(A)	% Returns longest dimension 
ans = 3                                                     

* - A one-dimensional matrix is also called a Vector, matrix can be a row vector or a column vector.



octave:29> A
A =
   1   2   3
   4   5   6
octave:30> A(1,1) % Returns element from 1st row and 1st column
ans = 1
octave:31> A(1,2)  % Returns element from 1st row and 2nd column
ans = 2
octave:32> A(3,3) % Returns element from 3rd row and 3rd column
ans = 3
octave:33> A(:,1) % Returns all Rows and 1st Column
ans = 
	1
	4
octave:34> A(2,:) % Returns all columns and 2nd row
ans = 
	4 5 6
octave:35> A(:,:) % Returns all rows and all columns
ans = 
   1   2   3
   4   5   6
octave:36> A(:,[1 3],:) % Returns all rows and 1st and 3rd columns
ans =
   1   3
   4   6
octave:37> A(:,2)=[22; 55] % Replace 2nd column with new values
A =
    1   22    3
    4   55    6 
octave:38> A(1,:)=[9 10 11] % Replace 1st row with with new values
A =
    9   10   11
    4   55    6
 octave:39> A = [A,[7;8]] % Add one column vector to right
A =
    9   10   11  7
    4   55    6.  8
 octave:40> A = [[9;10], A] % Add one column vector to left
A =
    9   10   11  7
    4   55    6   8
 octave:41> A(:) % Put all elements in a single column vector
A = 
	9
	10
	11
	7
	4
	55
	6
	8

Inverse & Transpose


The inverse of a matrix is a matrix which when multiplied with the original matrix produces an identity matrix. The transpose of a matrix is an operation that flips the matrix over its diagonal.

Let us see some examples


 octave:43> A = [1 2 3; 4 5 6]
 A = 
     1  2  3
     4  5  6  
 octave:44> B = pinv(A)
  B =
    -0.94444   0.44444
    -0.11111   0.11111
     0.72222  -0.22222
octave:45> A * B
ans = 
    1.00000   0.00000
    0.00000   1.00000
octave:46> A'  % Creates transpose of matrix A
ans =
   1   4
   2   5
   3   6
octave:47> (A')'  
 A = 
   1  2  3
   4  5  6 
octave:48> find(A>2) % Get all elements greater than 2
 A = 
   0  2  3
   4  5  6   
octave:49> A>2 % Element wise comparision and returns 1 or 0 (True of False)
A=
   0   0   1
   1   1   1   


If Statement

if (condition)
  then-body
else
  else-body
endif

Practical Examples


Let us try to solve some mathematical equations using Matrices in Octave


Ex. 1 Consider an example where housing price data where you have a size in sq. ft and price in $

Suppose this data is available in a text file, like the one below



Let us write some Octave commands to load this data in the matrix. In order to load data, your file should be either present in the root of your working directory or you need to use the full path.


You can use Octave CLI or create a function inside the file and make it run. Please note that your file name and function name should be the same. To run this file you simply need to write the name of the file in Octave CLI, this will automatically execute the function inside this file. If you do not want to print statements inside the file, please use a semicolon (;) after each statement. If you want to debug your program and check variable values, then simply do not use a semicolon at the end of each statement.



octave:1> data = load('housingdata.txt')
data =
    260   1000
    280   1200
    340   1500
    450   2000
    550   2800

Octave loads the data into a 2x2 matrix. Let us move this data into 2 different matrix x and y.


In CLI you can execute two commands using,


octave:1> x = data(:,1),y=data(:,2)
x =
   260
   280
   340
   450
   550
y = 
  1000
  1200
  1500
  2000
  2800

Let us see how we can solve the below equation using normal as well as vectorized methods.

Hypothesis Function


# Example


Let us calculate the hypothesis first using the normal method. To calculate the hypothesis for all the examples in matrix x, we need to perform a loop. Let us try this out using Octave however you can use any other language of your choice as far as normal method is concerned. For matrix operations, you need to choose a language that supports such complex matrix operations.

Let us load the data first

function [] = lg()
  data = load('housingdata.txt');
  x = data(:,1);
  y = data(:,2);
  m = size(x,1);
  theta = [0.5,0.5];
end

Now loop through the data to calculate hypothesis, so let us first define a data structure to hold hypothesis values and create a function to calculate hypothesis


function [] = lg()
  data = load('housingdata.txt');
  x = data(:,1);
  y = data(:,2);
  m = size(x,1);
  theta = [0.5,0.5];

  hypothesis = calculateHypothesis(x,y,m,theta(1),theta(2))
end

function[hypothesis] =  calculateHypothesis(x,y,m,theta0,theta1)
  hypothesis = zeros(m,1);
  for(i=1:m)
     hypothesis(i) = theta0 + theta1 * x(i);
  end
end

You will get the following output in the form of a column vector


hypothesis =
   130.50
   140.50
   170.50
   225.50
   275.50

Now consider we have millions of examples and we need to calculate the hypothesis for million records, looping through millions of options will be a time-consuming operation. Let us evaluate the matrix multiplication option. Consider modifying the above hypothesis equation as

So let us add one more column to our matrix x with value 1


tempx = [ones(m,1),x]

Matrix tempx will look like this

tempx =
     1   260
     1   280
     1   340
     1   450
     1   550

Now to calculate the hypothesis in one go, you can apply the formula

hypothesis = x*theta'  

Please use a pen and paper to evaluate this function and see how this works. Now adding one more function for vectorized implementation

function [] = lg()
  data = load('housingdata.txt');
  x = data(:,1);
  y = data(:,2);
  m = size(x,1);
  theta = [0.5,0.5];

  %hypothesis = calculateHypothesis(x,y,m,theta(1),theta(2))
  hypothesis_vector = calculateHypothesisVectorize(x,y,m,theta)
end

function[hypothesis] =  calculateHypothesis(x,y,m,theta0,theta1)
  hypothesis = zeros(m,1);
 for(i=1:m)
 hypothesis(i) = theta0 + theta1 * x(i);
  end
end

function[hypothesis_vector] calculateHypothesisVectorize(x,y,m,theta)
  
  tempx = [ones(m,1),x]
  hypothesis_vector = zeros(m,1);
  hypothesis_vector = tempx*theta';
  
end

We have calculated the hypothesis using two different approaches. In ML we rely on vectorized implementation in order to perform the task quickly and efficiently.


Plotting Graph


Octave also comes with a powerful 2D/3D plotting engine, let us see how to draw graphs using Octave. Plotting data is also very simple, to plot graph for x and y use below code

plotData(x, y);
xlabel('\Size (Sq.Ft)'); ylabel('\Price ($) in 10,000s');

The following graph will be displayed



I would strongly recommend you to try out various octave operations on your own. You can also go through Octave documentation available on octave website https://www.gnu.org/software/octave/index


Happy Reading...

164 views0 comments

Commentaires


bottom of page