Machine Learning (Andrew Ng) @ Coursera その4


自学自習でOctaveの設定変更してみた。
Octave起動するといろいろ設定がdefault臭してすごく効率が悪い。
こういったときは .bashrc ならぬ、 octaverc なるものがあるんですわ。
この設定ファイルは、パス的には以下になります。
( 私のインストール先はCドライブ直下なのでその辺は読み替えて下さい。 )
 C:\Octave\3.2.4_gcc-4.4.0\share\octave\site\m\startup


で、2つの欲望を叶えるためoctavercを書き換えます。

1つは、起動時から自分の作業ディレクトリに飛びたい
2つは、糞長いプロンプトをPython風に ">>" にしたい

夢を叶えるスクリプトは以下のとおり。

## System-wide startup file for Octave.
##
## This file should contain any commands that should be executed each
## time Octave starts for every user at this site.
cd D:\Octave # Move to working directory
PS1(">>") # Change prompt

全然難しくないですね。


Plotting Data


clear;
t  = [0:0.01:0.98];
y1 = sin(2*pi*4*t);
y2 = cos(2*pi*4*t);
plot(t,y1);
hold on; # hold figures to overlap plotting
plot(t,y2, "r");
xlabel("time");
ylabel("value");
legend("sin", "cos");
title("my plot");
close;
cd "data"
print -dpng "myPlot.png" # png is file format
cd ".."
figure(1); plot(t, y1);
figure(2); plot(t, y2);
subplot(1,2,1); # Divides plot 1×2 grid, access first element
plot(t,y1);
subplot(1,2,2); # Divides plot 1×2 grid, access first element
plot(t,y2);
axis([0.5 1 -1 1]);
clf;
A = magic(5);
imagesc(A);
imagesc(A), colorbar, colormap gray; # three commands at the same time called comma chaining

Control Statements

v = zeros(10,1);
for i=1:10,
    v(i) = 2^i;
end;
indices = 1:10;
for i=indices,
    disp(i);
end;
i = 1;
while i <= 5,
    v(i) = 100;
    i = i+1;
end;
i=1;
while true,
    v(i) = 999;
    i=i+1;
    if i == 6,
        break;
    end;
end;
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 or two.");
end;
# exit # quit octave
# Add search path
addpath('D:\Octave\lib'); # don't use double quatation
squareThisNumber(5)

# cost function
X = [1 1; 1 2; 1 3]; # design matrix
y = [1; 2; 3]; # y axis values
theta = [0; 1];
j =  costFunctionJ(X,y,theta);

Vectorization



シグマ記号で定式化するとついついforループ回しがち。
ベクトル表記して内積表現すればOctaveが好きなカタチで処理してくれるんで速いよという話。
Highly optimized な linear algebraのライブラリが内部的に呼ばれるのでウンタラカンタラ。


# バカのやること 
clear;

tmp = 0;
for j=0:n
    tmp = tmp + theta(j)*x(j);
end;

# Vectorization
theta' * x;







コメント

このブログの人気の投稿

Callback関数を知らん人がまず理解すべきことのまとめ。

C言語でBluetoothスタックを叩きたい人のBluetooth開発入門その1

C++プログラミング入門(1) // 倉庫番プログラムの実装