In: Computer Science
Using Python:
1. Compute the difference of differences between consecutive numbers of a series:
input ser = pd.Series([1, 3, 6, 10, 15, 21, 27, 35]) output: [nan, 2.0, 3.0, 4.0, 5.0, 6.0, 6.0, 8.0] [nan, nan, 1.0, 1.0, 1.0, 1.0, 0.0, 2.0]
2. Compute the euclidean distance between two series:
Input: p = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) q = pd.Series([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) Desired Output: 18.165
1.
Source Code:
Output:
Code in text format (See above images of code for indentation):
#import library pandas
import pandas as pd
#series initilaization
ser = pd.Series([1, 3, 6, 10, 15, 21, 27, 35])
#difference between consecutive numbers of series
print(ser.diff().tolist())
#difference of difference betweeen consecutive number of
series
print(ser.diff().diff().tolist())
2.
Source Code:
Output:
Code in text format (See above images of code for indentation):
#import libraries numpy and pandas
import pandas as pd
import numpy as np
#two series of data intialized
p = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
q = pd.Series([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
#calculate euclid distance of p and q series
#norm in numpy is euclid norm
distance=np.linalg.norm(p-q)
#print euclid distance
print(round(distance,3))