Pointer in C

Define Pointer

1
int* ptr;

This declares ptr as a pointer to int

Operators

  • * Operator
    1. Means “Pointer” when assignment and initialization.
    2. Means “Go To” the address it stored.
  • & Operator
    Means “Get Address”.

Usage:

  1. Initialization
1
2
int i=10;
int* ptr = &i;
  1. Assignment
1
2
3
int i = 10;
int *ptr;
ptr = &i;

List Pointer

Let’s take this as an example.
Notice that type of tha array must match that of the pointer.

1
2
int a[10];
int *ptr;

Assign the address of a[0] to pointer ptr.

1
ptr = &a[0];

In C, this could be replaced by:

1
ptr = a;

Also, because elements in an array are continuous:

1
*(ptr+i) == a[i] == *(a+1) // True

Remind that a could not change, but ptr could change.

String Pointer

Malloc Function.

Use GDB to Debug in C

Basic

To begin, load the program:

1
> gbd [program name]

or have core dumped:

1
> gdb [program name] [core file]

Then set breakpoints(as follow) and run the program:

1
> run [parameters]

Breakpoints

Set Breakpoint

Two ways to set break points

  • Set break point by function name:
1
> break [function name]
  • Set break point by line number:
    1
    > break [line number]

Show Breakpoints

1
> ib

or

1
> info breakpoint

Delete breakpoint

To delete all break points:

1
> delete

Debug in Breakpoint

Print

Usage:

1
> print([variable])

Next:

Usage:

1
> next

or

1
> n

Logistic Regression

Linear regression could not yield proper result in classification Problem, because outliers could affect the result.

Logistic Regression is still a classification algorithm, even though it is called regression.

Hypothesis Function

Logistic regression uses the “Sigmoid Function” (or “Logistic Function”) as hypothesis function:

Logistic Function

h_\theta(x)=g(\theta^T x)
g(z) = \frac{1}{1 + e^{-z}}

h_\theta will give us the probability that our output is 1.

h_\theta(x) = P(y=1 | x ; \theta) = 1 - P(y=0 | x ; \theta)

Decision Boundary

Decision boundary is on h_\theta(x) = 0.5 , also \theta^Tx = 0
It is a feature of the trained model (parameters).

Non linear decision boundaries:
Just create new features to fit.

Cost Function

$
J(\theta) = \frac{1}{m} \sum_{i=1}^m \mathrm{Cost}(h_\theta(x^{(i)}),y^{(i)})


\begin{eqnarray} Cost(h_\theta(x),y) = \begin{cases} -\log(h_\theta(x)) & \text{if }y=1 \ -\log(1-h_\theta(x)) & \text{if }y = 0 \end{cases} =-y\log(h_\theta(x)) - (1 - y)\log(1 - h_\theta(x)) \end{eqnarray}
$

Vectorized version:

J(\theta) = -\frac{1}{m}(\log(g(X\theta))^{T}y+\log(1-g(X\theta))^{T}(1-y))

Why not Using Squared Error?

If using squared error on sigmoid function, it will be non-convex, so that we can’t use gradient descent.

Optimization

Derivative for Gradient Descent

It looks identical to linear regression
$
\frac{d}{d\theta_j}J(\theta)=\frac{1}{m}\displaystyle\sum_{i=1}^m{(h_\theta(x^{(i)})-y^{(i)})x^{(i)}_j}

$

$
J(\theta_0,\theta_1,\theta_2, …) = \frac{1}{m}\displaystyle\sum_{i=1}^m{Cost(h_\theta(x^{(i)}), y^{(i)})}+\frac{\lambda}{2m}\displaystyle\sum_{j=1}^n{\theta^2_j}

$

( \lambda is regularization parameter)

Update \theta :
$
\theta_j = \theta_j -\frac{\alpha}{m}\displaystyle\sum_{i=1}^m{(h_\theta(x^{(i)})-y^{(i)})x^{(i)}_j}

$
Vectorized implementation:

\theta = \theta - \frac{\alpha}{m} X^{T} (g(X \theta ) - y)

Points of Markdown

Basic writing

Paragraphs

Paragraphs in Markdown are just one or more lines of consecutive text followed by one or more blank lines.

You can create a heading by adding one or more # symbols before your heading text. The number of # you use will determine the size of the heading.

1
2
3
4
# The largest heading (an <h1> tag)
## The second largest heading (an <h2> tag)

###### The 6th largest heading (an <h6> tag)

Block-quotes

You can indicate block-quotes with a >

1
2
3
In the words of Abraham Lincoln:

> Pardon my French

Styling text

1
2
*This text will be italic*
**This text will be bold**

Both bold and italic can use either a * or an _ around the text for styling. This allows you to combine both bold and italic if needed.

1
**Everyone _must_ attend the meeting at 5 o'clock today.**

Lists

Unordered lists

You can make an unordered list by preceding list items with either a * or a -.

1
2
3
4
5
* Item
* Item

- Item
- Item

Ordered lists

You can make an ordered list by preceding list items with a number.

1
2
1. Item 1
2. Item 2

Nested lists

