Using R to Measure Distance

1.question description:

using R code to calculate the distance, such as “euclidean” “chebyshev” “cosine”

2.solutions :

using the key words “R package” and “distance” searching for the packages, and
we got the package of philentropy

2.1.code :

#install package in console
install.packages("philentropy")
#load the package
library(philentropy)
#define the vectors needed calculating
v1 <- (1,3,4)
v2 <- (2,3,2)
#calculate with the default parameters
#manhattan distance,euclidean,cosine,lin.cor(pearson)
distance(x, method = "manhattan")
distance(x, method = "euclidean")

2.2.output :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Metric: 'manhattan'; comparing: 2 vectors.
manhattan
3
> distance(x, method = "euclidean")
Metric: 'euclidean'; comparing: 2 vectors.
euclidean
2.236068
> distance(x, method = "chebyshev")
Metric: 'chebyshev'; comparing: 2 vectors.
chebyshev
2
> distance(x, method = "cosine")
Metric: 'cosine'; comparing: 2 vectors.
cosine
0.9037378
> lin.cor(A,B)
pearson
0.1889822

3.supplyment materials:

the difference between different type of distance

3.1. euclidean

https://en.wikipedia.org/wiki/Euclidean_distance

In mathematics, the Euclidean distance between two points in Euclidean space is a number, the length of a line segment between the two points. It can be calculated from the Cartesian coordinates of the points using the Pythagorean theorem, and is occasionally called the Pythagorean distance.

3.2. cosine distance

cosine distance (or cosine similarity, angular cosine distance, angular cosine similarity) between two variables.
https://en.wikipedia.org/wiki/Cosine_similarity

Cosine similarity is a measure of similarity between two non-zero vectors of an inner product space. It is defined to equal the cosine of the angle between them, which is also the same as the inner product of the same vectors normalized to both have length 1. The cosine of 0° is 1, and it is less than 1 for any angle in the interval (0, π] radians. It is thus a judgment of orientation and not magnitude: two vectors with the same orientation have a cosine similarity of 1, two vectors oriented at 90° relative to each other have a similarity of 0, and two vectors diametrically opposed have a similarity of -1, independent of their magnitude. The cosine similarity is particularly used in positive space, where the outcome is neatly bounded in {\displaystyle [0,1]}[0,1].

3.3. cosine distance

https://medium.com/swlh/is-correlation-distance-a-metric-5a383973978f#:~:text=Correlation%20distance%20is%20a%20popular,as%20d%3D1%2Dr.

Correlation distance is a popular way of measuring the distance between two random variables with finite variances¹. If the correlation² between two random variables is r, then their correlation distance is defined as d=1-r. However, a proper distance measure needs to have a few properties, i.e. should be a metric, and it is not trivial whether correlation distance has these properties. In this note, we ask whether correlation distance is a metric or not.

DS_1_1_Recursion

数据结构_chapter 1_1.1_递归(recursion)

1. 递归实现阶乘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>

double factorial(unsigned int number) //数据类型 double 可用于阶乘运算较宽数值范围乘积的存储
{
if(number<=0){ //此处应修改为: number<=1 ,若为 number<=0, 则会重复一次乘1操作
return 1;
}
return number*factorial(number-1);
}
int main()
{
unsigned int number;
scanf("%d",&number);
printf("%d的阶乘结果为:%f\n",number,factorial(number));
return 0;
}
2. 递归实现斐波那契数列前 ‘n’ 项输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
int fibonaci(int i)
{
if(i==1) //数列第一项
{
return 0;
}
if(i==2) //数列第二项
{
return 1;
}
return fibonaci(i-1) + fibonaci(i-2);
}

int main()
{
int number,i;
scanf("%d",&number);
for(i=1;i<=number;i++)
{
printf("%d\t\n",fibonaci(i));
}
return 0;
}
3. 递归实现从’1~n’整型数据顺序输出

非递归方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>

void PrintN(int N)
{
int i;
for(i=1;i<=N;i++)
printf("%d\n",i);
return;
}

int main()
{
int N;
scanf("%d",&N);
PrintN(N);
return 0;
}

递归方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
void PrintN(int N)
{
if(N>0){
PrintN(N-1);
printf("%d\n",N);
}
}
int main()
{
int N;
scanf("%d",&N);
PrintN(N);
return 0;
}

