Question

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:

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 3: def pattern_search_max(data_series, pattern, threshold). Numpy can be used if needed.

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 3: def pattern_search_max(data_series, pattern, threshold), is described as:

  • The three possible outcomes are "Insufficient data", "Not detected" and the location of the pattern if the pattern is found in the data series. Here, the location of the pattern refers to the index of the highest similarity measure that is also greater than or equal to the given threshold value.
  • The first parameter 'data_series' is a list of (float) values, the second parameter 'pattern' is a list of (float) values, and the third parameter 'threshold' is a (float) value. In this function, you need to use the function 'calculate_similarity_list'.

I have completed the code for function 1 (def calculate_similarity) and function 2 (def calculate_similarity_list)… and part of the pseudo-code for function 3, and would like some help to complement this… the code is as follows:

# function 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):

    output=[]

    for y in range(len(data_series)-len(pattern)+1):

        result=calculate_similarity(data_series[y:y+len(pattern)], pattern)

        output.append(result)

    return output

#function 3 - pseudo-code

def pattern_search_max(data_series, pattern, threshold):

    """ Search for the highest similarity measure that is also greater than

        or equal to the given threshold value and returns the index of that

        value.

        The function finds the index of the highest similarity value,

        using the similarity_list returned by the function

        'calculate_similarity_list'.

    Parameters

    ----------

    data_series : [float]

        A list of float values representing a data series.

    pattern : [float]

        A list of float values representing the pattern.

    threshold : [float]

        A float value. Selected similarity measure needs to be greater than or

        equal to the given threshold value.

    Returns

    -------

    "Insufficient data" : [String]

        If the given data_series is shorter than the given pattern.

    "Not detected" : [String]

        If all the similarity measures are (strictly) less than the given

        threshold value.

    float

        Index of the highest similarity measure that is also greater than

        or equal to the given threshold value.

    """

def pattern_search_max(data_series, pattern, threshold):

result = 0

if len(data_series) < len(pattern):

    result = 'Insufficient data'     

else:

    for k in range(0,len(csl.calculate_similarity_list(data_series, pattern))-1):

        if k >= threshold and k >result:

            result = k

        elif k < threshold:

            result = 'Not detected'

return result

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

Here's your #function 3 working as mentioned...

def pattern_search_max(data_series, pattern, threshold):

result = -1 #Stores the index of the segment which results in highest similarity

prev_max_value = -1 #Stores the highest similarity till now

if len(data_series) < len(pattern): #Not enough data

result = 'Insufficient data'   

else:

similarity_list = calculate_similarity_list(data_series, pattern) #Returns a list containing all the similarity values for every segment

for i in range(0,len(similarity_list)):

if similarity_list[i] >= threshold and similarity_list[i] > prev_max_value: #If similarity is greater than threshold and also greater than the previous similarity obtained then store the current index as result and update prev_max_value

result = i

prev_max_value = similarity_list[i];

if result == -1: After the loop ends if result is still -1 then it is clear that none of the obtained similarity surpasses the threshold hence pattern is not detected

result = 'Not detected'

return result

#HOPE IT HELPS....

Add a comment
Know the answer?
Add Answer to:
Coding for Python - The pattern detection problem – part 3: def pattern_search_max(data_series, pattern, threshold) Plea...
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