MB.
MB.

Reputation: 153

Python TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

Does anyone know why I would be getting an error like this?

TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

I would really appreciate it if you do, I am new to this and trying to learn, but I'm getting really caught up in the nitty gritty of Python!

Code:

for test in test_set:
 person_id = test['person_id']
 place_id = test['place_id']
 rating = test['rating']
 predicted_rating = simple_nn(person_id, place_id, 5)
 #difference =  (rating - predicted_rating)
 sq_err = (rating - predicted_rating) * (rating - predicted_rating)

 sq_err_sum = sq_err 
   
 sq_err_sum = sq_err_sum + sq_err
    
 rmse = math.sqrt(sq_err_sum/5)
 print rmse

Upvotes: 15

Views: 192600

Answers (6)

JeeyCi
JeeyCi

Reputation: 589

I suppose, that I'm having such problem in Windows 32x when any function implicitly converts dtype of variable to 'float64' -- I am having such problem working with

from scipy.stats import genpareto
##Generate random numbers:
r = genpareto.rvs(c, size=1000)
data = r.sort()

trying to minimize it with such MLE code - data = np.float64(r.sort()) helps, but still cannot plot (am not sure in the correctness of the result itself), though error disappears... fitting pareto with scipy.optimize.minimize seems problematic, as well as scipy.stats.pareto (here) without constraints -- as it probably can return NoneType... sometimes works another fit implementation

Upvotes: 0

mitesh keswani
mitesh keswani

Reputation: 153

During debug I realized that for me some column values were Nan, just incase it might be the same case for you. You could try the below snippet drops nan records.

df = df.dropna()

Upvotes: 0

farah
farah

Reputation: 1

I faced this problem as well when working with openpyxl; the operand * was supposed to change the value of the cells in the worksheet. This solution worked for me; the error was because of the cells with a None value. Adding the code below solved the issue:

if cell.value:
    corrected_price = cell.value * 0.9
else:
    corrected_price = 0

Upvotes: 0

Realife_Brahmin
Realife_Brahmin

Reputation: 63

In my case, I forgot to add

return

to the computed expression in the function definition.

Example:

def drift(y, t): theta*(mu-y)  # define OUP drift term
def diffusion(y, t): sigma  # define OUP diffusion term

will return the type 'None' when called,

where as,

def drift(y, t): return theta*(mu-y)  # define OUP drift term
def diffusion(y, t): return sigma  # define OUP diffusion term

will return actual numbers

Upvotes: 2

Ch HaXam
Ch HaXam

Reputation: 499

you can also use import division

from _ _ future _ _ import division

it worked for me in same case

Upvotes: 1

flakes
flakes

Reputation: 23624

Judging by what you've provided, and the error this is my conclusion.

The only place you use the - operand is in the two points

sq_err = (rating- predicted_rating) * (rating - predicted_rating)

because the error states 'float' and 'NoneType' we can conclude that rating is type float and predicted_rating is NoneType.

You defined predicted_rating as:

predicted_rating = simple_nn(person_id, place_id, 5)

So this means that somewhere in your code for the function simple_nn you are not returning anything. Perhaps if you used conditions you didn't evaluate every end path and the function simply returned.

for example... all of these functions return the None type.

def example1():
    pass

def example2():
    return

def example3(a = True, b  = True):
    if not a:
        return True
    elif not b:
        return False

Note in the last example there is a path where neither if case is satisfied,.. thus it could return None

Upvotes: 15

Related Questions