Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Train Naive Bayesian Classifier"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [],
- "source": [
- "from sklearn.naive_bayes import MultinomialNB\n",
- "classifier = MultinomialNB(alpha = 0.01).fit(train_tfidf_features, train_labels)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Test prediction with toy data"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 39,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[0. 0. 0. ... 0. 0. 0.]\n",
- " [0. 0. 0. ... 0. 0. 0.]]\n"
- ]
- }
- ],
- "source": [
- "docs_new = ['Doing business in China', 'New cold war with China']\n",
- "new_counts = vectorizer.transform(docs_new)\n",
- "new_tfidf = transformer.transform(new_counts)\n",
- "print(new_tfidf.toarray())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 40,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[3 3]\n",
- "['BenjaminKangLim' 'BenjaminKangLim']\n",
- "[[0.09206076 0.06376814 0.13749245 0.60536409 0.10131456]\n",
- " [0.13577759 0.07320687 0.09476922 0.59473204 0.10151428]]\n"
- ]
- }
- ],
- "source": [
- "toy_predicted = classifier.predict(new_tfidf)\n",
- "\n",
- "print(toy_predicted)\n",
- "print(label_encoder.inverse_transform(toy_predicted))\n",
- "print(classifier.predict_proba(new_tfidf))"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.8"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement