Question

I have to follow functions to perform:

Function 1: def calculate_similarity(data_segment, pattern):

The aim of this function is to calculate the similarity measure between one data segment and the pattern.

The first parameter 'data_segment' is a list of (float) values, and the second parameter 'pattern' is also a list of (float) values. The function calculates the similarity measure between the given data segment and pattern and returns the calculated similarity value (float), as described earlier in this assignment outline. The function returns "Error" if 'data_segment' and 'pattern' have different lengths.

Function 2: def calculate_similarity_list(data_series, pattern), is described as:

The aim of this function is to calculate the similarity measures between all possible data segments and the pattern.

The first parameter 'data_series' is a list of (float) values, and the second parameter 'pattern' is also a list of (float) values. The function calculates the similarity measures, using the above function 'calculate_similarity', of all possible data segments in a sequence, and returns the list of calculated similarity values.

I have the following code for this. My first function tests fine, but my second function is hitting an issue:

data_segment [] pattern def calculate_similarity(data_segment, pattern): if len(pattern) is not len (data_segment): m- error

The test series I have, and the expected answer, I should get is below:

data_series = [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3]

pattern = [40, 30, 20, 10]

print(calculate_similarity_list(data_series,pattern))

# expected_answer = [56, 34, -1.02, 219.34, -123.1, 179.8]

But I get:

In [8]: def calculate_similarity_list(data_series, pattern): data-segment [] #create empty data-segment results[] #create emp

0 0
Add a comment Improve this question Transcribed image text
Answer #1

data_segment = []
pattern = []

def calculate_similarity(data_segment, pattern):
   if len(pattern) is not len(data_segment):
       m = 'error'
   else:
       m = 0
       for z in range(0,len(data_segment)):
           m = m + data_segment[z] * pattern[z]

   return m

# Test function 1
data_segment = [-1, 2, -2, 3]
data_segment = [float(x) for x in data_segment]
pattern = [40, 30, 20, 10]
pattern = [float(x) for x in pattern]
print(calculate_similarity(data_segment,pattern))

#   expected_answer = 10
#   answer = 10

def calculate_similarity_list(data_series, pattern):
   data_segment = [] # create empty data_segment
   result = [] # create empty result list

   if len(data_series) < len(pattern):
       result.append('Insufficient Data')

   else:
       for i in range(0,len(data_series) - len(pattern) + 1):
           for j in range(i,i+len(pattern)):

               data_segment.append(data_series[j])

# there seems to be indentation error in this line, it was wriiten inside for loop, size of data_segment was always 1 which was not equal to size of pattern hence always error
           result.append(calculate_similarity(data_segment,pattern))
           data_segment.clear()

   return result
data_series = [-1, 2, -2, 3, 41, 38, 22, 10, -1, 3]
data_series = [float(x) for x in data_series]
pattern = [40, 30, 20, 10]
pattern = [float(x) for x in pattern]

print(calculate_similarity_list(data_series,pattern))

# expected_answer = [56, 34, -1.02, 219.34, -123.1, 179.8]

I am still not able to get the expected answer, please work out and tell how did you got this expected answer

I am getting [10.0, 490.0, 1210.0, 2330.0, 3320.0, 2370.0, 1190.0]

which you can verify:

(-1 * 40) + (2*30) + (-2*20) + (3*10) = 10

(2*40) + (-2 * 30) + (3*20) + (41*10) = 490 and so on

Add a comment
Know the answer?
Add Answer to:
I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this func...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT