Skip to content

Commit ade30ab

Browse files
authored
Add files via upload
1 parent 0c0c309 commit ade30ab

File tree

97 files changed

+154173
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+154173
-0
lines changed

Book6_Ch02_Python_Codes/Bk6_Ch02_01.ipynb

Lines changed: 652 additions & 0 deletions
Large diffs are not rendered by default.

Book6_Ch03_Python_Codes/Bk6_Ch03_01.ipynb

Lines changed: 694 additions & 0 deletions
Large diffs are not rendered by default.

Book6_Ch03_Python_Codes/Bk6_Ch03_02.ipynb

Lines changed: 285 additions & 0 deletions
Large diffs are not rendered by default.

Book6_Ch04_Python_Codes/Bk6_Ch04_01.ipynb

Lines changed: 676 additions & 0 deletions
Large diffs are not rendered by default.

Book6_Ch04_Python_Codes/Bk6_Ch04_02.ipynb

Lines changed: 157 additions & 0 deletions
Large diffs are not rendered by default.

Book6_Ch04_Python_Codes/Bk6_Ch04_03.ipynb

Lines changed: 250 additions & 0 deletions
Large diffs are not rendered by default.

Book6_Ch04_Python_Codes/Bk6_Ch04_04.ipynb

Lines changed: 157 additions & 0 deletions
Large diffs are not rendered by default.

Book6_Ch04_Python_Codes/Bk6_Ch04_05.ipynb

Lines changed: 120 additions & 0 deletions
Large diffs are not rendered by default.

Book6_Ch04_Python_Codes/Bk6_Ch04_06.ipynb

Lines changed: 207 additions & 0 deletions
Large diffs are not rendered by default.

Book6_Ch04_Python_Codes/Bk6_Ch04_07.ipynb

Lines changed: 183 additions & 0 deletions
Large diffs are not rendered by default.

Book6_Ch04_Python_Codes/Bk6_Ch04_08.ipynb

Lines changed: 116 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "38ebe56d-5a05-484f-9db6-4d9a9ca4b432",
6+
"metadata": {},
7+
"source": [
8+
"Chapter 05\n",
9+
"\n",
10+
"# 欧氏距离\n",
11+
"Book_6《数据有道》 | 鸢尾花书:从加减乘除到机器学习"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"id": "8fd2e18c-6578-4eb3-a40d-9781282d55fe",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"from scipy.spatial import distance\n",
22+
"import numpy as np"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 2,
28+
"id": "480dd9e7-27fe-44ea-a5d1-b012d23155fd",
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"x_i = (0, 0, 0) # data point\n",
33+
"q = (4, 8, 6) # query point"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 3,
39+
"id": "41f20fe9-4b51-4d76-8d0b-27390500ee30",
40+
"metadata": {},
41+
"outputs": [],
42+
"source": [
43+
"# calculate Euclidean distance\n",
44+
"dst_1 = distance.euclidean(x_i, q)\n",
45+
"\n",
46+
"dst_2 = np.linalg.norm(np.array(x_i) - np.array(q))"
47+
]
48+
}
49+
],
50+
"metadata": {
51+
"kernelspec": {
52+
"display_name": "Python 3 (ipykernel)",
53+
"language": "python",
54+
"name": "python3"
55+
},
56+
"language_info": {
57+
"codemirror_mode": {
58+
"name": "ipython",
59+
"version": 3
60+
},
61+
"file_extension": ".py",
62+
"mimetype": "text/x-python",
63+
"name": "python",
64+
"nbconvert_exporter": "python",
65+
"pygments_lexer": "ipython3",
66+
"version": "3.10.9"
67+
}
68+
},
69+
"nbformat": 4,
70+
"nbformat_minor": 5
71+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "68587ff0-cc9d-4a4c-9ca0-df46c20839ba",
6+
"metadata": {},
7+
"source": [
8+
"Chapter 05\n",
9+
"\n",
10+
"# 成对欧氏距离\n",
11+
"Book_6《数据有道》 | 鸢尾花书:从加减乘除到机器学习"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"id": "22d75b52-c37a-437d-8b74-3897be0e5536",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"from sklearn.metrics.pairwise import euclidean_distances\n",
22+
"\n",
23+
"# Sample data points\n",
24+
"X = [[-5, 0], [4, 3], [3, -4]]\n",
25+
"\n",
26+
"# Query point\n",
27+
"q = [[0, 0]]"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 2,
33+
"id": "f6e4443f-b711-44d7-805e-e37071f2f83c",
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"name": "stdout",
38+
"output_type": "stream",
39+
"text": [
40+
"Pairwise distances between X and q\n",
41+
"[[5.]\n",
42+
" [5.]\n",
43+
" [5.]]\n"
44+
]
45+
}
46+
],
47+
"source": [
48+
"# pairwise distances between rows of X and q\n",
49+
"dst_pairwise_X_q = euclidean_distances(X, q)\n",
50+
"print('Pairwise distances between X and q')\n",
51+
"print(dst_pairwise_X_q)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 3,
57+
"id": "d5b436f6-6a6b-42fc-85f0-2f4264125888",
58+
"metadata": {},
59+
"outputs": [
60+
{
61+
"name": "stdout",
62+
"output_type": "stream",
63+
"text": [
64+
"Pairwise distances between X and X\n",
65+
"[[0. 9.48683298 8.94427191]\n",
66+
" [9.48683298 0. 7.07106781]\n",
67+
" [8.94427191 7.07106781 0. ]]\n"
68+
]
69+
}
70+
],
71+
"source": [
72+
"# pairwise distances between rows of X and itself\n",
73+
"dst_pairwise_X_X = euclidean_distances(X, X)\n",
74+
"print('Pairwise distances between X and X')\n",
75+
"print(dst_pairwise_X_X)"
76+
]
77+
}
78+
],
79+
"metadata": {
80+
"kernelspec": {
81+
"display_name": "Python 3 (ipykernel)",
82+
"language": "python",
83+
"name": "python3"
84+
},
85+
"language_info": {
86+
"codemirror_mode": {
87+
"name": "ipython",
88+
"version": 3
89+
},
90+
"file_extension": ".py",
91+
"mimetype": "text/x-python",
92+
"name": "python",
93+
"nbconvert_exporter": "python",
94+
"pygments_lexer": "ipython3",
95+
"version": "3.10.9"
96+
}
97+
},
98+
"nbformat": 4,
99+
"nbformat_minor": 5
100+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "5626dae8-008d-441e-bd76-a799a4bb15ba",
6+
"metadata": {},
7+
"source": [
8+
"Chapter 05\n",
9+
"\n",
10+
"# 标准欧氏距离\n",
11+
"Book_6《数据有道》 | 鸢尾花书:从加减乘除到机器学习"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"id": "c42a0dbd-3aec-4d40-8445-20f1540959d2",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"from scipy.spatial import distance\n",
22+
"import numpy as np\n",
23+
"\n",
24+
"# Variance-covariance matrix\n",
25+
"SIGMA = np.array([[2, 1], [1, 2]])\n",
26+
"\n",
27+
"q = [0, 0]; # query point\n",
28+
"x_1 = [-3.5, -4]; # data point 1\n",
29+
"x_2 = [2.75, -1.5]; # data point 1"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 2,
35+
"id": "fb5fef77-9256-4878-813c-4bdb9bf888c7",
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"# Calculate standardized Euclidean distances\n",
40+
"\n",
41+
"d_1 = distance.seuclidean(q, x_1, np.diag(SIGMA))\n",
42+
"d_2 = distance.seuclidean(q, x_2, np.diag(SIGMA))\n",
43+
"\n",
44+
"# Note1: V is an 1-D array of component variances"
45+
]
46+
}
47+
],
48+
"metadata": {
49+
"kernelspec": {
50+
"display_name": "Python 3 (ipykernel)",
51+
"language": "python",
52+
"name": "python3"
53+
},
54+
"language_info": {
55+
"codemirror_mode": {
56+
"name": "ipython",
57+
"version": 3
58+
},
59+
"file_extension": ".py",
60+
"mimetype": "text/x-python",
61+
"name": "python",
62+
"nbconvert_exporter": "python",
63+
"pygments_lexer": "ipython3",
64+
"version": "3.10.9"
65+
}
66+
},
67+
"nbformat": 4,
68+
"nbformat_minor": 5
69+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "d6ef6479-b2ba-42e9-8e0e-bea7f535a8bc",
6+
"metadata": {},
7+
"source": [
8+
"Chapter 05\n",
9+
"\n",
10+
"# 马氏距离\n",
11+
"Book_6《数据有道》 | 鸢尾花书:从加减乘除到机器学习"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 1,
17+
"id": "dfd8a1aa-0e15-4e5d-94d1-c91d022a3f7c",
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"from scipy.spatial import distance\n",
22+
"import numpy as np\n",
23+
"from numpy.linalg import inv"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 2,
29+
"id": "384bbf81-052d-47f6-9f23-b4d3288fdde4",
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"# Variance-covariance matrix\n",
34+
"SIGMA = np.array([[2, 1], [1, 2]])"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 3,
40+
"id": "cdd13e51-5b97-40f1-902b-7773f3f863dd",
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"q = [0, 0]; # query point\n",
45+
"x_1 = [-3.5, -4]; # data point 1\n",
46+
"x_2 = [2.75, -1.5]; # data point 1"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 4,
52+
"id": "f6dfd618-5465-45fa-ac09-088520220d99",
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"# Calculate Mahal distances\n",
57+
"\n",
58+
"d_1 = distance.mahalanobis(q, x_1, inv(SIGMA))\n",
59+
"d_2 = distance.mahalanobis(q, x_2, inv(SIGMA))\n",
60+
"\n",
61+
"# Note1: the output of the function is Mahal distance, not Mahal distance squared\n",
62+
"# Note2: input is the inverse of the covariance matrix"
63+
]
64+
}
65+
],
66+
"metadata": {
67+
"kernelspec": {
68+
"display_name": "Python 3 (ipykernel)",
69+
"language": "python",
70+
"name": "python3"
71+
},
72+
"language_info": {
73+
"codemirror_mode": {
74+
"name": "ipython",
75+
"version": 3
76+
},
77+
"file_extension": ".py",
78+
"mimetype": "text/x-python",
79+
"name": "python",
80+
"nbconvert_exporter": "python",
81+
"pygments_lexer": "ipython3",
82+
"version": "3.10.9"
83+
}
84+
},
85+
"nbformat": 4,
86+
"nbformat_minor": 5
87+
}

0 commit comments

Comments
 (0)