Question

Coding for Python - The pattern detection problem – part 2: def calculate_similarity_list(data_series, pattern)

Please do not use 'print' or 'input' statements.

Context of the assignment is:

In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is located in the data series. Please see attachments below:

The pattern detection problem In this assignment, your goal is to write a python program to determine whether a given patternh fig 1: Data Series Fig 2: Pattem 50 20 10 a0 If you compare these two figures, you can see that this pattern appears betwee

We need to consider the following cases:

Case 1 - It is possible that the given data series is shorter than the given pattern, in this case, we return "Insufficient data".

Case 2 - All the similarity measures are (strictly) less than the given threshold value. This means none of the segments is similar to the given pattern. In this case, we say the pattern is not present in the data series and return "Not detected". This is not the case for the example just outlined.

Case 3 - At least one similarity measure is greater than or equal to the given threshold value. This is the case for the example outlined. The similarity measure plot above shows that the fifth similarity measure is the largest. You can work out that the fifth similarity measure is computed by using the segment formed by the fifth (index 4) to eighth (index 7) data points of the data series. This procedure is therefore telling us that the segment consisting of fifth to eighth data points is most similar to the given pattern. Indeed, this is what we had found by using visual inspection earlier. We will identify the location of the pattern by using the first index of the segment that has the highest similarity measure.

I will need to implement the following four functions, each in a separate file (provided). For this question I’m looking at getting start on function 2: def calculate_similarity_list(data_series, pattern).

The 4 functions will be:

def calculate_similarity(data_segment, pattern):

def calculate_similarity_list(data_series, pattern):

def pattern_search_max(data_series, pattern, threshold):

def pattern_search_multiple(data_series, pattern_width, threshold):

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 completed the code for function 1 (def calculate_similarity)… and would like some help to complement this… my code for def calculate_similarity is as follows:

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

The seudo-code to start function 2 is as per below:

def calculate_similarity_list(data_series, pattern):

   

""" Calculate the similarity measures between all possible data segments

    and the pattern.

    The function calculates the similarity measures, using the

    function 'calculate_similarity', of all possible data segments in a

    data_series against a given pattern and returns the list of calculated

    similarity values.

    Parameters

    ----------

    data_series : [float]

        A list of float values representing a data series.

       

    pattern : [float]

        A list of float values representing the pattern.

    Returns

    -------

        List of floats

            The list of calculated similarity values.

    """

    # Insert your code here.

The pattern detection problem In this assignment, your goal is to write a python program to determine whether a given pattern appears in a data series, and it so, where it is located in the data series. This type of problems is science, engineering, medicine and science. There are many different types of patten detection problems, the setting of this assignment in radars. A radar transmits a pulse of a specific shape and waits for a pulse of similar shape to return, in order to determine the position of an is known as matched filtering and is widely used in communication systems. This means your mobile phones perform the same type of calculations that you wil be programming below objoct. The method describecd Learning objectives By completing this assignment, you will learn: 1. To apply programming concepts of variable declaration, constant declaration, assignment, selection and iteration (for loop) 2. To translate an algorithm described in a natural language to a computer language 3. To organize programs into smaller modules by using functions 4. To use good program style including comments, meaning variable names and others 5. To get a practice on software development, which includes incremental development, testing and debugging Algorithm for locating a pattern in a data series rs in the data series. In the following example, the pattern to be detected is a sequence of 4 numbers (see Fig 2) and the data serie contains 10 data points (see Fig 1) 22, 10, 1, 3 pattern (40, 30, 20, 10) Fig 2 Patterm Fig 1: Data Senies 50 se
h fig 1: Data Series Fig 2: Pattem 50 20 10 a0 If you compare these two figures, you can see that this pattern appears between 5th (at index4) to 8th (at index 7) data points of the data senies. Note that it is not an exact maich but a farly close one. Now you can spot the pattern using your eyes, let us see how an algorithm can do i Since the given pattern has 4 data points, we will take a segment of 4 consecutive data points from the data series at a time and compute a simlarity measure. The similarity measure shoud have the property that if a segment of the data series is similar to the given pattern, then the simlarity measure of that segment is large, and vice versa Hence, if we can compute the simlarity measures of all possible segments, then we can identify the segment that is most similar to the given pattern. We wll now provide more details and the frst segment, which is formed by the first 4 data points of the data series. In terms of the two lists The algorithm begins with computing the similarity measure between the given pattern above, the similarity measure for the first segment is data serieste)'pattern(e] . data series(1) patternt)+ data, series 21 patternt2) data seriestal pattern(3) Atfer tis, we compute the simlarity measure between the given patrn and the second segment, which is formed by the second to fith data points of the the similarity measure for this segment is data, series (4]'pattern(3) ern(2) data series[11 patternte] + data series(21 pattern[11 data series([3) patt We then do the same with the segment formed by the third to sixth data points(the third segment), gving a similiarity measure equals to terntt) data,series(41'oatten2) data seriest51'patternt) d data,series12) patternte) data series(3]"pa the smaarity measure for the last possible segment, which isformed by the 7th to 10th data port. The Ilwing-re·mlary list for the abon and a plot
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

#function 2

def calculate_similarity_list(data_series, pattern):
data_segment=[] #create empty data_segment
result=[] #create empty result list
if len(data_series) < len(pattern): #if length is not sufficient then return insufficient
result.append('Insufficient Data')

else:
for i in range(0,len(data_series)-len(pattern)+1): #now take index from 0 to starting index of last data_segment
for j in range(i,i+len(pattern)): #iterate through all the index of data_segment
data_segment.append(data_series[j]) #now add elements one by one to data_segment
result.append(calculate_similarity(data_segment,pattern)) #it adds values one by one
data_segment.clear() #it clears the list
return result


#testing function 2
data_series=[-1,2,-2,3,41,38,22,10,-1,3]
pattern=[40,30,20,10]
print(calculate_similarity_list(data_series,pattern))

OUTPUT-

RESTART:C:/Users/pc/AppData/Local/Programs/Python/Python35/data_series.py - [10, 490, 1210, 2330, 3320, 2370, 1190]

Add a comment
Know the answer?
Add Answer to:
Coding for Python - The pattern detection problem – part 2: def calculate_similarity_list(data_series, pattern) Please do not use 'print' or 'input' statements. Context of the assignme...
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
  • Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Plea...

    Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Please do not use 'print' or 'input' statements. Context of the assignment is: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is located in the data series. Please see attachments below: We need to consider the following cases: Case 1 - It is possible that the...

  • Coding for Python – don’t need it to be complex: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is locate...

    Coding for Python – don’t need it to be complex: In this assignment, your goal is to write a Python program to determine whether a given pattern appears in a data series, and if so, where it is located in the data series. This type of problem is common in many health disciplines. For example, identifying treatment pathways. The goal is to detect whether a certain pattern appears in the data series. In the following example, the pattern to be...

  • I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this func...

    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...

  • Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: ret...

    Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: return mlist else: mid=len(mlist)//2 return merge(mergesort(mlist[:mid]),mergesort(mlist[mid:])) Problem 1 (30 points) stable merge sort Consider the following unsorted list of integers containing duplicates where the relative position of each duplicated integer in the unsorted list is noted as a subscript. For example, 1, is at a smaller index than 12. The subscript is ignored when comparing two values since it would not actually...

  • I really need help with this python programming assignment Program Requirements For part 2, i need...

    I really need help with this python programming assignment Program Requirements For part 2, i need the following functions • get floats(): It take a single integer argument and returns a list of floats. where it was something like this def get_floats(n):   lst = []   for i in range(1,n+1):     val = float(input('Enter float '+str(i)+': '))     lst.append(val)   return lst • summer(): This non-void function takes a single list argument, and returns the sum of the list. However, it does not use...

  • please answer "def turn_payouts(move_a, move_b):" in python. Notes Two players will face each other. They each...

    please answer "def turn_payouts(move_a, move_b):" in python. Notes Two players will face each other. They each decide independently to "cooperate" or "cheat". If they both cooperated, they each win two points. If they both cheated, nobody wins anything. one cheats, the cheater gets +3 and the cooperator loses a point. That wasn't very kind! One turn is defined as each player making a choice, and winning or losing some points as a result. Shared history against this player is available...

  • Python coding exercise: please include comments Goal #1: import financial data given into your pr...

    Python coding exercise: please include comments Goal #1: import financial data given into your program provided to you as a CSV formatted text file Use the following data for testing (the following is only a sample of the data; there are over 4000 rows of data): *Note: The data you will read in is linear by date (but, non-contiguous due to holidays and weekends,) reflecting a timeline of stock performance in chronological order; however your program should run through the...

  • I need a python 3 help. Please help me with this question Part 2. Linked Lists...

    I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...

  • PYHTON CODING FUNCTIONS HELP Part 2. Also need help with these last functions. Requirements/restraints and the...

    PYHTON CODING FUNCTIONS HELP Part 2. Also need help with these last functions. Requirements/restraints and the map referred to is pictured in the screenshot. Need help with these 4 tasks: Function 4: def is_map(map) : given a map (see screenshot), does it meet the following criteria? 1) it is a list of lists of values of type int; 2) it has at least one row and one column; 3) it’s rectangular (all sub-lists are same length); 4) only non-negative ints...

  • solution is required in pseudo code please. 2 Knapsack Problem În al Knapsack problem. given n items(11-12. . . ....

    solution is required in pseudo code please. 2 Knapsack Problem În al Knapsack problem. given n items(11-12. . . . . 1"} with weight {w1·W2. . . . . ux) and value (n 2, .., nJ, the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity In this question, we will consider two different ways to represent a solution to the...

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