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:
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:
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
I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this func...
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 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...
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:
We need to consider the following cases:
Case 1 - It is possible that the given...
2. Consider the following function # listOfNumbers is a list of only numbers # def processList(listOfNumbers): result = [] for i in listOfNumbers: if i<0 == 0: result.append(i*i) else: result.append((i*i)+1) return result First, study and test processList(listOfNumbers) to determine what it does Then rewrite its body so that it accomplishes the same task with a one-line list comprehension. Thus, the resulting function will have exactly two lines, the def line and a return line containing a list comprehension expression. 3....
BlockPy: #33.8) Maximum Odd Use the Min/Max pattern to write a function maximum_odd that finds the highest odd value in the list (consumes a list of numbers and returns a number). Use the provided helper function is_odd (that consumes a single number and returns a boolean indicating whether it is true or false). Do not change the helper function is odd. Call your maximum_odd function on your favorite list of numbers Console Feedback: Instructor Feedback You cannot use the builtin...
this is the function and here's what I did for this
function but there is an error showing saying nonetype does not
have type len() because math.ceil function (we cannot import any
math..) for this function in python, if you could help me with this
code.
thanks
gerer grodpugu def slice_list(1st: List(Any) ni int) -> List[List[Any]]: Return a list containing slices of <st> in order. Each slice is a List of size <n> containing the next <n> elements in <tst>....
[Python] I have the following function that removes all non-numerical characters from a string: def remove_non_numeric(s): seq_type= type(s) return seq_type().join(filter(seq_type.isdigit, s)) And I need help implementing it into this function: def list_only_numbers( a_list ) : # Create a new empty list. # Using a loop (or a list comprehension) # 1) call remove_non_numeric with a list element # 2) if the return value is not the empty string, convert # the string to either int or float (if it contains...
HI I would like to ask how to write the function when my list is
[[1,2,4],[0,3,2]]. Like merging them. I managed to do it separately
only. The mean for [1,2,4] is 2.33 and so xbar return me
[-1.33,-0.33,1.67] (is like [1,2,4] minus the 2.33. I need help for
more lists. Thanks
>>> def mean(Ist): return sum(Ist)/len(Ist) >>> def xbar(data): i = 0 a = mean(data) for i in range(len(data)): data[i] -= a i += 1 return data >>> xbar([1,2,4]) [-1.3333333333333335,...
Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...
COMPLETE THE _CONSTRUCT() AND CONSTRUCTTREE() FUNCTIONS class expressionTree: class treeNode: def __init__(self, value, lchild, rchild): self.value, self.lchild, self.rchild = value, lchild, rchild def __init__(self): self.treeRoot = None #utility functions for constructTree def mask(self, s): nestLevel = 0 masked = list(s) for i in range(len(s)): if s[i]==")": nestLevel -=1 elif s[i]=="(": nestLevel += 1 if nestLevel>0 and not (s[i]=="(" and nestLevel==1): masked[i]=" " return "".join(masked) def isNumber(self, expr): mys=s.strip() if len(mys)==0 or not isinstance(mys, str): print("type mismatch error: isNumber")...