{"id":13937405,"url":"https://github.com/muatik/naive-bayes-classifier","last_synced_at":"2026-01-11T00:50:07.845Z","repository":{"id":15860171,"uuid":"18600551","full_name":"muatik/naive-bayes-classifier","owner":"muatik","description":"yet another general purpose naive bayesian classifier.","archived":false,"fork":false,"pushed_at":"2019-09-01T00:48:35.000Z","size":74,"stargazers_count":172,"open_issues_count":5,"forks_count":72,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-11-02T05:38:25.516Z","etag":null,"topics":["bayes-classifier","bayesian","classifier","machine-learning","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/muatik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-09T14:35:48.000Z","updated_at":"2024-10-13T14:52:44.000Z","dependencies_parsed_at":"2022-08-26T07:41:19.406Z","dependency_job_id":null,"html_url":"https://github.com/muatik/naive-bayes-classifier","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muatik%2Fnaive-bayes-classifier","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muatik%2Fnaive-bayes-classifier/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muatik%2Fnaive-bayes-classifier/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/muatik%2Fnaive-bayes-classifier/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/muatik","download_url":"https://codeload.github.com/muatik/naive-bayes-classifier/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226693903,"owners_count":17667757,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bayes-classifier","bayesian","classifier","machine-learning","python"],"created_at":"2024-08-07T23:03:34.438Z","updated_at":"2026-01-11T00:50:07.781Z","avatar_url":"https://github.com/muatik.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"Naive Bayesian Classifier\n======================\n\nyet another general purpose Naive Bayesian classifier.\n\n##Installation\nYou can install this package using the following ```pip``` command:\n\n```sh\n$ sudo pip install naiveBayesClassifier\n```\n\n\n##Example\n\n```python\n\"\"\"\nSuppose you have some texts of news and know their categories.\nYou want to train a system with this pre-categorized/pre-classified \ntexts. So, you have better call this data your training set.\n\"\"\"\nfrom naiveBayesClassifier import tokenizer\nfrom naiveBayesClassifier.trainer import Trainer\nfrom naiveBayesClassifier.classifier import Classifier\n\nnewsTrainer = Trainer(tokenizer.Tokenizer(stop_words = [], signs_to_remove = [\"?!#%\u0026\"]))\n\n# You need to train the system passing each text one by one to the trainer module.\nnewsSet =[\n    {'text': 'not to eat too much is not enough to lose weight', 'category': 'health'},\n    {'text': 'Russia is trying to invade Ukraine', 'category': 'politics'},\n    {'text': 'do not neglect exercise', 'category': 'health'},\n    {'text': 'Syria is the main issue, Obama says', 'category': 'politics'},\n    {'text': 'eat to lose weight', 'category': 'health'},\n    {'text': 'you should not eat much', 'category': 'health'}\n]\n\nfor news in newsSet:\n    newsTrainer.train(news['text'], news['category'])\n\n# When you have sufficient trained data, you are almost done and can start to use\n# a classifier.\nnewsClassifier = Classifier(newsTrainer.data, tokenizer.Tokenizer(stop_words = [], signs_to_remove = [\"?!#%\u0026\"]))\n\n# Now you have a classifier which can give a try to classifiy text of news whose\n# category is unknown, yet.\nunknownInstance = \"Even if I eat too much, is not it possible to lose some weight\"\nclassification = newsClassifier.classify(unknownInstance)\n\n# the classification variable holds the possible categories sorted by \n# their probablity value\nprint classification\n```\n***Note***: Definitely you will need much more training data than the amount in the above example. Really, a few lines of text like in the example is out of the question to be sufficient training set.\n\n\n\n##What is the Naive Bayes Theorem and Classifier\nIt is needless to explain everything once again here. Instead, one of the most eloquent explanations is quoted here.\n\nThe following explanation is quoted from [another Bayes classifier][1] which is written in Go. \n\n\u003e  BAYESIAN CLASSIFICATION REFRESHER: suppose you have a set  of classes\n\u003e (e.g. categories) C := {C_1, ..., C_n}, and a  document D consisting\n\u003e of words D := {W_1, ..., W_k}.  We wish to ascertain the probability\n\u003e that the document  belongs to some class C_j given some set of\n\u003e training data  associating documents and classes.\n\u003e \n\u003e  By Bayes' Theorem, we have that\n\u003e \n\u003e     P(C_j|D) = P(D|C_j)*P(C_j)/P(D).\n\u003e \n\u003e  The LHS is the probability that the document belongs to class  C_j\n\u003e given the document itself (by which is meant, in practice,  the word\n\u003e frequencies occurring in this document), and our program  will\n\u003e calculate this probability for each j and spit out the  most likely\n\u003e class for this document.\n\u003e \n\u003e  P(C_j) is referred to as the \"prior\" probability, or the  probability\n\u003e that a document belongs to C_j in general, without  seeing the\n\u003e document first. P(D|C_j) is the probability of seeing  such a\n\u003e document, given that it belongs to C_j. Here, by assuming  that words\n\u003e appear independently in documents (this being the   \"naive\"\n\u003e assumption), we can estimate\n\u003e \n\u003e     P(D|C_j) ~= P(W_1|C_j)*...*P(W_k|C_j)\n\u003e \n\u003e  where P(W_i|C_j) is the probability of seeing the given word  in a\n\u003e document of the given class. Finally, P(D) can be seen as   merely a\n\u003e scaling factor and is not strictly relevant to  classificiation,\n\u003e unless you want to normalize the resulting  scores and actually see\n\u003e probabilities. In this case, note that\n\u003e \n\u003e     P(D) = SUM_j(P(D|C_j)*P(C_j))\n\u003e \n\u003e  One practical issue with performing these calculations is the \n\u003e possibility of float64 underflow when calculating P(D|C_j), as \n\u003e individual word probabilities can be arbitrarily small, and  a\n\u003e document can have an arbitrarily large number of them. A  typical\n\u003e method for dealing with this case is to transform the  probability to\n\u003e the log domain and perform additions instead  of multiplications:\n\u003e \n\u003e    log P(C_j|D) ~ log(P(C_j)) + SUM_i(log P(W_i|C_j))\n\u003e \n\u003e  where i = 1, ..., k. Note that by doing this, we are discarding  the\n\u003e scaling factor P(D) and our scores are no longer  probabilities;\n\u003e however, the monotonic relationship of the  scores is preserved by the\n\u003e log function.\n\nIf you are very curious about Naive Bayes Theorem, you may find the following list helpful:\n\n* [Insect Examples][2]\n* [Stanford NLP - Bayes Classifier][3]\n\n#Improvements\nThis classifier uses a very simple tokenizer which is just a module to split sentences into words. If your training set is large, you can rely on the available tokenizer, otherwise you need to have a better tokenizer specialized to the language of your training texts.\n\n## TODO\n* inline docs\n* unit-tests\n\n## AUTHORS\n* Mustafa Atik @muatik\n* Nejdet Yucesoy @nejdetckenobi\n\n\n  [1]: https://github.com/jbrukh/bayesian/blob/master/bayesian.go\n  [2]: http://www.cs.ucr.edu/~eamonn/CE/Bayesian%20Classification%20withInsect_examples.pdf\n  [3]: http://nlp.stanford.edu/IR-book/html/htmledition/naive-bayes-text-classification-1.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuatik%2Fnaive-bayes-classifier","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmuatik%2Fnaive-bayes-classifier","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmuatik%2Fnaive-bayes-classifier/lists"}