1def percentage_error(actual, predicted):
2 res = np.empty(actual.shape)
3 for j in range(actual.shape[0]):
4 if actual[j] != 0:
5 res[j] = (actual[j] - predicted[j]) / actual[j]
6 else:
7 res[j] = predicted[j] / np.mean(actual)
8 return res
9
10def mean_absolute_percentage_error(y_true, y_pred):
11 return np.mean(np.abs(percentage_error(np.asarray(y_true), np.asarray(y_pred)))) * 100
1# The map() function applies a given function
2# to each item of an iterable (list, tuple etc.) and returns an iterator
3
4numbers = [2, 4, 6, 8, 10]
5
6# returns square of a number
7def square(number):
8 return number * number
9
10# apply square() function to each item of the numbers list
11squared_numbers_iterator = map(square, numbers)
12
13# converting to list
14squared_numbers = list(squared_numbers_iterator)
15print(squared_numbers)
16
17# Output: [4, 16, 36, 64, 100]
1# Python program to demonstrate working
2# of map.
3
4# Return double of n
5def addition(n):
6 return n + n
7
8# We double all numbers using map()
9numbers = (1, 2, 3, 4)
10result = map(addition, numbers)
11print(list(result))
1#generate a list from map iterable with lambda expression
2list( map(lambda x: x*2, numeros) )
1def calculateSquare(n):
2 return n*n
3
4
5numbers = (1, 2, 3, 4)
6result = map(calculateSquare, numbers)
7print(result)
8
9# converting map object to set
10numbersSquare = list(result)
11print(numbersSquare)