Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: unsupported operand type(s) for +: 'map' and 'float'

Tags:

python

I was practicing tesorflow on Colaboratory and my code is as below. There is some issue, the error message is

"TypeError: unsupported operand type(s) for +: 'map' and 'float'"

Can someone tell me what's wrong? thanks!

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(20)
y = map(lambda x: x + np.random.randn(1), x)
a, b = np.polyfit(x, y, 1)
plt.plot(x, y, 'o', np.arange(20), a*np.arange(20)+b, '-');**
like image 493
Pingyao Chen Avatar asked Mar 08 '18 07:03

Pingyao Chen


1 Answers

the 'Colaboratory Introduction' is writed by Python2, as @cdarke said, map() return a map object in Python3. So you should trans the map obj to a list. Replace the code like this:

y = list(map(lambda x: x + np.random.randn(1), x))
like image 68
KenZhangCn Avatar answered Sep 28 '22 08:09

KenZhangCn