or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
void PrintN(int N);
int main()
{
int N;
scanf("%d",&N);
PrintN(N);
return;
}
void PrintN(int N)
{
if(N>0){
PrintN(N-1);
printf("%d\n",N);
}
}

6_array_data_processing_2

6.数组处理批量数据

6.4 将整型数组元素行列位置对调
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<stdio.h>

int main()
{
int a[2][3]={{1,2,3},{4,5,6}};
int b[3][2],i,j;

printf("array a:\n");
for(i=0;i<=1;i++) //遍历二维数组a 元素并赋值于b;遍历行
{
for (j=0;j<=2;j++) //遍历数组a 列
{
printf("%5d",a[i][j]);
b[j][i]=a[i][j]; //数组a中元素与数组b中 元素对调(行、列)
}
printf("\n");
}
printf("array b:\n");
for(i=0;i<=2;i++) //遍历数组b,循环结构for遍历行
{
for(j=0;j<=1;j++) //遍历列
printf("%5d",b[i][j]);
printf("\n");
}
return 0;
}
6.5 将整型数组中最大值元素及相应行列位置输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
int main()
{
int i,j,row=0,colum=0,max;
int a[3][4]={{1,2,3,4},{9,8,7,6},{-10,10,-5,2}};
max=a[0][0];
for(i=0;i<=2;i++) //数组遍历,行遍历
for(j=0;j<=3;j++) // 数组遍历,列遍历
if(a[i][j]>max) //“打擂台”算法
{max=a[i][j];
row=i;
colum=j;
}
printf("max=%d\nrow=%d\ncolum=%d\n",max,row,colum); //最大值输出、行/列号输出
return 0;
}
6.6 字符数组输出特定字符
1
2
3
4
5
6
7
8
9
10
#include<stdio.h>
int main()
{
char c[15]={'I',' ','a','m',' ','a',' ','s','t','u','d','e','n','t','.'};
int i;
for(i=0;i<15;i++)
printf("%c",c[i]);
printf("\n");
return 0;
}
6.7 用星号(*)输出钻石图案
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
int main()
{
char diamond[][5]={{' ',' ','*'},{' ','*',' ','*',' '},{'*',' ',' ',' ','*'},{' ','*',' ','*',' '},{' ',' ','*',' ',' '}}; //定义字符数组元素初始值,5行5列
int i,j;
for(i=0;i<5;i++) //遍历数组元素,行遍历
{
for(j=0;j<5;j++) //遍历数组元素,列遍历
printf("%c",diamond[i][j]);
printf("\n");
}
return 0;
}

6_array_data_processing

6.数组处理批量数据

6.1 为数组赋值0~9,并将数组元素逆序输出
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>

int main()
{
int i,a[10]; //定义整型变量i ,整型数组a
for(i=0;i<=9;i++) //循环for为数组元素赋值
a[i]=i;
for(i=9;i>=0;i--) //循环for 将数组元素逆序输出
printf("%d",a[i]);
printf("\n");
return 0;
}
6.2 输出斐波那契数列第2~21项(斐波那契数列:0,1,1,2,3,5…)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>

int main()
{
int i;
int f[20]={1,1}; //定义整型数组,第1,2项均为1

for(i=2;i<20;i++) //循环结构for为整型数组第3至20项赋值
f[i]=f[i-2]+f[i-1];

for(i=0;i<20;i++)
{
if(i%5==0) printf("\n");
printf("%12d",f[i]); //输出数组元素值,每数值占12列且右对齐
}
printf("\n");
return 0;
}

6.3 自定义10个数值,将数值按升序形式输出(冒泡排序法)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>

int main()
{
int a[10];
int i,j,t;
printf("input 10 numbers :\n");
for(i=0;i<10;i++) //循环结构for 读取数据并存储
scanf("%d",&a[i]);
printf("\n");
for(j=0;j<9;j++) //重复趟数总计9次,遍历每趟
for(i=0;i<9-j;i++) // 每趟内遍历数组元素
if(a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;} //条件判断,若前一项大于后一项,进行数值交换
printf("the sorted numbers :\n");
for(i=0;i<10;i++) //循环结构输出sorted 后的数组元素
printf("%d",a[i]);
printf("\n");
return 0;
}

