.map() python code example
Example 1: mAPE python
def percentage_error(actual, predicted):
res = np.empty(actual.shape)
for j in range(actual.shape[0]):
if actual[j] != 0:
res[j] = (actual[j] - predicted[j]) / actual[j]
else:
res[j] = predicted[j] / np.mean(actual)
return res
def mean_absolute_percentage_error(y_true, y_pred):
return np.mean(np.abs(percentage_error(np.asarray(y_true), np.asarray(y_pred)))) * 100
Example 2: mapping in python string
def uppercase(u):
return str(u.upper())
map_iterator = map(uppercase,['a','b','c'])
mylist = list(map_iterator)
print(mylist)
Example 3: map in python 3
map(function, iterable, ...)
Example 4: python dictionary map function
dictionary = {k: f(v) for k, v in dictionary.items()}
Example 5: map in python
def addition(n):
return n + n
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
Example 6: função map python
lista = [1, 2, -3, 4, 5, -9]
def quadrado(n):
return n*n
map(quadrado, lista)
[1, 4, 9, 16, 25, 81]