You can create nested lists by indenting list items by two spaces.

1
2
3
4
5
1. Item 1
1. A corollary to the above item.
2. Item 2
* A corollary that does not need to be ordered.
* This is indented four spaces.

Code syntax highlighting

In-line formats

Use single back-ticks (`) to format text in a special mono-space format. Everything within the back-ticks appear as-is, with no other special formatting.

1
Here's an idea: why don't we take `SuperiorProject` and turn it into `**Reasonable**Project`.

Multiple lines

You can use triple back-ticks (```) to format text as its own distinct block.

1
2
3
4
``` 
x = 0
x = 2 + 2
```

Syntax highlighting

In fenced block, add an optional language identifier for syntax highlighting.
For example, to syntax highlight Ruby code:

1
2
3
4
5
```ruby
require 'redcarpet'
markdown = Redcarpet.new("Hello World!")
puts markdown.to_html
```

You can create an in-line link by wrapping link text in brackets ( [ ] ), and then wrapping the link in parentheses ( ( ) ).

For example, to create a hyper-link to www.github.com, with a link text that says, Visit GitHub!, you’d write this in Markdown: [Visit GitHub!](https://www.github.com).

Image

In-line image:

1
![Alt Msg](URL)

Block image:

1
![Alt Msg][URL]

GitHub Flavored Markdown

underscores in words

Where Markdown transforms underscores (_) into italics, GFM ignores underscores in words. To emphasize a portion of a word, use asterisks (*).

URL auto-linking

GFM will auto-link standard URLs, so if you want to link to a URL (instead of setting link text), you can simply enter the URL and it will be turned into a link to that URL.

1
http://example.com

becomes: http://example.com

Strike-through

GFM adds syntax to create strike-through text, which is missing from standard Markdown.

1
~~Mistaken text.~~

becomes: Mistaken text.

Tables

You can create tables by assembling a list of words and dividing them with hyphens - (for the first row), and then separating each column with a pipe |:

1
2
3
4
First Header  | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell

You can also add extra pipes on the ends:

1
2
3
4
| First Header  | Second Header |
| ------------- | ------------- |
| Content Cell | Content Cell |
| Content Cell | Content Cell |

Dashes don’t need to match the length of text:

1
2
3
4
| Name | Description          |
| ------------- | ----------- |
| Help | Display the help window.|
| Close | Closes a window |

Finally, by including colons : within the header row, you can define text to be left-aligned, right-aligned, or center-aligned:

1
2
3
4
5
| Left-Aligned  | Center Aligned  | Right Aligned |
| :------------ |:---------------:| -----:|
| col 3 is | some wordy text | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |

A colon on the left-most side indicates a left-aligned column; a colon on the right-most side indicates a right-aligned column; a colon on both sides indicates a center-aligned column.

Math knowledge about Matrix

Basic

\begin{bmatrix} a & b \\ c & d \\ e & f \end{bmatrix}

A matrix has 3 rows and 2 columns, so it is a 3x2 matrix.

  • A_{ij} refers to the element in the i th row and j th column of matrix A
  • v_i refers to the element in the i th row of the vector.
  • In general, all our vectors and matrices will be 1-indexed.
  • Matrices are usually denoted by uppercase names while vectors are lowercase.
  • “Scalar” means that an object is a single value, not a vector or matrix.
  • \mathbb{R} refers to the set of scalar real numbers.
  • \mathbb{R}^n refers to the set of n-dimensioned vectors of real numbers

Addition

Matrix dimensions must agree
Plussing is just add each coordinate numbers together.(Minus as well)

\begin{bmatrix} a & b \\ c & d \\ \end{bmatrix} + \begin{bmatrix} w & x \\ y & z \\ \end{bmatrix} = \begin{bmatrix} a+w & b+x \\ c+y & d+z \\ \end{bmatrix}

Multiplication

Matrix * number

Plussing is just add each coordinate numbers together.(Minus as well)

\begin{bmatrix} a & b \\ c & d \end{bmatrix} * x = \begin{bmatrix} ax & bx \\ cx & dx \end{bmatrix}

Matrix * vector

\begin{bmatrix} a & b \ c & d \ e & f \end{bmatrix}* \begin{bmatrix} x\ y \end{bmatrix} = \begin{bmatrix} ax + by\\ cx + dy\\ ex + fy \end{bmatrix}

$
([mn][n1] = [m1])
$

Matrix * matrix

\begin{bmatrix} a & b \\ c & d \newline e & f \end{bmatrix} * \begin{bmatrix} w & x \newline y & z \newline \end{bmatrix} = \begin{bmatrix} aw + by & ax + bz \newline cw + dy & cx + dz \newline ew + fy & ex + fz \end{bmatrix}

([m*n] * [n*o] = [m*o])

Warnings

Two properties of matrix multiple:

  • Not commutative. A*B \neq B*A
  • Associative. (A*B)*C = A*(B*C)

Inverse & transpose

Inverse

The inverse of a matrix A is denoted A^{-1} . Multiplying by the inverse results in the identity matrix.
A non square matrix does not have an inverse matrix. We can compute inverses of matrices in octave with the pinv(A) function.

Invertibility:

Transpose

A = \begin{bmatrix} a & b \newline c & d \newline e & f \end{bmatrix}

A^T = \begin{bmatrix} a & c & e \newline b & d & f \newline \end{bmatrix}

In other words:

A_{ij} = A^T_{ji}

Points of Git

Clone

Branch

List branches

1
2
3
> git branch -r // remote branches

> git branch -a // all branches

Commit

1
> git commit

Merge

Pull

1
> git pull <远程主机名> <远程分支名>:<本地分支名>

Push

1
> git push <远程主机名> <本地分支名>:<远程分支名>

Fetch

1
> git fetch <远程主机名> <分支名>

Checkout

1
> git checkout

Points of Matlab

Control Flow

for loop

1
2
3
for x= [initVal:step:endVal or valArray]
% body
end

Basic Calculation

Inequity Note, not “!=”

1 ~= 2  % true

Variable assignment:

a = 3; % semicolon suppresses output

Displaying them:

1
2
3
4
5
6
7
8
a = pi
disp(a)
disp(sprintf('2 decimals: %0.2f', a))
disp(sprintf('6 decimals: %0.6f', a))
format long
a
format short
a

Matrix Calculation

1. To create a matrix:

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

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

Or using a built-in function

1
2
3
4
5
ones(row, col)  % same as C = [2 2 2; 2 2 2]
zeros(row, col)
rand(row, col) % from a uniform distribution (range [0, 1])
randn(row, col) % from a normal distribution (mean=0, var=1)
eye(row) % 4x4 identity matrix

2. Get informations from matrix

1
2
3
4
size(M)    % return a 1x2 matrix: [(number of rows) (number of columns)]
size(M,1) % number of rows
size(M,2) % number of cols
length(M) % size of longest dimension

3. Manipulating matrix

1
2
3
4
5
6
7
8
9
10
11
12
13
%% indexing
A(3,2) % indexing is (row,col)
A(2,:) % ":" means every element along that dimension
A([1 3],:) % print all the elements of rows 1 and 3

A(:,2) = [10; 11; 12] % change second column
A = [A, [100; 101; 102]]; % append column vec
A(:) % Select all elements as a column vector.

% Putting data together
% (A & B are matrics posessing same dims)
C = [A B] or [A,B] % concatenating A and B matrices side by side
C = [A; B] % Concatenating A and B top and bottom

Plotting

1
2
3
w = -6 + sqrt(10)*(randn(1,10000))  % (mean = -6, var = 10)
hist(w) % plot histogram using 10 bins (default)
hist(w,50) % plot histogram using 50 bins

Points of C

Basic

Head Part

1
2
#include <stdio.h> // from system
#include "xx.h" // from current folder

Loops

For loops

Usage:

1
2
3
4
for (init; condition; increment)
{
...
}

Tips:

  1. In “init” part, remember to create a new variable.
  2. Start with 0, use i<n to loop n times.

While loops and do-while loops

Usage:

1
2
3
4
5
6
7
8
9
while(condition)
{
...
}

do
{
...
} while (condition);

Array

To create an array

1
int a[4];

Initialization

  1. You could initialize the elements partly: when elements provided is less than those in the array, only former elements were assigned.
1
int a[10]= {0, 1, 2};
  1. You could only assign elements one by one even assigning same value:
1
int a[10]= {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  1. Not providing the length is allowed:
1
2
3
// these two are equal
int a[]={1,2,3,4,5};
int a[5]={1,2,3,4,5};

Pointer

Click for more about pointer in c

Struct

Click for more about struct in c

Linear Regression

Linear regression is used when predicting numeric output from numeric input that has linear relationship.

Hypothesis Function

x= \begin{bmatrix} x_0\\ x_1\\ …\\ x_n \end{bmatrix}\in \mathbb{R}^{n+1}, \theta= \begin{bmatrix} \theta_0\\ \theta_1\\ …\\ \theta_n \end{bmatrix}\in \mathbb{R}^{n+1}

h_\theta(x) = \theta_0 + \theta_1 x_1 + \theta_2 x_2 … = \theta^Tx
For convenience, define x_0=1 (to use matrix for calculation).

Cost Function

Squared Error

$
J(\theta) = \frac{1}{m}\displaystyle\sum_{i=1}^m{Cost(h_\theta(x^{(i)}), y^{(i)})}

$

$
Cost(h_\theta(x), y)=\frac{1}{2}(h_\theta(x)-y)^2

$

Optimization

Derivative for Gradient Descent

$
\frac{d}{d\theta_j}J(\theta)=\frac{1}{m}\displaystyle\sum_{i=1}^m{(h_\theta(x^{(i)})-y^{(i)})x^{(i)}_j}

$

Update \theta :
$
\theta_j = \theta_j -\frac{\alpha}{m}\displaystyle\sum_{i=1}^m{(h_\theta(x^{(i)})-y^{(i)})x^{(i)}_j}

$

Vectorized implementation:

\theta = \theta - \frac{\alpha}{m} X^{T} (X \theta - y)

Normal equation

See Normal Equation for Linear Regression