Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:37:46+00:00 2026-06-02T06:37:46+00:00

I am trying to write naive bayes classifier and I keep getting this error:

  • 0

I am trying to write naive bayes classifier and I keep getting this error:

Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
import naive_assignment
File "C:\Python27\naive_assignment.py", line 655, in <module>
main()
File "C:\Python27\naive_assignment.py", line 650, in main
pans.append(p.classify(row))
File "C:\Python27\naive_assignment.py", line 597, in classify
less50Kcp = less50Kcp + self.less_cat_probs.get(query[4])
TypeError: unsupported operand type(s) for +: 'float' and 'NoneType'

I ament sure how to fix it since most of the fixes out there say return something but already am in the code.

def classify(self, query):

    less50Knp = 0.0
    less50Kcp = 0.0
    great50Knp = 0.0
    great50Kcp = 0.0

    less50Knp = less50Knp +self.less_num_prob_dist(float(query[1])/100)
    less50Knp = less50Knp + self.less_num_prob_dist(float(query[3])/100)
    less50Knp = less50Knp + self.less_num_prob_dist(float(query[5])/100)
    less50Knp = less50Knp + self.less_num_prob_dist(float(query[11])/100)
    less50Knp = less50Knp + self.less_num_prob_dist(float(query[12])/100)
    less50Knp = less50Knp + self.less_num_prob_dist(float(query[13])/100)

    less50Kcp = less50Kcp + self.less_cat_probs.get(query[2])
    less50Kcp = less50Kcp + self.less_cat_probs.get(query[4])
    less50Kcp = less50Kcp + self.less_cat_probs.get(query[6])
    less50Kcp = less50Kcp + self.less_cat_probs.get(query[7])
    less50Kcp = less50Kcp + self.less_cat_probs.get(query[8])
    less50Kcp = less50Kcp + self.less_cat_probs.get(query[9])
    less50Kcp = less50Kcp + self.less_cat_probs.get(query[10])
    less50Kcp = less50Kcp + self.less_cat_probs.get(query[14])



    less50K_prob = less50Kcp * less50Knp

    great50Knp = great50Knp + self.great_num_prob_dist(float(query[1])/100)
    great50Knp = great50Knp + self.great_num_prob_dist(float(query[3])/100)
    great50Knp = great50Knp + self.great_num_prob_dist(float(query[5])/100)
    great50Knp = great50Knp + self.great_num_prob_dist(float(query[11])/100)
    great50Knp = great50Knp + self.great_num_prob_dist(float(query[12])/100)
    great50Knp = great50Knp + self.great_num_prob_dist(float(query[13])/100)

    great50Kcp = great50Kcp + self.great_cat_probs.get(query[2])
    great50Kcp = great50Kcp + self.great_cat_probs.get(query[4])
    great50Kcp = great50Kcp + self.great_cat_probs.get(query[6])
    great50Kcp = great50Kcp + self.great_cat_probs.get(query[7])
    great50Kcp = great50Kcp + self.great_cat_probs.get(query[8])
    great50Kcp = great50Kcp + self.great_cat_probs.get(query[9])
    great50Kcp = great50Kcp + self.great_cat_probs.get(query[10])
    great50Kcp = great50Kcp + self.great_cat_probs.get(query[14])


    great50K_prob = great50Kcp * great50Knp

    if less50K_prob > great50K_prob:
        return ' <=50K'

    elif less50K_prob < great50K_prob:
        return ' >50K'

    else:
        return 'unknown'

I know it isn’t the best way to code it.
The main function that calls this is:

def main():

data = getInputData('./trainingset.txt')
test = getInputData('./queries.txt')

p = nbayes(data)
p.train()

pans = []

for row in test:
    pans.append(p.classify(row))
print("n-bayes")
print(pans)


main()

Does anyone have idea how to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:37:47+00:00Added an answer on June 2, 2026 at 6:37 am

    self.less_cat_probs.get(query[4]) apparently evaluates to None – you need to check for this and avoid it, or fix the code that produces it.

    The error message explains this pretty well – it’s throwing an unsupported type error, and telling you you cannot add a float to a NoneType on the given line. As we can see that less50Kcp is a float, the other item must be None, hence the error, as None is not a number.

    A possible fix – presuming self.less_cat_probs is a dict, would be to provide get() with a default value of 0, so that the addition will still work when not finding a key. E.g:

    less50Kcp = less50Kcp + self.less_cat_probs.get(query[4], 0)

    There is the question, however, of whether this is the desired functionality – you might instead want to ensure you have entries in the dict where needed, and presumably you would want to repeat this fix across your results.

    Please note that the code you have given us is a really bad example of copy/paste coding – this leads to more bugs, harder maintenance, more bugs, and a lot more typing. I’d highly recommend doing this properly, reducing repetitive code using loops and data structures, it will make it easier to spot bugs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a file using the FileOutputStream and OutputStreamWriter but I keep
I'm trying to build a file server using Java TCP sockets. I keep getting
i'm having the the post's title error when trying to write a file on
I am trying to create a file on the filesystem, but I keep getting
Trying to write a couple of functions that will encrypt or decrypt a file
Im trying to write a single byte at a certain location in a file.
I am trying to write a parallel prefix scan on cuda by following this
I'm trying to write this custom addition class for very large integers, bigger than
I'm trying to write a simple program to alert me when ram is getting
I am trying to write an output text file from table. I am not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.