Skip to content

Commit 71b1f15

Browse files
authored
Add files via upload
1 parent a189843 commit 71b1f15

File tree

1 file changed

+281
-0
lines changed

1 file changed

+281
-0
lines changed
+281
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"nteract": {
7+
"transient": {
8+
"deleting": false
9+
}
10+
}
11+
},
12+
"source": [
13+
"### DistanceMetric\n",
14+
"\n",
15+
"This class provides a uniform interface to fast distance metric functions. The various metrics can be accessed via the get_metric class method and the metric string identifier"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {
21+
"nteract": {
22+
"transient": {
23+
"deleting": false
24+
}
25+
}
26+
},
27+
"source": [
28+
"**Available Metrics**\n",
29+
"\n",
30+
"The following lists the string metric identifiers and the associated distance metric classes:\n",
31+
"\n",
32+
"* Metrics intended for real-valued vector spaces:\n",
33+
"\n",
34+
"|**Identifier**|**Class name in scikit-learn**|**Args**|**Distance function**|\n",
35+
"|--------------|------------|------------|------------|\n",
36+
"|`Euclidean`|EuclideanDistance|.|$sqrt(sum((x - y)^2))$|\n",
37+
"|`Manhattan`|ManhattanDistance|.|$sum(x - y)$|\n",
38+
"|`chebyshev`|ChebyshevDistance|.|$max(x - y)$|\n",
39+
"|`minkowski`|MinkowskiDistance|p|$sum(x - y^p)^(1/p)$|\n",
40+
"|`wminkowski`|WMinkowskiDistance|p, w|$sum(w * (x - y)^p)^(1/p)$|\n",
41+
"|`seuclidean`|SEuclideanDistance|V|$sqrt(sum((x - y)^2 / V))$|\n",
42+
"|`mahalanobis`|MahalanobisDistance|V or VI|$sqrt((x - y)' V^-1 (x - y))$|\n",
43+
"\n",
44+
"\n",
45+
"\n",
46+
"**Metrics intended for two-dimensional vector spaces:**\n",
47+
"\n",
48+
"* Note that the haversine distance metric requires data in the form of [latitude, longitude] and both inputs and outputs are in units of radians.\n",
49+
"\n",
50+
"|**Identifier**|**Class name in scikit-learn**|**Distance function**|\n",
51+
"|--------------|------------|------------|------------|\n",
52+
"|`haversine`|HaversineDistance|$2 arcsin(sqrt(sin^2(0.5*dx) + cos(x1)cos(x2)sin^2(0.5*dy)))$|"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 2,
58+
"metadata": {
59+
"nteract": {
60+
"transient": {
61+
"deleting": false
62+
}
63+
}
64+
},
65+
"outputs": [
66+
{
67+
"data": {
68+
"text/plain": [
69+
"array([[0, 1, 2],\n",
70+
" [3, 4, 5]])"
71+
]
72+
},
73+
"execution_count": 2,
74+
"metadata": {},
75+
"output_type": "execute_result"
76+
}
77+
],
78+
"source": [
79+
"from sklearn.neighbors import DistanceMetric\n",
80+
"import numpy as np\n",
81+
"\n",
82+
"euclidean_dist = DistanceMetric.get_metric('euclidean')\n",
83+
"X = np.array([[0, 1, 2],[3, 4, 5]])\n",
84+
"X"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 3,
90+
"metadata": {
91+
"nteract": {
92+
"transient": {
93+
"deleting": false
94+
}
95+
}
96+
},
97+
"outputs": [
98+
{
99+
"data": {
100+
"text/plain": [
101+
"array([[0. , 5.19615242],\n",
102+
" [5.19615242, 0. ]])"
103+
]
104+
},
105+
"execution_count": 3,
106+
"metadata": {},
107+
"output_type": "execute_result"
108+
}
109+
],
110+
"source": [
111+
"euclidean_dist.pairwise(X)"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 4,
117+
"metadata": {
118+
"nteract": {
119+
"transient": {
120+
"deleting": false
121+
}
122+
}
123+
},
124+
"outputs": [],
125+
"source": [
126+
"Manhattan_dist = DistanceMetric.get_metric('manhattan')"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 5,
132+
"metadata": {
133+
"nteract": {
134+
"transient": {
135+
"deleting": false
136+
}
137+
}
138+
},
139+
"outputs": [
140+
{
141+
"data": {
142+
"text/plain": [
143+
"array([[0., 9.],\n",
144+
" [9., 0.]])"
145+
]
146+
},
147+
"execution_count": 5,
148+
"metadata": {},
149+
"output_type": "execute_result"
150+
}
151+
],
152+
"source": [
153+
"Manhattan_dist.pairwise(X)"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 6,
159+
"metadata": {
160+
"nteract": {
161+
"transient": {
162+
"deleting": false
163+
}
164+
}
165+
},
166+
"outputs": [],
167+
"source": [
168+
"ChebyshevDistance = DistanceMetric.get_metric('chebyshev')"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": 7,
174+
"metadata": {
175+
"nteract": {
176+
"transient": {
177+
"deleting": false
178+
}
179+
}
180+
},
181+
"outputs": [
182+
{
183+
"data": {
184+
"text/plain": [
185+
"array([[0., 3.],\n",
186+
" [3., 0.]])"
187+
]
188+
},
189+
"execution_count": 7,
190+
"metadata": {},
191+
"output_type": "execute_result"
192+
}
193+
],
194+
"source": [
195+
"ChebyshevDistance.pairwise(X)"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 8,
201+
"metadata": {
202+
"nteract": {
203+
"transient": {
204+
"deleting": false
205+
}
206+
}
207+
},
208+
"outputs": [
209+
{
210+
"data": {
211+
"text/plain": [
212+
"array([[0. , 5.19615242],\n",
213+
" [5.19615242, 0. ]])"
214+
]
215+
},
216+
"execution_count": 8,
217+
"metadata": {},
218+
"output_type": "execute_result"
219+
}
220+
],
221+
"source": [
222+
"MinkowskiDistance = DistanceMetric.get_metric('minkowski')\n",
223+
"MinkowskiDistance.pairwise(X)"
224+
]
225+
},
226+
{
227+
"cell_type": "code",
228+
"execution_count": 10,
229+
"metadata": {
230+
"nteract": {
231+
"transient": {
232+
"deleting": false
233+
}
234+
}
235+
},
236+
"outputs": [
237+
{
238+
"data": {
239+
"text/plain": [
240+
"2.0"
241+
]
242+
},
243+
"execution_count": 10,
244+
"metadata": {},
245+
"output_type": "execute_result"
246+
}
247+
],
248+
"source": [
249+
"from scipy.spatial import distance\n",
250+
"distance.wminkowski([1, 0, 0], [0, 1, 0], 1, np.ones(3))"
251+
]
252+
}
253+
],
254+
"metadata": {
255+
"kernel_info": {
256+
"name": "python3"
257+
},
258+
"kernelspec": {
259+
"display_name": "Python 3",
260+
"language": "python",
261+
"name": "python3"
262+
},
263+
"language_info": {
264+
"codemirror_mode": {
265+
"name": "ipython",
266+
"version": 3
267+
},
268+
"file_extension": ".py",
269+
"mimetype": "text/x-python",
270+
"name": "python",
271+
"nbconvert_exporter": "python",
272+
"pygments_lexer": "ipython3",
273+
"version": "3.6.8"
274+
},
275+
"nteract": {
276+
"version": "0.24.0"
277+
}
278+
},
279+
"nbformat": 4,
280+
"nbformat_minor": 4
281+
}

0 commit comments

Comments
 (0)