Typewind

Coraje y Corazón


  • Home

  • About

  • Archives

  • Tags

  • Books

  • Games

2 Methods of Calulating the Distance between 2 Vectors

Posted on 2017-03-21 |

These days I’m coding with the K-means clustring. In this algorithm, we often need to calculate the distance between 2 vectors. The expression of disctace between vector x and y is:
$$ d=\sqrt{\sum{(x{i}-y{i})^{2}}} $$

Depend on this expression, we can write our code as:

1
2
3
4
5
import numpy as np
x=np.array([1,2,3,4])
y=np.array([0,1,0,1])
d = np.sqrt(((x-y)**2).sum())
print(d)

This code works well, but we can also use the numpy.linalg.norm to calculate the distance:

1
2
3
4
5
import numpy as np
x=np.array([1,2,3,4])
y=np.array([0,1,0,1])
d = np.linalg.norm(x-y)
print(d)

For those guys who are struggling with the math, maybe the norm is a better choice ;)

Actually numpy.linalg.norm is very powerful. It can calculate different kind of normalisations by giving different value to the ord parameter. Here is the doc of norm

L2 Normalisation

Posted on 2017-03-20 |

L2 norm is $$ L{2}=\sqrt {\sum{1}^{i} (x_i^2)} $$
For example, if x = (1,-1,2) the L2 norm of x is $$\sqrt{(1+1+4)} = \sqrt{6} $$

By dividing x’s each element from L2 we normalise x into unit L2
$$ \frac{x{i}}{L{2}}, \forall x $$
For example after normalisation, $$ x=(\frac{1}{\sqrt{6}},\frac{-1}{\sqrt{6}}, \frac{2}{\sqrt{6}}) $$

Sparse Matrix (CSR Model)

Posted on 2017-03-19 |

CSR Sparse Matrix

In CSR model, a m*n sparse matrix is organised by 3 vectors:
(i is both the index of the indptr vector and the index of the row )

  • indptr: The indptr vector is a record that how to read the data from indices and data
  • indices: The column index of all non-zero numbers in each row. Column indices for row i are stored in indices[indptr[i]:indptr[i+1]]. (Read the indptr[i] but not read the [indptr[i+1]])
  • data: the corresponding data of all non-zero numbers in each row. The data of row i are stored in indices[indptr[i]:indptr[i+1]]. (Read the indptr[i] but not read the [indptr[i+1]]).
    Noticed that:
  1. The length of the indptr vector is equal to the (number of rows - 1)

    1
    m=len(indptr)-1
  2. The lenght of data is equal to the length of the indices

    1
    len(indices)==len(data)
Read more »

Pre-match banner for Tiago

Posted on 2017-03-14 |

Illustrated as the banner of the game Atletico vs Leverkusen.
Tiago
Match Thread (in Chinese)

Ant System and TSP

Posted on 2017-03-14 |

Ant System

Formulas

$$ P{ij}=\frac{[\tau{ij}(t)]^\alpha[\eta{ij}]^\beta}{\sum{k\in Ni}[\tau{ij}(t)]^\alpha[\eta_{ij}]^\beta} $$

  • $$ \tau_{ij}(t) $$ : The amount of pheromon on the arc(i, j) at time t
  • $$ \eta_{ij} $$ : The heuristic value of moving from i to j.
  • $$ \eta{ij}=Q/d{ij} $$, Q is a real number. Some books/papers set Q=1, but in the tutorial Q=100. Q can arrange the weight of the distance when compute the posibility.
  • $$ \alpha, \beta $$
  • To be continiued *

Using Hexo MathJax and Writing LaTex Equations

Posted on 2017-03-12 |

Install

Hexo supports the TeX or the LaTex by plugin MathJax. We only need 3 steps to install it:

  1. npm install hexo-math --save
  2. cd to the dictionary of your local blog, input:hexo math install
  3. Add plugins: hexo-math into the _config.yml
    Read more »

Markdown Grammar and Useful Package and Skills

Posted on 2017-03-12 |

How to use Markdown

Markdown (MD) is a simple markup language. The grammar of MD is briefer and easier than HTML/XML. To be fair, everyone can understand how to use MD in 10 minutes. :) That’s why MD is becoming more and more popular in nearly years.

Actually there are lots of perfect tutorials about Markdown and this one might be the worst one. I write this just for my own practice. Hope it can help. ;P

The official site of Markdown is here

Read more »

安赫尔-科雷亚——即使不曾有过翅膀

Posted on 2017-03-11 |

Imgur
对于一个球员,或者希望自己前程似锦的人们来说,“实在是因为运气不好”这个理由,可以算是最稀奇的理由了。通常来说,我们喜欢上一个球员,大抵是因为他是我们主队的明星,或是因为我们欣赏他踢球的风格,但有时,忠诚与偏爱都是次要的。有时,球员们那不堪回首的黑历史,会让他的其他一切都黯然失色,融入背景,再不见痕迹。

而其中的一些黑历史则会让人觉得,他们经历了这么多的苦难,理应获得成功。不让他们享受到无上的荣光,则会显得天道不公。

而马竞的新人,安赫尔-科雷亚(Angel Correa),就是这样的一名球员。

Read more »

Hello World

Posted on 2017-03-11 |

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

1…67
Shuyao Chen

Shuyao Chen

Computer Science, Football and ACG

69 posts
89 tags
Weibo Twitter Linkedin
Total Visitor:
© 2022 Shuyao Chen
Powered by Hexo
Theme - NexT.Mist