Last active
July 19, 2022 12:21
-
-
Save lowener/9dfa1e8b0a5883ec7c8cff0b4089de1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "id": "5957db9b", | |
| "metadata": { | |
| "tags": [] | |
| }, | |
| "source": [ | |
| "# TF-IDF + Multinomial\n", | |
| "\n", | |
| "Transform the text through a TF-IDF vectorizer, and run a multinomial naive Bayes model." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 10, | |
| "id": "637d0f2e", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "CPU times: user 55.4 ms, sys: 7.57 ms, total: 63 ms\n", | |
| "Wall time: 63 ms\n", | |
| "CPU times: user 20.3 ms, sys: 8.16 ms, total: 28.4 ms\n", | |
| "Wall time: 28.2 ms\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "0.9248046875" | |
| ] | |
| }, | |
| "execution_count": 10, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "vec = TfidfVectorizer(stop_words='english', ngram_range=(1,3))\n", | |
| "x_train = vec.fit_transform(X_train_text)\n", | |
| "x_test = vec.transform(X_test_text)\n", | |
| "\n", | |
| "mnb = MultinomialNB()\n", | |
| "%time mnb.fit(x_train, y_train)\n", | |
| "%time mnb.score(x_test, y_test)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 11, | |
| "id": "da09815a", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "CPU times: user 264 ms, sys: 67.6 ms, total: 332 ms\n", | |
| "Wall time: 332 ms\n", | |
| "CPU times: user 31.8 ms, sys: 27.9 ms, total: 59.7 ms\n", | |
| "Wall time: 59.4 ms\n" | |
| ] | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "0.9248046967473131" | |
| ] | |
| }, | |
| "execution_count": 11, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "vec = TfidfVectorizer(stop_words='english', ngram_range=(1,3))\n", | |
| "x_train = vec.fit_transform(X_train_text)\n", | |
| "x_test = vec.transform(X_test_text)\n", | |
| "x_train_np, x_test_np = x_train.get(), x_test.get()\n", | |
| "y_train_np, y_test_np = y_train.to_numpy(), y_test.to_numpy()\n", | |
| "\n", | |
| "mnb = MultinomialNB_sk()\n", | |
| "%time mnb.fit(x_train_np, y_train_np)\n", | |
| "%time mnb.score(x_test_np, y_test_np)" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "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.8.13" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment