### Install FastDTW using pip Source: https://github.com/slaypni/fastdtw/blob/master/README.rst This command installs the fastdtw library using pip, the Python package installer. Ensure you have Python and pip installed. ```bash pip install fastdtw ``` -------------------------------- ### FastDTW Alignment Path Example Source: https://context7.com/slaypni/fastdtw/llms.txt This example demonstrates how to use the `fastdtw` function to calculate the distance and alignment path between two sequences. It also shows how to extract aligned sequences and calculate point-wise differences. ```APIDOC ## FastDTW Alignment Path Example ### Description This example demonstrates how to use the `fastdtw` function to calculate the distance and alignment path between two sequences. It also shows how to extract aligned sequences and calculate point-wise differences. ### Method `fastdtw(x, y, radius=1, dist=None)` ### Endpoint N/A (This is a Python library function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import numpy as np from fastdtw import fastdtw # Two sequences with different lengths and timing x = np.array([1, 1, 2, 3, 2, 0]) y = np.array([0, 1, 1, 2, 3, 2, 1]) distance, path = fastdtw(x, y) print(f"Distance: {distance}") print(f"Path length: {len(path)}") print(f"Alignment path:") for i, j in path: print(f" x[{i}]={x[i]} <-> y[{j}]={y[j]}") # Extract aligned sequences x_aligned = [x[i] for i, j in path] y_aligned = [y[j] for i, j in path] print(f"\nAligned x: {x_aligned}") print(f"Aligned y: {y_aligned}") # Calculate point-wise differences along alignment differences = [abs(x[i] - y[j]) for i, j in path] print(f"Point-wise differences: {differences}") print(f"Sum of differences (=distance): {sum(differences)}") ``` ### Response #### Success Response (200) - **distance** (float) - The calculated distance between the two sequences. - **path** (list of tuples) - A list of (index_x, index_y) tuples representing the optimal alignment path. #### Response Example ```json { "distance": 3.0, "path": [ (0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (5, 6) ] } ``` ``` -------------------------------- ### FastDTW Example: Calculate Distance and Path Source: https://github.com/slaypni/fastdtw/blob/master/README.rst This Python code demonstrates how to use the fastdtw library to calculate the distance and optimal path between two time series. It utilizes numpy for array manipulation and scipy's euclidean distance metric. ```python import numpy as np from scipy.spatial.distance import euclidean from fastdtw import fastdtw x = np.array([[1,1], [2,2], [3,3], [4,4], [5,5]]) y = np.array([[2,2], [3,3], [4,4]]) distance, path = fastdtw(x, y, dist=euclidean) print(distance) ``` -------------------------------- ### Exact DTW with dtw function Source: https://context7.com/slaypni/fastdtw/llms.txt Illustrates the use of the `dtw` function for computing the exact Dynamic Time Warping distance and optimal alignment paths. Examples cover 1D and 2D sequences, including the use of custom distance functions and built-in p-norm calculations like Manhattan distance. ```python import numpy as np from fastdtw import dtw # 1D exact DTW calculation x = np.array([1, 2, 3, 4, 5], dtype='float') y = np.array([2, 3, 4], dtype='float') distance, path = dtw(x, y) print(f"Exact Distance: {distance}") print(f"Optimal path: {path}") # 2D sequences with custom distance function x_2d = np.array([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]) y_2d = np.array([[2, 2], [3, 3], [4, 4]]) # Custom Euclidean distance function def euclidean_dist(a, b): return np.sqrt(np.sum((a - b) ** 2)) distance, path = dtw(x_2d, y_2d, dist=euclidean_dist) print(f"2D Exact Distance: {distance}") # Using built-in p-norm (p=1 for Manhattan distance) distance_manhattan, _ = dtw(x_2d, y_2d, dist=1) print(f"Manhattan Distance: {distance_manhattan}") ``` -------------------------------- ### Custom Distance Functions in FastDTW Source: https://context7.com/slaypni/fastdtw/llms.txt Shows how to implement and use custom distance functions with FastDTW, including p-norm calculations and user-defined functions like cosine distance and weighted Euclidean distance. This allows for flexible similarity measures tailored to specific data characteristics. ```python import numpy as np from fastdtw import fastdtw # Sample multi-dimensional time series x = np.array([[0, 0], [1, 1], [2, 2], [3, 3]]) y = np.array([[0, 1], [1, 2], [2, 3]]) # Default distance (absolute difference for 1D, 1-norm for multi-dimensional) distance_default, _ = fastdtw(x, y) print(f"Default (1-norm): {distance_default}") # Using p-norm with p=2 (Euclidean) distance_euclidean, _ = fastdtw(x, y, dist=2) print(f"Euclidean (2-norm): {distance_euclidean}") # Custom cosine-based distance function def cosine_distance(a, b): dot = np.dot(a, b) norm_a = np.linalg.norm(a) norm_b = np.linalg.norm(b) if norm_a == 0 or norm_b == 0: return 1.0 return 1.0 - (dot / (norm_a * norm_b)) distance_cosine, path = fastdtw(x, y, dist=cosine_distance) print(f"Cosine Distance: {distance_cosine}") print(f"Alignment: {path}") # Weighted distance for features with different importance def weighted_distance(a, b, weights=np.array([1.0, 2.0])): return np.sqrt(np.sum(weights * (a - b) ** 2)) distance_weighted, _ = fastdtw(x, y, dist=weighted_distance) print(f"Weighted Distance: {distance_weighted}") ``` -------------------------------- ### Approximate DTW with fastdtw function Source: https://context7.com/slaypni/fastdtw/llms.txt Demonstrates using the `fastdtw` function to compute approximate Dynamic Time Warping distance and alignment paths for 1D and 2D time series. It shows how to use custom distance functions like Euclidean and p-norm, and how the `radius` parameter affects accuracy and performance. ```python import numpy as np from scipy.spatial.distance import euclidean from fastdtw import fastdtw # 1D time series comparison x = np.array([1, 2, 3, 4, 5], dtype='float') y = np.array([2, 3, 4], dtype='float') distance, path = fastdtw(x, y) print(f"Distance: {distance}") print(f"Alignment path: {path}") # 2D time series with custom distance function x_2d = np.array([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]) y_2d = np.array([[2, 2], [3, 3], [4, 4]]) distance, path = fastdtw(x_2d, y_2d, dist=euclidean) print(f"2D Distance: {distance}") # Using p-norm distance (e.g., Euclidean norm with p=2) distance, path = fastdtw(x_2d, y_2d, dist=2) print(f"2-norm Distance: {distance}") # Adjusting radius for accuracy vs performance trade-off distance_low, _ = fastdtw(x, y, radius=1) distance_high, _ = fastdtw(x, y, radius=5) print(f"Radius 1: {distance_low}, Radius 5: {distance_high}") ``` -------------------------------- ### Extracting and Analyzing Alignment Paths in Python Source: https://context7.com/slaypni/fastdtw/llms.txt This snippet demonstrates how to compute the distance and alignment path between two NumPy arrays using fastdtw. It shows how to iterate through the path to map indices and calculate point-wise differences between the aligned sequences. ```python import numpy as np from fastdtw import fastdtw # Two sequences with different lengths and timing x = np.array([1, 1, 2, 3, 2, 0]) y = np.array([0, 1, 1, 2, 3, 2, 1]) distance, path = fastdtw(x, y) print(f"Distance: {distance}") print(f"Path length: {len(path)}") print(f"Alignment path:") for i, j in path: print(f" x[{i}]={x[i]} <-> y[{j}]={y[j]}") # Extract aligned sequences x_aligned = [x[i] for i, j in path] y_aligned = [y[j] for i, j in path] print(f"\nAligned x: {x_aligned}") print(f"Aligned y: {y_aligned}") # Calculate point-wise differences along alignment differences = [abs(x[i] - y[j]) for i, j in path] print(f"Point-wise differences: {differences}") print(f"Sum of differences (=distance): {sum(differences)}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.