Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "### Train Naive Bayesian Classifier"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 38,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "from sklearn.naive_bayes import MultinomialNB\n",
  17. "classifier = MultinomialNB(alpha = 0.01).fit(train_tfidf_features, train_labels)"
  18. ]
  19. },
  20. {
  21. "cell_type": "markdown",
  22. "metadata": {},
  23. "source": [
  24. "### Test prediction with toy data"
  25. ]
  26. },
  27. {
  28. "cell_type": "code",
  29. "execution_count": 39,
  30. "metadata": {},
  31. "outputs": [
  32. {
  33. "name": "stdout",
  34. "output_type": "stream",
  35. "text": [
  36. "[[0. 0. 0. ... 0. 0. 0.]\n",
  37. " [0. 0. 0. ... 0. 0. 0.]]\n"
  38. ]
  39. }
  40. ],
  41. "source": [
  42. "docs_new = ['Doing business in China', 'New cold war with China']\n",
  43. "new_counts = vectorizer.transform(docs_new)\n",
  44. "new_tfidf = transformer.transform(new_counts)\n",
  45. "print(new_tfidf.toarray())"
  46. ]
  47. },
  48. {
  49. "cell_type": "code",
  50. "execution_count": 40,
  51. "metadata": {},
  52. "outputs": [
  53. {
  54. "name": "stdout",
  55. "output_type": "stream",
  56. "text": [
  57. "[3 3]\n",
  58. "['BenjaminKangLim' 'BenjaminKangLim']\n",
  59. "[[0.09206076 0.06376814 0.13749245 0.60536409 0.10131456]\n",
  60. " [0.13577759 0.07320687 0.09476922 0.59473204 0.10151428]]\n"
  61. ]
  62. }
  63. ],
  64. "source": [
  65. "toy_predicted = classifier.predict(new_tfidf)\n",
  66. "\n",
  67. "print(toy_predicted)\n",
  68. "print(label_encoder.inverse_transform(toy_predicted))\n",
  69. "print(classifier.predict_proba(new_tfidf))"
  70. ]
  71. }
  72. ],
  73. "metadata": {
  74. "kernelspec": {
  75. "display_name": "Python 3",
  76. "language": "python",
  77. "name": "python3"
  78. },
  79. "language_info": {
  80. "codemirror_mode": {
  81. "name": "ipython",
  82. "version": 3
  83. },
  84. "file_extension": ".py",
  85. "mimetype": "text/x-python",
  86. "name": "python",
  87. "nbconvert_exporter": "python",
  88. "pygments_lexer": "ipython3",
  89. "version": "3.6.8"
  90. }
  91. },
  92. "nbformat": 4,
  93. "nbformat_minor": 2
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement