Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

Does anyone know why I would be getting an error like this?! I would really appreciate it if you do, I am new to this and trying to learn but im getting really caught up in the nitty gritty of python! this is the error I am getting:

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

 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
like image 204
MB. Avatar asked Apr 12 '14 23:04

MB.


People also ask

How do I fix unsupported operand type S in Python?

The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num .

How do you fix unsupported operand type S for NoneType and int?

The Python "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'" occurs when we try to use the addition (+) operator with a None value. To solve the error, figure out where the variable got assigned a None value and correct the assignment.

What does TypeError unsupported operand type S for -: List and int?

The Python "TypeError: unsupported operand type(s) for /: 'list' and 'int'" occurs when we try to use the division / operator with a list and a number. To solve the error, figure out how the variable got assigned a list and correct the assignment, or access a specific value in the list.

What does TypeError unsupported operand type S for -: STR and STR mean?

The Python "TypeError: unsupported operand type(s) for -: 'str' and 'str'" occurs when we try to use the subtraction - operator with two strings. To solve the error, convert the strings to int or float values, e.g. int(my_str_1) - int(my_str_2) .


2 Answers

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

like image 65
Realife_Brahmin Avatar answered Oct 05 '22 15:10

Realife_Brahmin


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

like image 30
flakes Avatar answered Oct 05 '22 17:10

flakes