Hexo_github_搭建个人博客

个人博客搭建(github + nodejs + hexo + theme (3-Hexo)):

1.git 下载与安装

git 简介:

Git (/ɡɪt/)is a distributed version-control system(分布式版本控制系统) for tracking changes in source code during software development.It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows[clarification needed].

The reasons why we use git

git 下载与安装:

you can download : website

the installtion just follows the defalut.

git 使用:

git 中文教程: 廖雪峰git 教程

2.Nodejs 下载与安装

nodejs 简介:

As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications. In the following “hello world” example, many connections can be handled concurrently. Upon each connection, the callback is fired, but if there is no work to be done, Node.js will sleep.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

nodejs 下载与安装:

根据操作系统选择相应 ‘source code’ 或者 ‘pre-built installer’
安装过程保持默认设置即可

nodejs 使用:

官方手册 documentation

3.Hexo 下载与安装

Hexo 简介:

Hexo is a fast, simple and powerful blog framework. You write posts in Markdown (or other markup languages) and Hexo generates static files with a beautiful theme in seconds.

Hexo 下载与安装:
3.1. 命令行安装
1
2
npm install hexo-cli -g

windows 系统可能出现报错: win32 不支持 fsevents , SKIPPING OPTIONAL DEPENDENCY, 不影响 hexo 基本功能
json optional dependencies

1
2
3
4
WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.1.2 (node_modules\hexo-cli\node_modules\chokidar\node_module
s\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any
"} (current: {"os":"win32","arch":"x64"})
3.2.检查 hexo 是否已经安装成功:
1
hexo version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
hexo-cli: 4.2.0
os: Windows_NT
node: 12.18.4
v8: 7.8.279.23-
uv: 1.38.0
zlib: 1.2.11
brotli: 1.0.7
ares: 1.16.0
modules: 72
nghttp2: 1.41.0
napi: 6
llhttp: 2.1.2
http_parser: 2.
openssl: 1.1.1g
cldr: 37.0
icu: 67.1
tz: 2019c
unicode: 13.0
3.3.初始化(文件克隆 hexo-starter、Submodule themes/landscape、;安装依赖)
1
hexo init username.github.io

初始化成功提醒:

1
INFO  Start blogging with Hexo!
3.4.测试 本地服务 是否可以启动
1
hexo server

server 成功提醒INFO :

1
2
3
4
INFO  Validating config
INFO Start processing
INFO Hexo is running at http://localhost:4000 . Press Ctrl+C to stop.
INFO Bye!
3.5. 生成 SSH Keys, 并添加至 github, 测试 本地与 github 通信是否成功
1
ssh-keygen -t rsa -C "***@***.com"

KEY 添加方法 : Github –> settings —> SSH keys setting

通信测试 :

1
ssh -T git@github.com
1
Hi ***! You've successfully authenticated, but GitHub does not provide shell access.

or

1
$ ssh git@github.com
1
2
Hi ******! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
3.6. 配置 SSH 服务 、_config.yml文件 : 如未配置,则每次 hexo deploy 均需要输入用户名及密码:
1
2
$ git config --global user.name "******"// github用户名
$ git config --global user.email "******"// github注册邮箱

_config.yml文件更改为:

1
2
3
4
deploy:
type: git
repository: git@github.com:username/username.github.io.git
branch: master

不当写法:此种写法为 hexo2.* 版本书写规则,执行 hexo d 时会导致报错 Deployer not found: github or Deployer not found: git , 可通过安装插件解决 npm install hexo-deployer-git --save

1
2
3
4
deploy:
type: github
repository: https://github.com/username/username.github.io.git
branch: master
4. 域名解析:

添加域名解析后,可使个性化域名指向博客 。
为域名添加两条记录:
主机记录(@ ; www); 记录类型(A ; CNAME); 解析线路 (默认 ; 默认) ; 记录值 ( IP ; ***.github.io) ; TTL (10min ;10min)

IP 获取方式 :

1
ping ***.github.com  
5. Hexo 使用教程(主题更换):

官方教程
3-hexo yelog

You need to set client_id and slot_id to show this AD unit. Please set it in _config.yml.